0

I'm trying to generate a full list of post categories with links based on post type.

So, let's say I'm on an archive page for the custom post type "reports" - when I click on a category link (for example "education") I'd like it to take me to a category archive that only shows posts with a custom post type of "reports" in the "education" category.

What's the best way to achieve this?

1 Answer 1

0

You can create a template page and use the WP loop: taxonomy-{taxonomy}-{term}.php https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/

Or you can use the get_posts functionality: https://codex.wordpress.org/Template_Tags/get_posts#Taxonomy_Parameters

Something like this maybe:

    <?php
      $args = array(
         'posts_per_page' => -1,
         'post_type' => 'reports',
         'my_custom_taxonomy_name' => 'education',
         'post_status' => 'publish'
     );

    $show_albums = get_posts( $args );
    ?>

This will return a list of posts in that category, you can then display them on the page.

8
  • Good solution, but wouldn't this require me to create a custom template for every single category? This is work for a client site, which I won't be maintaining, so if they were to add a new category the functionality wouldn't work for it. Whatever I do needs to be forward compatible so they can manage it on their own. Any other thoughts?
    – jongoeson
    Commented Jun 8, 2016 at 20:13
  • That's right, instead of option one taxonomy-{taxonomy}-{term}.php you would create the template page taxonomy-{taxonomy}.php which all terms will default too (the WP loop will work out what posts to display) developer.wordpress.org/files/2014/10/template-hierarchy.png
    – emily
    Commented Jun 8, 2016 at 21:17
  • I'm still not quite following - surely running a query on taxonomy-{taxonomy}.php is still a custom category template, isn't it? I'd still have to create taxonomy-education.php and taxonomy-my-category.php for each respective category?
    – jongoeson
    Commented Jun 8, 2016 at 22:01
  • No, because there aren't any category specific template pages they will default to using the taxonomy-{taxonomy}.php page (the wordpress template hierarchy link I sent shows the fallback template page in order of fallback). So you only need to create one template page, all individual categories will default to use that template. The template will pick up that it should only display posts from the category the user is in automatically within the WP loop.
    – emily
    Commented Jun 9, 2016 at 14:44
  • Sorry, I'm really not getting this. I don't understand how this works for automation purposes. If I have to use the loop or get_posts in that template, to filter my posts according to post_type, I still have to have individual args per category, no? I'm not using a custom taxonomy here. I'm using custom terms within the category taxonomy, and want to return category links (that are added automatically every time a new category is added) that display posts only from a certain post type. I'm really not sure how the above does that.
    – jongoeson
    Commented Jun 9, 2016 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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