0

I'm currently using archive.php as a generic listing for categories, tags, authors, etc. However, my custom post types have a very customized archive page that's completely different from archive.php.

I'm wondering if there's a way to create one archive file that acts as a generic template for only my custom post types. If it was included in the Template Hierarchy, it would likely be called archive-post-type.php or post-type.php.

I know I can use includes or get_template_part, but it's really just the same archive file across multiple post types, so I'd rather just create one file for all of them. Ideally, this CPT archive template could still be overridden by archive-$posttype.php.

If I utilized archive.php instead for the custom post types, I'd have the same problem in reverse, since category.php, tag.php, date.php, etc. would all be the same template, yet still different from archive.php.

  • Could you please start using syntax highlighting? – kaiser Sep 6 '14 at 10:01
  • Yup, I will from now on. Didn't realize that I should. – David Sep 6 '14 at 10:04
3
add_filter( 'template_include', 'wpsites_cpt_archive_page_template', 99 );

function wpsites_cpt_archive_page_template( $template ) {

    if ( is_post_type_archive()  ) {
        $new_template = locate_template( array( 'your-cpt-template.php' ) );
        if ( '' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}

Try this in your functions file.

You can add your post types to this line like this:

if ( is_post_type_archive(array( first-post-type, second-post-type ) ) ) {

Uses is_post_type_archive & template_include

  • If someone also wanted to use a number of individual CPT templates, this method would probably be more trouble than it's worth, but since I want ALL of my CPT archives to use the same template, this is a great solution for me. – David Sep 7 '14 at 15:49
  • It took me hours to find this answer, and it's exactly what I needed. Thanks so much! – Christian Varga Sep 1 '15 at 2:27
0

I think that Brad's answer point in the right direction if I understand you correctly.

I really do think that you need to rethink your use of templates. Using archive.php to act as a template for all the other archive types cramps one style quite a lot. It is just easier to create all these archive type templates and create the opportunity for a wider use with your content and styling. You can simply just copy arhive.php and rename it category.php for instance, it is that easy. All you need to do is to adopt styling for each archive type

This also opens up more opportunities in the Template Hierarchy. I'm not going to go into this, you can just follow the link provided and study it :-)

The template hierarchy doesn't offer much for the archive.php template, you have two choices, archive.php or archive-{$post_type}.php. You can go outside of this, but you will then need redirects and template includes (as Brad pointed out), which unnecessarily clogs up your functions file. Why use extra code?

I truly think that your best answer here is to make use of the different templates available in the template hierarchy. It will save you on writing unnecessary code, will be faster as you cut out unnecessary code, gives your more freedom when it comes to individual styling and is less confusing. So create an archive template for each post type, keep within the template hierarchy, and I bet you will have less headaches

  • Pieter, I agree... on most sites, using the various archive templates would be best for flexibility. On this site, the blog just isn't prominent enough for me to bother all of those. Doing so would be major redundancy of code. I am very familiar with the Template Hierarchy and I'm trying to keep my code simple. This website is a special case where it appears that the simplest way may be to do something like Brad suggested. It feels like the new template I'm talking about is a missing piece of the template hierarchy. – David Sep 6 '14 at 22:06
  • Thanks for your reply. I do respect the desision your made, but I would still have gone with the different templates, even if it was a bit over-kill. It does feel a bit if you are trying to reinvent the wheel, :-), but that is my opinion. I would agree that there is sometimes templates missing from the hierarchy that should have actually been there. Unfortunately I don't have the answer you are looking for, all I can say is, I hope you get this solved :-). Enjoy – Pieter Goosen Sep 7 '14 at 6:41
0

I guess you can make a page template and add the following code in that:-

<?php $args = array( 'post_type' => 'your_custom_post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_title();
    echo '<div class="entry-content">';
    the_content();
    echo '</div>';
endwhile; ?>

Hope this helps you solve your problem.

  • 2
    Please do properly format the code you are posting - thanks. You can find information about the editing options on the editing help page of the help center. – Nicolai Sep 6 '14 at 12:37
  • Custom queries in place of the main query always creates more issues that solving the original one issue – Pieter Goosen Sep 6 '14 at 12:38
  • 1
    Actually this is what I currently have, but it requires hacking, even moreso to get pagination working. It's doable but not ideal... using the main query is much better, as Pieter said. – David Sep 6 '14 at 21:40

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.