0

Here i read that if i create a custom post type, Wordpress will look to its specific template first, then it falls back to single.php.

I registered a custom post type (i'll show the taxonomy as well), which is properly shown in admin; i can create them well and i've been able to add them to the list of posts in blog's main page. However, when i try to visit its page, i'm not getting a 404 error, but title is not shown in head section and it uses the index.php template, although i have a single.php which is working with common posts and i'd like to use. My new type (in functions.php)

function mytheme_customtypes () {
    $post_args = array(
        'label' => _('Blog Post'),
        'description' => _('Site\'s blog posts'),
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'taxonomies' => array('blog_posts'),
        'supports' => array('title', 'editor', 'comments', 'trackbacks', 'thumbnail'),
        'capability_type' => 'post',
        'rewrite' => array('slug' => 'blog', 'has_archive' => 'blog_posts', 'with_front' => false)
    );

    $tax_args = array(
        'label' => 'Blog posts',
        'description' => _('Raccolta dei post per il blog'),
        'show_ui' => true,
        'show_in_nav_menus' => true,
        'rewrite' => array('slug' => 'blogs',  'with_front' => false),
        'hierarchical' => true
    );

    register_post_type('blog_post', $post_args);

    register_taxonomy('blog_posts', 'blog_post', $tax_args);
}
add_action('init', 'mytheme_customtypes');

Trying to create a new template name single-blog_post.php had no effect. Any help is appreciated

1

public is false by default, your post type will not be visible on the front end without explicitly setting it to true.

$post_args = array(
    'public' => true,
    'label' => _('Blog Post'),
    'description' => _('Site\'s blog posts'),
    'show_ui' => true,
    'show_in_nav_menus' => true,
    'taxonomies' => array('blog_posts'),
    'supports' => array('title', 'editor', 'comments', 'trackbacks', 'thumbnail'),
    'capability_type' => 'post',
    'rewrite' => array('slug' => 'blog', 'has_archive' => 'blog_posts', 'with_front' => false)
);
  • That's exactly what happened. Due to some lacks on my english, i thought that parameter would allow any wp user to edit it. Thanks, problem solved. – Daniele Squalo May 25 '17 at 17:38
0

I am not sure I follow your question entirely (the bit about "adding" them to the blog). However from your description I think you are missing distinction between archive templates and singular templates.

index.php is catch–all template, it is last fallback for both archives and singular branches of hierarchy.

But before that the logic is different, roughly:

  • archive-blog_post.php > archive.php > index.php for archive
  • single-blog_post.php > single.php > index.php for singular

So if you are looking at archive of your CPT then any singular templates for it that you have are irrelevant. And other way around.

You would need to create both templates for your CPT if you want to customize both archives and singular entries for it.

  • Actually my problem is for singular post only. My wp is going straight to index.php template ignoring single.php and single-blog_post.php – Daniele Squalo May 25 '17 at 17:32
  • Then it is not normal WP behavior and hard to guess without hands–on debug. Generally WP hierarchy just works, unless something messes it up or there are issues with set up (template files are not actually in expected place and such). Double check everything. – Rarst May 25 '17 at 17:35
  • I'll try debugging, but i just think i did something wrong on my custom type setup – Daniele Squalo May 25 '17 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.