Site logo

Archived topic

Custom post type file template

5 replies · Started by Alberto on October 18, 2017

Viewing posts 1–6 of 6

Hello!
Excuse because I post this in this other resolved post
This information is very useful, thanks!

I’m also working with custom post types and I want to edit not only the page template but also the archive page. So in my child theme I’ve created single-myCPT.php where I wrote
<?php get_template_part( 'content', 'single-myCPT' ); ?>

I’ve also created archive-myCPT.php but I don’t know how to reference my custom content replacing get_post_format().
<?php get_template_part( 'content', get_post_format() ); ?>

Hope I explained well…

Thanks in advance!

Hi Alberto,

Here's what I said in the other topic - we can use this one to continue discussing :)

This line: get_template_part( 'content', 'single-myCPT' )

Will look for a file like this: content-single-myCPT.php

If you’re already using a custom single-myCPT.php file, you might as well copy the code inside content-single.php, and replace this with it:

get_template_part( 'content', 'single' )

get_template_part() is cool as it allows you to only change the loop – not the entire single.php file. In your case, it’s probably easier to keep everything in one file.

Hello Tom!
I'm not sure I'm understanding you...
I've got single-myCPT.php file where I wrote this:

<?php get_template_part( 'content', 'standhouders' ); ?>

And I've got content-myCPT.php (copied from content-single) that's called from the code above.

Now, I want to do the same with a file template (the archive file), so I've got the file archive-myCPT.php file that has this code by default:

get_template_part( 'content', get_post_format() );

So, If I want to modify the file template (copied from archive.php file) how can I do that?
I want to show not only the content but other custom fields in the archive page.

Hope I explained well…

Regards

So if you need to modify what's inside the get_template_part() on your custom archives page, you would change:

get_template_part( 'content', get_post_format() );

To:

get_template_part( 'content', 'archiveCPT' );

Then your file would be: content-archiveCPT.php

Hope I'm understanding correctly :)

Hi Tom!
That's exactly what I need!

Thank you very much!

You're very welcome :)

This archived topic is closed to new replies.