- This topic has 7 replies, 3 voices, and was last updated 2 years, 9 months ago by Tom.
-
AuthorPosts
-
June 12, 2021 at 3:12 am #1819510Ovidiu
Hi,
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()
toget_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
June 12, 2021 at 6:47 am #1819625DavidStaffCustomer SupportHi 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.June 12, 2021 at 8:35 am #1819869OvidiuHi 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' )
togenerate_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.
June 12, 2021 at 4:59 pm #1820126DavidStaffCustomer SupportUse a copy of the
content-single.php
to create yourcontent-single-reviews.php
content template. You can then update that template to output your requirements.June 13, 2021 at 1:15 am #1820271OvidiuHi David,
That’s exactly what I did but for some reason it doesn’t work after changing
generate_do_template_part( 'single' )
togenerate_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' )
withget_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.
June 13, 2021 at 7:34 pm #1821043TomLead DeveloperLead DeveloperThat 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 ๐
June 14, 2021 at 12:24 am #1821168OvidiuAwesome, many thanks, Tom.
June 14, 2021 at 8:07 pm #1822260TomLead DeveloperLead DeveloperNo problem ๐
-
AuthorPosts
- You must be logged in to reply to this topic.