最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Changing title using filter not working with argument

programmeradmin2浏览0评论

I'm trying to change my custom page's title and I'm able to do this with this function:

add_filter('the_title','change_title');
function change_title($data){
    global $post;
    return 'Page ID ' . $post->ID;
}

and I'm calling the filter:
apply_filters('the_title', $response->data->name); This works fine but when I try to use the $data the page title returning to default name. I mean if change the filter to this:

add_filter('the_title','change_title');
function change_title($data){
    global $post;
    return 'Page ID ' . $data;
}

the title returning to default title. Why I can't set a dynamic title? Also I'm pretty sure that I'm passing a string. (BTW I'm using Hestia theme)

I'm trying to change my custom page's title and I'm able to do this with this function:

add_filter('the_title','change_title');
function change_title($data){
    global $post;
    return 'Page ID ' . $post->ID;
}

and I'm calling the filter:
apply_filters('the_title', $response->data->name); This works fine but when I try to use the $data the page title returning to default name. I mean if change the filter to this:

add_filter('the_title','change_title');
function change_title($data){
    global $post;
    return 'Page ID ' . $data;
}

the title returning to default title. Why I can't set a dynamic title? Also I'm pretty sure that I'm passing a string. (BTW I'm using Hestia theme)

Share Improve this question asked Sep 18, 2019 at 15:32 MustafaMustafa 1
Add a comment  | 

1 Answer 1

Reset to default 0

Maybe you can add a higher priority to your filter as the 3rd parameter of the hook:

/**
 * Modify the data
 * 
 * @param String $data
 *
 * @return String $data
 */
function change_title( $data ) {

    global $post;

    return 'Page ID ' . $data;

}
add_filter( 'the_title', 'change_title', 15 );

The default priority is 10 so it could be that a later filter is overwriting what you have.

发布评论

评论列表(0)

  1. 暂无评论