0

I've added a template in my theme, I want to change page title using following code. but its change my menu titles also. I'm unable to fix this issue. Please help.

function get_id_by_slug($page_slug) {
    $page = get_page_by_path($page_slug);
    if ($page) {
        return $page->ID;
    } else {
        return null;
    }
}

$branchPageSlug     = $_GET['slug'];
$branchPageID       = get_id_by_slug($branchPageSlug);

global $branchPageTitle;
$branchPageTitle    = get_the_title($branchPageID);


function cf_ChangePageTitle( $title ) {
    global $branchPageTitle;
    return $branchPageTitle;
}
add_filter( 'the_title', 'cf_ChangePageTitle' );
0

There are a lot of things that can be improved in the code, like the use of global variables, but Im guessing you are learning so I'll answer your question first.

You can try this technique to alter the title only in the page, keeping the menu intact.

Try this tutorial

Basically

Go to Appearance -> Menus and your menu item. Here you will see the changed page title. In order to keep the old menu name, open this menu and type it under the navigation label and click Save

Also consider reading this for the correct use of the global keyboard

  • Dear, I am using template and I want to change title by custom code in my template. – Shahrukh Raza Nov 9 '17 at 14:41
  • You said your code works ? Only the menu needs to use the old name ? – LumberHack Nov 9 '17 at 14:48
0

This is the working example of your code I have introduced some changes like "get_page_by_path(page_path)" requires page path. here you need to take care of all type of pages those who have parents and those who don't. I have tested this code in page.php.

 function get_id_by_slug($page_path) {
    $page = get_page_by_path($page_path);
    if ($page) {
        return $page->ID;
    } else {
        return null;
    }
}
global $post;
$branchPageSlug     = $post->post_name;
$branchPageID       = get_id_by_slug(get_page_uri( $post->ID ));
global $branchPageTitle;
$branchPageTitle    = get_the_title($branchPageID);


function cf_ChangePageTitle( $title ) {
    global $branchPageTitle;
    //you can change your page title here conditionally/unconditionally 
    $branchPageTitle = "your page title";
    return $branchPageTitle;
}
add_filter( 'the_title', 'cf_ChangePageTitle' );
0

You have to update your filter callback like the following. $branchPageTitle will be applied only when the post type is page.

function cf_ChangePageTitle( $title, $post_id ) {
    if ( 'page' === get_post_type( $post_id ) ) {
        global $branchPageTitle;
        return $branchPageTitle;
    }
    return $title;
}
add_filter( 'the_title', 'cf_ChangePageTitle', 10, 2 );
0

the_title filter will affect everything that calls the get_the_title function which can be anywhere especially for such a common functionality. If you want the change to happen only on a specific scope you need to make sure in your filter handling that you are in the right scope.

One way to limit scope is to use functions like is_admin, is_single and their friends to make sure you are in the correct scope in your filter handling function. Another way is to add the filter only after you are sure that you are in the scope, for example if it needs to be run only in the context of a page template, you can add it as part of the template.

Unrelated note: never use global. This code is small enough for people to understand what is it that going on there, but 5 more lines and in a month you will get white hair from trying to understand what is going on and fighting all kinds of bugs. Just don't do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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