0

please I need help with this code

add_action( 'the_title', 'adddd', 10, 2 );
function adddd( $title, $post_id ) 
{
    if( has_category( 30, $post_id ) ) {
        $title = 'Prefix ' . $title;
    }

    return $title;
}

Changing the post title of a specific category.

The code above is working fine displaying the prefix but am getting error when I tried adding a word or two after the post title.

Please any fix for this? Thanks.

1
  • 1
    please post the code which is not working
    – Michael
    Aug 15, 2017 at 23:58

2 Answers 2

1

You should use filter not action:

function adddd( $title, $post_id ) {
    if( in_category( 30 ) ) {
        $title = 'Prefix - ' . $title . ' - xxx';
    }
    return $title;
}
add_filter( 'the_title', 'adddd', 10, 2 );
2
  • Thanks very much. My own code is working fine. What I wanted was to add additional word after the post title. Can you help me with the full code? Thanks
    – Swaglord07
    Aug 16, 2017 at 4:12
  • Just add this word after $title. I'll edit my answer. Aug 16, 2017 at 5:11
0

It returned an error. So I just edited mine adding . ' xxx' after the title

add_action( 'the_title', 'adddd', 10, 2 );
function adddd( $title, $post_id ) 
{
    if( has_category( 30, $post_id ) ) {
        $title = 'Prefix  ' . $title . '  xxx';
    }

    return $title;
}

It's working as it should now. Thanks very much for your help.

Your Answer

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

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