Trying to add some custom text into WordPress Post title via function.php

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

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 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.

Trying to add some custom text into WordPress Post title via function.php

Trying to add some custom text into WordPress Post title via function.php
Please check, If it works for you. also please share the details that i have mentioned.

Thanks!:)


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x