0

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;
}
4
  • Hi, it's useful to use more precise language that 'not working' so people can help you debug - what's not working? what actually happens? what did you expect to happen?
    – mozboz
    Jul 7, 2020 at 13:22
  • @mozboz I'm actually trying to add some text in WordPress post title via ACF plugin, I have created the custom field called exclusive and I want it to show before or after the WordPress post title so everywhere it updates dynamically on the posting page, archive page and category page. but it is not showing my custom field before or after the title, I want it as a part of wordpress post title
    – uixpider
    Jul 7, 2020 at 13:27
  • 1
    Sure, so with this code, what's not working? what did you expect to happen? did you try to debug the code by seeing where it did something you didn't expect? For example, are you sure the hook is running? Are you sure that the check for the babysitters post type is working?
    – mozboz
    Jul 7, 2020 at 13:34
  • Can you please share a way or screenshot of the customfield section that you have created in acf custom field? And what is $exclusive->y their because my code is working fine, I have shared the code and screenshots of it in the answer section. Jul 7, 2020 at 14:05

1 Answer 1

0

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!:)

2
  • Not working for me.
    – uixpider
    Jul 7, 2020 at 14:24
  • uixpider, the code you have shared it's working for me, do you working on a local machine or any domain you have tried this? If possible you can share the link and i think there is some other error, because of that it's not working. Can you do one thing, just install the fress wp on local and only use this code to the functions.php file in activated theme, and try it. If this works then there is any proplem in your old setup. Jul 7, 2020 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.