简体   繁体   中英

wordpress single custom post type template

I am trying to create single page for custom post types but i can't really achieve it. Here is all that i am trying out -

snippet from functions.php for the registration of the custom post type

<?php
function custom_post_type() {

$labels = array(
    'name'                => _x( 'Tutorials', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Tutorial', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Tutorial', 'text_domain' ),
    'parent_item_colon'   => __( 'Parent Tutorial:', 'text_domain' ),
    'all_items'           => __( 'All Tutorials', 'text_domain' ),
    'view_item'           => __( 'View Tutorial', 'text_domain' ),
    'add_new_item'        => __( 'Add New Tutorial', 'text_domain' ),
    'add_new'             => __( 'New Tutorial', 'text_domain' ),
    'edit_item'           => __( 'Edit Tutorial', 'text_domain' ),
    'update_item'         => __( 'Update Tutorial', 'text_domain' ),
    'search_items'        => __( 'Search Tutorials', 'text_domain' ),
    'not_found'           => __( 'No Tutorials found', 'text_domain' ),
    'not_found_in_trash'  => __( 'No Tutorials found in Trash', 'text_domain' ),
);
$args = array(
    'label'               => __( 'Tutorial', 'text_domain' ),
    'description'         => __( 'Tutorial information pages', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array('title','editor','author','excerpt','custom-fields'),
    'taxonomies'          => array( 'category', 'post_tag' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'page',
);
register_post_type( 'Tutorial', $args );
}
add_action( 'init', 'custom_post_type' ); ?>

and i am using the template named single-Tutorial.php for this custom post type template. Also, in my posts display i am using the_permalink() to link to the post.

Now consider a post with title test3 now in my dashboard i see its permalink as -

http://localhost/deadman/portfolio/tutorial/test3/

and when i echo the contents of the_permalink i get -

http://localhost/deadman/portfolio/tutorial/test3/ 

Now though they are pointing at the same place but still i am being redirected to the homepage rather than the single post type page

The name of custom post can't contains capital letters so change the register_post_type( 'Tutorial', $args ); to register_post_type( 'tutorial', $args );

Also the template file should be single-tutorial.php and not single-Tutorial.php .

Lastly you have to save the permalink settings to flush the news url's. That is need it because wordpress cache the rewrite rules and when you create a custom post wordpress don't automatically refresh the new rewrite rules.

Usefull links :

http://wp-bytes.com/function/2013/02/flushing-permalinks/

http://codex.wordpress.org/Rewrite_API/flush_rules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM