Site logo

Archived topic

Category template for custom post type

10 replies · Started by Mike on January 30, 2019

Viewing posts 1–11 of 11

Having trouble creating a custom template for a CPT and custom taxonomy category page.

I have a CPT with two custom taxonomies. I want to list the categories in one of those taxonomies in a sidebar widget (same behavior as the core Categories widget).

I created the categories widget two ways, first with a plugin, Custom Post Type Widgets, and then with a function in Snippets and a shortcode in a Custom HTML widget. Both work, and the results of clicking a category is displayed with the archive.php template.

I'm using a child theme. In it, I created a custom template named archive-[CPT NAME].php but that is not recognized, archive.php is still used. I also tried category.php and category-[CPT NAME].php, and taxonomy-[CPT NAME].php, but archive.php was still used.

This seems to be the same thing as described in Template file name for Category under Custom Post Type.

I'm using the CPT UI plugin to create CPTs and taxonomies.

What filename do I use to override archive.php?

Those filenames should work as long as the CPT slug matches. Can you link me to one of those custom post type archives where it should be working?

I put the link in the original post. The sidebar on the right is generated by the Custom Post Type Widgets widget, for the CPT "course" and taxonomy "topic". Everything is associating properly, as the category listings are fine, it's just changing the template.

For no logical reason, just to see, I even copied the archive.php file to the child theme, and the snippet I have that shows which template file a page is using (full path appears at bottom of page when in as admin) shows that archive.php is then used from the child theme as it should be.

Really hope it's not something simple and stupid that I've done, because I go to great lengths not to post here in Support. But here I've tried everything I can think of. :)

So your archive-[CPT NAME].php would affect the content of your page. The link in your post is just a regular page - it's not an archive.

You can check by looking at the <body> classes. You'll see WordPress thinks it's just a regular page: http://prntscr.com/mf08fl

That means it would be using the page.php template.

Let me know if I'm misunderstanding :)

Situation: I have a custom post type (naem: course) with a custom category (name: topic). Want a sidebar categories widget that displays the topiccategories (should behave same as standard Categories widget). Used Custom Post Type Widgets plugin to create a CPT Categories widget here: right sidebar > "Course topics" widget

Problem: Trying to make a custom template for the category archive pages, but the WP template hierarchy doesn't seem to be working. The archive.php template is called when you click the links in the widget, for example: a category archive page from that category widget. When archive-course.php or taxonomy-topic.php are present, the are not recognized.

In the last link, look above left of the main page title for the text "this is archive.php" -- I entered that in archive.php as a marker.

These files are all there right now, but archive.php is still being used.

in generatepress_child theme dir:
   archive.php 
   archive-course.php
   taxonomy-topic.php

Only archive.php is being recognized, not either archive-course.php or taxonomy-topic.php.

Hope that's clearer!

Nope. Ran across that suggestion in my searches, but didn't seem to directly relate to this problem. But I just tried: saved permalinks Plain, then back to Post name, cleared browser cache, and tried in different browser, and same, only archive.php is used.

How would you recommend creating a Categories widget for a CPT and custom taxonomy, with a custom template for its category archive page?

I thought you had the widget all working? We just need to figure out why the template isn't working?

If you install the Query Monitor plugin, does it tell you the list of templates the page is using? Feel free to shoot me some login details once you install it if you want me to take a look: https://generatepress.com/contact

Yes, the widget is working.

Will install Query Monitor and get back to you! :)

Grrr, SOLVED, and, in the end, it was something simple and stupid that I should've caught.

It was a wrong taxonomy template filename. Was uploading and renaming archive.php, and meant to rename it taxonomy-topic.php but actually entered taxonomy-course.php (the CPT name, not the tax name). In my mind it was properly named, but not in reality... Noticed in Query Monitor.

Thanks!!!!

Aha! Glad you got it working :)

This archived topic is closed to new replies.