[Resolved] generate_do_template_part vs custom template in child theme

Home Forums Support [Resolved] generate_do_template_part vs custom template in child theme

Home Forums Support generate_do_template_part vs custom template in child theme

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
  • #1819510


    I am using a custom post type on my site called reviews (created with the Toolset plugin) where I use some extra stuff/info compared to the regular posts I have on the site. Precisely, in a review I am also outputting JSON-LD review schema in the <header> and some other info about the product being reviewed and are all stored in custom meta fields (created via the same Toolset plugin). Until using GP+Premium I would simply use a custom template in my child theme with the -review slug in the file name to only target the reviews and not the regular posts.

    Here’s what I did so far with GP:

    • Created a child theme
    • copied the single.php file in my child theme, renamed it to single-reviews.php
    • copied the header.php file in my child theme, renamed it to header-reviews.php
    • created the JSON-LD review schema code in the <header> section of the header-reviews.php
    • changed the get_header() to get_header('reviews') in single-reviews.php

    So basically I took care of the JSON-LD schema part, but I still need to display some info like price, buying link and so on in the actual body of the review, right before the actual content (retrieved via the loop).

    For that, I would need to also create a new content-single-reviews.php and insert the code that retrieves the above-mentioned info from the meta fields. I have two questions regarding that:

    – How do I modify the code inside single-reviews.php to use the newly created content-single-reviews.php?
    – Would I be able to achieve it in a simpler and more efficient way with the new generate_do_template_part? I didn’t manage to find anything in the docs.

    I am sorry for the long post, I tried my best to explain what I want to achieve and provide some context. I am working on a local clone of the site, so I cannot provide a live link.

    Thank you for your time

    Customer Support

    Hi there,

    if you kept the single.php template as per the GP templates you should have this line:


    Which is is where the request for the content-single is made or in your case you would have content-single-reviews.
    As long as the generate_do_template_part function is your template then you will be able to use a Block Element Content Template – just make sure your Display Rules are set to Reviews > All Reviews.


    Hi David,

    Thank you. Yes, my single-reviews.php is currently identical to the one in GP, I only changed the file name. The only thing I need now is for it to use my content-single-reviews.php instead of the content-single.php.

    For that, I tried changing the generate_do_template_part( 'single' ) to generate_do_template_part( 'single-reviews' ), but doing that will not show any content at all. Have I misunderstood what you suggested above?

    Also, I must have this implemented entirely in my theme’s templates as I haven’t yet started using Gutenberg/blocks (long overdue, I know). Still using the classic editor.

    Thanks again for your help.

    Customer Support

    Use a copy of the content-single.php to create your content-single-reviews.php content template. You can then update that template to output your requirements.


    Hi David,

    That’s exactly what I did but for some reason it doesn’t work after changing generate_do_template_part( 'single' ) to generate_do_template_part( 'single-reviews' ) inside single-reviews.php. The review content would simply be blank. It just won’t use the content-single-reviews.php template. I even installed the What the file plugin and content-single-reviews.php is not being listed as a template part.

    As a side note, when replacing generate_do_template_part( 'single-reviews' ) with get_template_part( 'content-single', 'reviews' ) it seems to work as it should (although the comments and comment form are not being displayed).

    Later edit: after coming across a post by Tom I left the generate_do_template_part( 'single' ) unchanged inside single-reviews.php added the following code in functions.php

    // Disable the core template part on the blog page.
    add_filter( 'generate_do_template_part', function( $do ) {
        if ( is_singular('reviews') ) {
            return false;
        return $do;
    } );
    // Add our own.
    add_action( 'generate_before_do_template_part', function() {
        if ( is_singular('reviews') ) : ?>
            <?php get_template_part( 'content-single', 'reviews' );?>
        <?php endif;
    } );

    This seems to work and also show the comments. Any feedback on this method and its reliability will be much appreciated. Thanks.

    Lead Developer
    Lead Developer

    That method you posted is perfect.

    It basically tells GP not to output its template for reviews, and then allows you to hook in your custom template part ๐Ÿ™‚


    Awesome, many thanks, Tom.

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