Archived topic
Merge Hook Element with Post Title
23 replies · Started by Magnus on February 22, 2022
Hello,
I've added a Hook Element with an individual content called "Enduro Score" on my Blog Page after the Post Title H1, see example: https://ride-with-love.bike/gavia-pass-mtb-enduro-trails/
But I want the Hook be part of the H1 Title, so I'm looking for a way to merge Post Title and Hook Element within the H1 Headline. I've tried with Block Element – Page Hero and could rebuild the existing H1 Headline with a dynamic field, but didn't found a way to add PHP oder a Hook Element.
Is it possible to merge Page Title with a Hook Element?
Thank you, Mags
This is the Code of my Hook Element
`<?php
// Get custom field value
$subtitle = get_post_meta( get_the_ID(), 'taq_review_score', true );
// Display $subtitle if not empty
if ( ! empty( $subtitle ) ) {
echo '<p class="custom-subtitle">Enduro Score ' .round(($subtitle/10), 1). '</p>';
}
?>
Hi there,
just to be clear do you want this <p class="custom-subtitle">Enduro Score ' .round(($subtitle/10), 1). '</p> inside the <h1> tag ?
Yes!
Is it that simple to keep the PHP in the Hook, delete the "echo" call in the Hook and include the custom-subtitle in the H1?
Ok so there are no action hooks inside the title.
But we have the generate_get_the_title_parameters filter in GP.
So we can use this PHP Snippet to change that output:
add_filter( 'generate_get_the_title_parameters', function( $params ) {
$subtitle = get_post_meta( get_the_ID(), 'taq_review_score', true );
if ( is_singular() && $subtitle ) {
$params = array(
'before' => sprintf(
'<h1 class="entry-title"%1$s>',
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
),
'after' => sprintf(
'<p class="custom-subtitle">Enduro Enduro Score: %1$s</p></h1>',
round(($subtitle/10), 1)
),
);
}
return $params;
} );
So that snippet:
1. Gets the Custom Field: taq_review_score
2. Checks if were on a singular template and the taq_review_score has content.
3. If so it changes the after parameter to include your HTML and custom field.
That snippet goes in your Child Theme functions file ( or added using the Code Snippet plugin ), not in an Element.
Excellent :-) That's it and works perfect!
Thanks for the great support
Glad to be of help!
One more question: how must the code look like if the enduro score should be on first place in the Headline instead of the last place?
Try this:
add_filter( 'generate_get_the_title_parameters', function( $params ) {
$subtitle = get_post_meta( get_the_ID(), 'taq_review_score', true );
if ( is_singular() && $subtitle ) {
$params = array(
'before' => sprintf(
'<h1 class="entry-title"%1$s><p class="custom-subtitle">Enduro Enduro Score: %2$s</p>',
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '',
round(($subtitle/10), 1)
),
'after' => '</h1>',
);
}
return $params;
} );
❤️
Hi David,
really works great on my single post page, e.g.: https://ride-with-love.bike/gavia-pass-mtb-enduro-trails/
Is it possible to expand the code to all pages where the 'taq_review_score' is available, e.g. on my category overview pages https://ride-with-love.bike/category/uebersicht-enduro-touren/ or on my homepage?
Thanks in advance for your feedback,
Mags
Hi Mags,
Try this:
add_filter( 'generate_get_the_title_parameters', function( $params ) {
$subtitle = get_post_meta( get_the_ID(), 'taq_review_score', true );
if ( is_singular() && $subtitle ) {
$params = array(
'before' => sprintf(
'<h1 class="entry-title"%1$s><p class="custom-subtitle"> %2$s</p>',
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '',
round(($subtitle/10), 1)
),
'after' => '</h1>',
);
}
if ( ! is_singular() && $subtitle ) {
$params = array(
'before' => sprintf(
'<h2 class="entry-title"%2$s><a href="%1$s" rel="bookmark"><p class="custom-subtitle"> %3$s</p>',
esc_url( get_permalink() ),
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '', round(($subtitle/10), 1)
),
'after' => '</a></h2>',
);
}
return $params;
} );
Hello,
the code for displaying my "Enduro Score" in Headline works perfect on my page, e.g. on https://ride-with-love.bike/category/uebersicht-gratis-enduro-touren/
Now I've created a page based on Elements: https://ride-with-love.bike/ride-with-love-der-spezialist-fuer-mtb-enduro-alpencross-mehrtagestouren/ and the "Enduro Score" is not showing within the h3-headline on this page.
I've already tried to modify the php within the functions.php and added an additional "if-loop" for the h3 class on that page:
add_filter( 'generate_get_the_title_parameters', function( $params ) {
$subtitle = get_post_meta( get_the_ID(), 'taq_review_score', true );
if ( is_singular() && $subtitle ) {
$params = array(
'before' => sprintf(
'<h1 class="entry-title"%1$s><div class="headline_enduro-score">%2$s<p class="value">Enduro Score</p></div><span class="headline">',
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '',
round(($subtitle/10), 1)
),
'after' => '</h1>',
);
}
if ( ! is_singular() && $subtitle ) {
$params = array(
'before' => sprintf(
'<h2 class="entry-title"%2$s><a href="%1$s" rel="bookmark"><p class="custom-subtitle">%3$s <span>Enduro Score</span></p>',
esc_url( get_permalink() ),
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '', round(($subtitle/10), 1)
),
'after' => '</a></h2>',
);
}
if ( ! is_singular() && $subtitle ) {
$params = array(
'before' => sprintf(
'<h3 class="wp-container-1 wp-block-post-title"%2$s><p class="custom-subtitle">%3$s <span>Enduro Score</span></p>',
esc_url( get_permalink() ),
'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '', round(($subtitle/10), 1)
),
'after' => '</h3>',
);
}
return $params;
} );
And also created an dedicated Hook only for this page (But I guess this Hook is not necessary, because the existing hook is set to work on all pages). I didn't modified this php Code so far:
<?php
// Get custom field value
$subtitle = get_post_meta( get_the_ID(), 'taq_review_score', true );
// Display $subtitle if not empty
if ( ! empty( $subtitle ) ) {
echo '<p class="custom-subtitle">Enduro Score ' .round(($subtitle/10), 1). '</p>';
}
?>
But the page still doesn't show the "Enduro Score" within the headline. What I am doing wrong?
Thank you for your support,
Mags
Is that using the core query loop block ?
Yes
Yeah the theme filters aren't going to work there, the core blocks and its post title will simply return the_title function.
You would need to use the render_block filter hook:
https://developer.wordpress.org/reference/hooks/render_block/
Which can get complicated, heres a real basic example of how that works:
function db_rerender_post_title_block( $block_content, $block ) {
if ( 'core/post-title' === $block['blockName'] ) {
$content = 'some content before';
$content .= $block_content;
$content .= 'some content after';
return $content;
}
return $block_content;
}
add_filter( 'render_block', 'db_rerender_post_title_block', 10, 2 );
Which would allow you to wrap some stuff around ( before/after ) the H3 Post title. For example:
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 );