Separate content.php for custom post type

Hi there,

I have a custom post type called project and I want it to look different than the normal posts. I successfully created a archive-project.php and it does a great job customizing everything above and below the loop.I’m struggling to find the correct way of changing the appereance for the loop, so everything which is inside content.php for normal posts. content-project.php was my initial guess but without any success.

Thanks for any hints! :slight_smile:

PS: I’m struggling with issues like this on a regular basis, missing a reference where I can look it up quickly. The theme wrapper post always comes into mind, but is way to complex and doesn’t answer many practical questions in cases like this.

1 Like

Figured it out in my own:

in archive-project.php:

Changing

<?php while (have_posts()) : the_post(); ?>
 <?php get_template_part('templates/content-project', get_post_format()); ?>
<?php endwhile; ?>

To point to the right template part.

1 Like

This actually has nothing to do with the wrapper; it’s just how we break down our templates into reusable chunks. The wrapper governs the relationship between the content and the general page markup, but it doesn’t affect the content at all.

Excuse me jumping on this thread, but I thought it cleaner than starting a fresh as its relevant.

I’ve spent half hour looking but not joy. I have a similar situation where I’ve successfully created an archive page, but I want a custom content-single.php. I also had a stab with (all in the templates folder) content-portfolio.php and portfolio-single.php and single-portfolio.php but none of the above work.

I didnt follow the previous comment and tried update the following, but must be missing something

<?php get_template_part('templates/portfolio', get_post_type() != 'post' ? get_post_type() : get_post_format()); ?>

The documentation doesn’t seem to cover single page templates explicitly - or I’m misreading it.

Thanks

Nevermind, I’ve worked it out. Hopefully this will help though. Can I suggest we update the documentation to be a little more complete for those that aren’t that up on their Wordpress

Custom Post Types

Archive page -> Copy index.php (in root)to archive-[CPT slug]

Archive page - individual element -> Copy content-single.php (in templates) to something like content-single-portfolio.php and then update the Template reference within your archive-[CPT slug] to something like:

<?php get_template_part('templates/content-single-portfolio', get_post_type() != 'post' ? get_post_type() : get_post_format()); ?>

Single page -> Copy single.php (in root) to single-[CPT slug].php

3 Likes