Site logo

[Support request] Template for custom post types is not working

Home Forums Support [Support request] Template for custom post types is not working

Home Forums Support Template for custom post types is not working

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #1827377
    ilie

    Hello,

    On the support page here:
    https://docs.generatepress.com/article/setting-up-a-simple-custom-post-type/

    Step 3, is not working.

    Changing that line from:
    generate_do_template_part( 'single' );

    To
    generate_do_template_part( 'project' );

    Results in an empty page…

    The only way I can get the new templates to work is to change it to this instead:
    get_template_part( 'content', 'project' );

    If you look inside the ‘generate_do_template_part’ helper function, it will completely ignore anything but the standard template parts. So is the documentation outdated? Or am I doing something wrong here?

    #1827378
    ilie

    These old tickets seem to have the same solution as I found, which means the documentation needs to be updated to reflect the source code.
    https://generatepress.com/forums/topic/custom-post-type-template/
    https://generatepress.com/forums/topic/custom-post-type-template/

    #1828024
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Thanks for the heads up, I just updated the article.

    Let us know if you have any other questions 🙂

    #2331988
    Burke

    I’m having the same problem as ilie. The only way I can make anything show up on the page is if, in single-mycpt.php I use:

    get_template_part( ‘content’, ‘mycpt’ );

    Otherwise it’s just blank nothingness – neither the post content nor anything in content-mycpt.php is appearing on the page.

    However, I need to change the whole template. Please advise.

    #2332220
    David
    Staff
    Customer Support

    Hi there,

    so are you not wanting a content-mycpt.php ?

    #2332231
    Burke

    I want the best option to make all of the Generateblocks work properly. What I will probably do is use a single-mycpt.php, keep generate_do_template_part( 'single' ); in it, and use if ('mycpt' !== get_post_type())... where I have to.

    I really wish I could keep all of my headers and content completely separate from content-single.php though. This seems like something Generatepress should make easier for us.

    #2333316
    David
    Staff
    Customer Support

    So GP does handle that.
    You have the Single template, which contains the Loop and the stuff outside the loop, e.g Header , Footer and other hooks.
    Then the Content template which is litterally just the container for your post content.

    I am not sure what the issue is. Can you explain a little on what you want to achieve ?

    #2333339
    Burke

    I figured out some things on my own, but this is what is happening:

    1. My custom post type is ‘winery’.

    2. In my child theme I copied over single.php and content-single.php

    3. I renamed the two files single-winery.php and content-winery.php

    At this point the custom post type and the comments section are showing up fine with the wysiwyg text (i.e. the_content()) appearing on the custom post.

    4. I change the line generate_do_template_part( 'single' ); to generate_do_template_part( 'winery' ); and both the_content() and the comments section disappear.

    5. If I replace generate_do_template_part( 'winery' ); with get_template_part( 'content', 'winery' ); then the_content() appears but not the comments section.

    I’m using MAMP locally and have to refresh once or twice for things to appear correctly – that was the source of my original confusion.

    But, why did the comments section disappear? And what is the difference beteen generate_do_template_part and get_template_part ?

    #2333800
    David
    Staff
    Customer Support

    Ah ok. To cover:

    generate_do_template_part is a function we implemented so you could hook in your own templates without the need for child themes. It’s what we use for the Block Element – Content Template.

    You can see the function here:

    https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/theme-functions.php#L553

    if the $template matches the core templates it returns a get_template_part callback.
    It also has its own before > generate_before_do_template_part and after > generate_after_do_template_part hooks.

    And the latter one is where we call the comments template:

    https://github.com/tomusborne/generatepress/blob/adfe090929b0515cdf894f4c6b722cfe8c0790dc/inc/structure/comments.php#L192

    So this does introduce choices.

    You can either:

    1. The traditional child theme methods and use get_template_part( 'content', 'winery' );
    You will need to hook in the <?php generate_do_comments_template('single'); ?> in your content template.

    2. Use the generate_do_template_part callback, and then you can do something like this:

    
    // Disable the core template part
    add_filter( 'generate_do_template_part', function( $do ) {
        if ( is_singular('winery') ) {
            return false;
        }
        return $do;
    } );
    
    // Hook in your own template.
    add_action( 'generate_before_do_template_part', function() {
        if ( is_singular('winery') ) {
            get_template_part( 'content', 'winery' );
        }
    } );
    

    The latter method, probably makes more sense if you want to use the Block Element – Content template or take advantage of the extra GP hooks

    #2334015
    Burke

    So do I just put that big section of code you provided into single-winery.php instead of (replacing) generate_do_template_part( 'winery' );?

    I did that and the page is all still blank.

    #2334037
    David
    Staff
    Customer Support

    So you did this:

    2. In my child theme I copied over single.php and content-single.php

    3. I renamed the two files single-winery.php and content-winery.php

    At that point you would still have the original generate_do_template_part( 'single' ); in your single-winery template.
    You can leave that as is.

    And then the code i provided you add to your functions.php.

    #2334039
    Burke

    Perfect. That did it. Thank you!

    #2334054
    David
    Staff
    Customer Support

    You’re welcome – thanks for your patience. Its something we need to make a little clearer 🙂

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