Archived topic
Template Tags not working
1 reply · Started by Mark on December 13, 2020
I made a Block Element to use as a page header. Hooked it into generate_after_navigation. It's appearing as it's supposed to but the template tag just shows as text...{{Post_title}}
It's not pulling in the actual title like it did in "the old days".
Is there another way to use the template tags now?
Thanks.
Hi,
{{post_title}} and other template tags only work within the Header Element (not block elements).
That said, you'll need a plugin that displays dynamic text to display within your Block element layout.
Alternatively, you can write your own shortcodes that display dynamic texts.
Example: "A shortcode that displays the page/post title"
function dynamic_title_function( $atts ) {
$atts = shortcode_atts(
array(
'tag' => '',
'style' => '',
), $atts, 'dynamic_title' );
$tag = $atts['tag'];
$style = $atts['style'];
$output = '';
if(!empty($tag)){
$output = sprintf( '<%1$s style="%3$s"> %2$s </%1$s>',
$tag,
esc_html( get_the_title() ),
$style
);
} else {
$ouput = esc_html( get_the_title() );
}
echo $output;
}
add_shortcode( 'dynamic_title', 'dynamic_title_function' );
With this snippet, you can use [dynamic_title tag="h1" style="color:red;"] on your block element to display page title.
Note: I've written this shortcode in a way where if you only want the string, you can just use [dynamic_title] without the attributes.
You can also make this for other things like meta terms.