$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Shortcode To Display Post Custom Field Value in Post Title, Post Content|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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

}
Post a comment

comment list (0)

  1. No comments so far