[Resolved] Custom post type template

Home Forums Support Custom post type template

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #245667
    Fatih

    Hi, I’m trying to create a template for a custom post type. I have created the file single-posttype.php. and I have noticed that it also uses the content.php. How can I create content-posttype.php and make single-posttype.php use it? I know this sounds complicated but I hope I made it clear.

    Thanks

    #245759
    Tom
    Lead Developer
    Lead Developer

    In your file, find:

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

    Now if your file is named content-posttype.php, replace it with:

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

    #245790
    Fatih

    Awesome, thank you so much πŸ™‚

    #245891
    Tom
    Lead Developer
    Lead Developer
    #405422
    Alberto

    Hello Tom!
    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!

    #405890
    Tom
    Lead Developer
    Lead Developer

    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.

    #808941
    s2prod

    Dear Tom,

    If i want to create a custom page, which page of your theme will be most usefull to get a basis ?

    content.php, page.php ??

    #809486
    Tom
    Lead Developer
    Lead Developer

    It depends on which areas you’re editing. page.php is likely the one you want to edit.

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