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!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
the_title filter takes two parameters, one of them is the ID of the post for which it was called. Something like that should work.
add_filter( 'the_title', 'se385007_title_filter', 20, 2 );
function se385007_title_filter( $title, $post_id )
{
$new_title = str_replace( "[geo_name]", "[geo_name post_id=$post_id]", $title );
return do_shortcode( $new_title );
)
Method 2
I believe that’s expected. get_the_ID() returns post_ID from main query, and in a single page will return post_ID for that page. The way you filter post title affects all post title that’ll be rendered in a page regardless of their actual post_ID. Since the problem is in a widget, you need to get the ID from its query (not main one).
I’d try to use the following instead (untested):
function geo_name_function( $atts ) {
$atts = shortcode_atts( array(
'post_id' => get_queried_object_id(),
), $atts, 'geo_name' );
return get_post_meta( $atts['post_id'], 'field_name', true );
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0