Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Boris Kamp
Boris Kamp
16,660 Points

How to build a wordpress theme.

Hi guys!

Im following the 'how to build a WP theme' course and want to customize further.

I have created custom post types and templates. and was wondering how I could create archive pages for the custom categories that I created for that custom post type.

When I create archive.php or categories.php, it is not used by the custom tags or categories. My archive.php is only used by the standard 'post' type.

One more thing, lets say I have 3 post types:

  • Reviews A
  • Reviews B
  • Reviews C

And I need one common category among all 3 post types (category A), but also need to be able to display archive pages for all 'Reviews A' with 'Category A', 'Reviews b' with 'Category A' etc. but also ALL Reviews with Category A. How would I do this?

Thanks!

4 Answers

Nathalie C
Nathalie C
2,594 Points

I used the Custom Post Type UI plugin to create the taxonomies, attaching each custom taxonomy to my custom post type. For example, I created a custom post type 'work' and then created two taxonomies of 'works tags' and 'project tags', making sure that each was attached to my custom post type 'work' by checking the correct 'attach to post type' box I then placed the plugin-generated code for each taxonomy into my functions.php file.

To pull the taxonomy into the taxonomy.php file I used

<?php single_cat_title(); ?>

The code in more detail was

<p class="parahighlight">Project Tags: <?php single_cat_title(); ?></p>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'work' ); ?>
<?php endwhile; else: ?>
<p>There are no posts or pages here yet</p>
<?php endif; ?>

I do use the normal archives.php also for my blog post categories and tags.

I may have oversimplified what I need to do. I am still getting to grips with Wordpress having worked a couple of times through the 'How to Build a Wordpress Theme' tutorial, so I apologise if this isn't quite correct, but I think this is how I got it to work.

I found this link https://codex.wordpress.org/Template_Hierarchy very helpful.

Boris Kamp
Boris Kamp
16,660 Points

Thanks for your explanation Nathalie! I will look into it in the coming weeks. Thanks!

Nathalie C
Nathalie C
2,594 Points

I used taxonomy.php for my custom post type.

Boris Kamp
Boris Kamp
16,660 Points

Interesting, Can you please give me more info on this? how did you get WP to recognize taxonomy.php to use for the archives? and do you have a normal archive.php as well?

Zac Gordon
Zac Gordon
Treehouse Guest Teacher

The normal archive.php would still work as the default post type archive page

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Try checking out this post on WP Beginner: http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/

We have to make sure archives are enabled and then it's a matter of just using the correct template, as Nathalie points out.

On a related note, I am starting work on a WordPress Template Hierarchy Deep Dive that will get into this in more depth. Sorry it might not be out till after you finish your current project though ;p

Boris Kamp
Boris Kamp
16,660 Points

Thanks for the link! Would be very interested in you new deep dive! any ETA?

Thanks!

Boris Kamp
Boris Kamp
16,660 Points

Thanks for asking Zac! Im on holiday for a while so have no time to really work things out at the moment. However, I will definitely get back on this!

Thanks Zac!