Вопрос

I need to add a custom text in wordpress Post title dynamically which I'm trying to add via this code in function.php

Following code is not working

add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
    if('babysitters' == get_post_type($id)){
        $exclusive = get_field('exclusive', $id);   // pass the id into get_field
        $title = $title .', ' .$exclusive->y;
    }
    return $title;
}
Это было полезно?

Решение

The first thing that i am not getting is $exclusive->y ? can you please share custom field settings that you have created?

And the second thing that i have found, you must have to put global $post; in the first line of the function :

add_filter( 'the_title', 'wpb_new_title', 10, 2 );
function wpb_new_title( $title, $id ) {
   global $post;
   if('post' == get_post_type($id)){
        $exclusive = get_field('exclusive', $id);   // pass the id into get_field
        $title = $title .', ' . $exclusive;
    }
    return $title;
}

I have added a title on the default post. see the screenshot of the settings and the view page. in viewpage my post name is Hello World and i am adding ",test additional title" from the filter hook.

settings image

output Image Please check, If it works for you. also please share the details that i have mentioned.

Thanks!:)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с wordpress.stackexchange
scroll top