Archived topic
Merge Hook Element with Post Title
23 replies · Started by Magnus on February 22, 2022
Thank you David, so I need to add your code snippet in to the function.php or do I generate a new Hook in Elements for that page with that code?
It would need to go in functions.php - and just the second code ie.
function db_rerender_post_title_block( $block_content, $block ) {
$subtitle = get_post_meta( get_the_ID(), 'taq_review_score', true );
if ( ! empty( $subtitle ) && 'core/post-title' === $block['blockName'] ) {
$content = '<div class="post-title-wrap"><p class="custom-subtitle">Enduro Score ' .round(($subtitle/10), 1). '</p>';
$content .= $block_content;
$content .= '</div>';
return $content;
}
return $block_content;
}
add_filter( 'render_block', 'db_rerender_post_title_block', 10, 2 );
The first code was just an example of how the filter works.
Works :-) Thank you!
Hello David,
sorry for asking again. Your colleague Ying noted to change from core query loop to query loop block of GenerateBlocks. So I did an now I need to add my Enduro Score to this page: https://ride-with-love.bike/ride-with-love-der-spezialist-fuer-mtb-enduro-alpencross-und-mehrtagestouren/
Could you update the code snippet for me? I'm looking forward to your support, but I'm not a coder :-(
Thank you!
Mags
function db_rerender_post_title_block( $block_content, $block ) {
$subtitle = get_post_meta( get_the_ID(), 'taq_review_score', true );
if ( ! empty( $subtitle ) && 'core/post-title' === $block['blockName'] ) {
$content = '<div class="post-title-wrap"><p class="custom-subtitle_v2">' .round(($subtitle/10), 1). '<span>Enduro Score</span></p>';
$content .= $block_content;
$content .= '</div>';
return $content;
}
return $block_content;
}
add_filter( 'render_block', 'db_rerender_post_title_block', 10, 2 );
Hi Mag,
If you are using GB's query loop block, you won't need PHP code.
You can add a headline block, enable the dynamic data, choose post meta as data type, then choose taq_review_score from the dropdown list as Post meta field.
In order to see the meta in the dropdown list, you need to make sure the custom field taq_review_score is available via the REST api.
For more info, check this topic:
https://community.generateblocks.com/t/setting-post-meta-field/650
Hello Ying,
thank you. This solution works. But I struggle to get it in the desired solution. See example: https://www.screencast.com/t/KTieOZohkGr
In my case I need to format the taq_review_score from e.g. "97.5" to "9.8" and need to format this value via CSS in the desired look & feel. I feel this was more easier for me with the php code in the functions.php.
I tried to put the post title and the taq_review_score in a grid in one line. But formatting the taq_review_score from e.g. "97.5" to "9.8" was not possible for me. And I guess if I give the Post Meta field an own dedicated class I would be possible to design it via CSS.
So for now I see two options:
1. Use the Post Meta Field if possible to format value from e.g. "97.5" to "9.8"
2. if not, include the taq_review_score via php and functions.php
What do you think would work the best?
Thank you, Mags
You can use this snippet to filter in the post_meta:
function db_rerender_post_title_block( $block_content, $block ) {
$subtitle = (float)get_post_meta( get_the_ID(), 'taq_review_score', true );
if ( ! empty( $block['attrs']['className'] ) && 'post-title-block' === $block['attrs']['className'] ) {
$content = '<div class="post-title-wrap"><p class="custom-subtitle_v2">' .round(($subtitle/10), 1). '<span>Enduro Score</span></p>';
$content .= $block_content;
$content .= '</div>';
return $content;
}
return $block_content;
}
add_filter( 'render_block', 'db_rerender_post_title_block', 10, 2 );
You need to select the Headline block thats displaying the title and in Advanced > Additional CSS Class(es) add: post-title-block
You will notice that is the class i used in the condtion:
if ( ! empty( $block['attrs']['className'] ) && 'post-title-block' === $block['attrs']['className'] ) {
Also note i edited this line: $subtitle = (float)get_post_meta( get_the_ID(), 'taq_review_score', true ); to include the float operator so we make sure the Custom Field is a numerical value before we start dividing it.
GREAT, works perfect :-) Thank you!
Awesome - glad to hear that!