[Resolved] Custom post type file template

Home Forums Support [Resolved] Custom post type file template

Home Forums Support Custom post type file template

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #405654
    Alberto

    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!

    #405908
    Tom
    Lead Developer
    Lead Developer

    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.

    #406190
    Alberto

    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

    #406684
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #406803
    Alberto

    Hi Tom!
    That’s exactly what I need!

    Thank you very much!

    #407106
    Tom
    Lead Developer
    Lead Developer

    You’re very welcome 🙂

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.