• Hi everyone, I’m developing a theme with several post types and having some problems with the templates.

    I need to show a page for each post type and also show the categories for each post type.

    For now I’m using the archive-{post-type}.php to show the last posts, it’s perfect if I don’t need to show the categories. When I get the categories, WP shows the category.php. Should I use conditionals to load specific part of the template or there is a way to use a custom template file to show a category from a specific post type?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Have you looked at the Template Hierarchy?
    One thing that you might not have noticed is that all templates (and all queries, actually) are for posts. There are none for taxonomy objects. And there aren’t mixtures of post type and taxonomy either. If the taxonomy is unique to the post type, using the taxonomy URL will show only that post type. But if you extended “category” to your custom post type, then it will show a mixture of types.

    Thread Starter garciaporto

    (@garciaporto)

    Hi Joy, thank you for take the time to answer me.

    I have been thinking a way to solve this and I noticed if I could get the taxonomy of the post and show a template depending on this value could solve the problem. But my knowledge about hooks doesn’t go that far and don’t know how to ask for what I need.

    I don’t know if this a right way to solve it, but it’s what I came with. If you know a better way, I’m here reading.

    This is the idea:

    if('kitchens' == get_post_type()):
        get_template_part( 'cat-kitchen' );
      elseif('projects' == get_post_type()):
        get_template_part( 'cat-projects' );
      elseif('outlet' == get_post_type()):
        get_template_part( 'cat-outlet' );
      else:
        get_template_part( 'cat-blog' );
      endif;

    I doesn’t seem like you need this much distinction. You are using get_post_type, but referring to different categories, so what are you really doing? Or did you just use that function because you didn’t know the taxonomy function?

    if I could get the taxonomy of the post and show a template depending on this value

    One thing that you can do is put the term as a class and style it differently. Or do that with the post type.
    Another thing is that you can call the function like this, once, instead of many if statements:
    get_template_part( 'cat-type', get_post_type() );
    That way, it will use the cat-type-kitchens.php part for a ‘kitchens’ post type, and the cat-type-post.php part for a ‘post’ post type. (or if it doesn’t find cat-type-post.php, it will use cat-type.php)
    Or use the taxonomy term instead, although there could be quite a few on each post, so I still don’t understand what you are attempting.

    Thread Starter garciaporto

    (@garciaporto)

    When WP shows categories, uses the category.php template. I don’t know if I can rename the template to show only categories from a custom post type, what I do know is if I rename the template as category-{category-ID}.php it can show a specific template for that category, but it isn’t what I need.

    What I need is to show categories from a specific post type, the template to show it may vary depending on the post type.

    If you know a way to accomplish this task, let me know. I’m stuck at this point

    It is still unclear what you want. As I said before, the query dictates the template, and they are all oriented to retrieving posts of one designation. Either a post type query or a taxonomy query. There are no mixtures.
    The category.php template is used for the standard category taxonomy, so it is for the post post type. If you use the standard taxonomy on a custom post type, the category query result can be a mixture of post types that use the category.php template (or fall back to the archive.php template).
    If you define a custom taxonomy for a custom post type then a query for the taxonomy will show only that post type. The template would be taxonomy.php unless you make more specific templates like taxonomy-$taxonomy.php or taxonomy-$taxonomy-$term.php. Within that template, you can use the get_template_part with second parameter if you need to.

    If you have the URL figured out for the query, the template is easy to find on the hierarchy chart. If you can’t figure out the URL, it’s an indication that maybe it doesn’t exist. The data has to be retrieved by WP so the template can show it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘custom post type categories template’ is closed to new replies.