3

How to change just wordpress post title but not menu items.

add_filter('the_title', 'wordpress_title');
function wordpress_title(){
  return 'New title';
}

enter image description here

1
  • Use in_the_loop() conditional check.
    – Amit Kosti
    Sep 7, 2012 at 8:00

4 Answers 4

1
add_filter('the_title', 'wordpress_title');
function wordpress_title($title){

    //Return new title if called inside loop
    if ( in_the_loop() )
        return 'New title';

    //Else return regular   
    return $title;

}

Have you tried the in_the_loop() conditional check to return new title only if called inside loop. That means nav menu's will not get affected.

1

If you're using custom nav menus, you can do this entirely without code. Go to Appearance -> Menus and change the "Navigation Label" of each menu item you want to be different.

0
<?php add_filter('the_title', function($title) { return '<b>'. $title. '</b>';}) ?> 
2
  • Menu items ant post name now bold. I need change just post title (<h2>) and not menu items.
    – qq3
    Oct 8, 2011 at 13:49
  • Modify the theme, and not the filter.
    – keatch
    May 10, 2012 at 7:25
0

You need the right set of contditions:

  • Inside the loop
  • The ID of the post matches the id from the URL (a bit complex)
  • ...Other optional conditions

Place this in your plugin or theme:

add_filter( 'the_title', 'change_my_title');
function change_my_title ($title) {
    if ( in_the_loop() && get_the_ID() === url_to_postid(full_url($_SERVER))) {
        $title = $title . " added by plugin";
    }
    return $title;
}

// Function found here: http://stackoverflow.com/a/8891890/358906
function full_url($s) {
    $ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
    $sp = strtolower($s['SERVER_PROTOCOL']);
    $protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
    $port = $s['SERVER_PORT'];
    $port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
    $host = isset($s['HTTP_X_FORWARDED_HOST']) ? $s['HTTP_X_FORWARDED_HOST'] : isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : $s['SERVER_NAME'];
    return $protocol . '://' . $host . $port . $s['REQUEST_URI'];
}

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.