I want to create a shortcode that pulls value from a custom field of a post and parses that value on Post title, post content.
So if the custom field value is Icecream for Post 1.
On the admin side the post title will be Favorite [geo_name], it will be displayed as Favorite Icecream.
Here is my code so far:
function geo_name_function( $atts ) {
$atts = shortcode_atts( array(
'post_id' => get_the_ID(),
), $atts, 'geo_name' );
return get_post_meta( $atts['post_id'], 'field_name', true );
}
add_shortcode('geo_name', 'geo_name_function');
add_filter( 'the_title', function( $geo_name_function ) {
return do_shortcode( $geo_name_function );
});
I keep having one issue.
If Recent Posts Widget is on one of the posts it displays the same value for all post titles.
For example:
Article 1
[geo name] value = Geo 1
Admin title: [geo_name] title Rendered title: Geo 1 title
Article 2
[geo name] value = Geo 2
Admin title: [geo_name] title Rendered title: Geo 2 title
If I am on an Article 1 page my Recent Post Widget looks like:
Recent Posts
Geo 1 title -> url to article 1
Geo 1 title -> url to article 2
If I am on an Article 2 page my Recent Post Widget looks like:
Recent Posts
Geo 2 title -> url to article 1
Geo 2 title -> url to article 2
My PHP knowledge is very limited, Please assist!