Shortcode To Display Post Custom Field Value in Post Title, Post Content

admin2025-03-11  1

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!

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!

Share Improve this question asked Mar 12, 2021 at 22:31 DiscoverDiscover 331 gold badge1 silver badge6 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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 );
)

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 );

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1741683038a174630.html

最新回复(0)