I would appreciate your help in this regard. Thank You F
You can add this code to the functions.php file of your theme. And from your backend > Questions> Edit Question, you can use %%question_tag%%
add_action( 'wpseo_register_extra_replacements', function() {
//error_log('Yoast custom variable registered: %%question_tag%%');
wpseo_register_var_replacement(
'%%question_tag%%',
function() {
global$post;
if ( ! $post ) {
return'';
}
$tags = get_the_terms( $post->ID, 'emqa-question_tag' );
if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {
$names = implode( ', ', wp_list_pluck( $tags, 'name' ) );
// error_log('Yoast %%question_tag%% = ' . $names);
return$names;
}
return'';
},
'advanced',
__( 'Show tag of emo questalk pro (taxonomy emqa-question_tag)', 'emo-questalk-pro' )
);
});
The code working for Yoast SEO plugin.
The Yoast hook has run.
The replacement value works when rendering on the frontend.
But in the backend preview (Yoast snippet editor), it still shows %%question_tag%%
.
Explanation
This is the default behavior of Yoast:
Many custom variables created with wpseo_register_var_replacement()
only work on the frontend (in <title>
and <meta>
when rendered on the site).
In the backend preview, Yoast only renders its built-in variables (%%title%%
, %%excerpt%%
, %%category%%
, …). Custom variables will almost always appear in raw form (%%myvar%%
).
Please login or Register to submit your answer