网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

如何挂钩页面标题以在wordpress中过滤它

GG网络技术分享 2025-03-18 16:12 10


问题描述:

I have a page called used-cars and I\'m trying filter its title and display it as an H1 header. I have rewrite the permalink structure to pass double terms e.g example.com/used-cars/term1/term2 and it works like a charm

So at this point. I\'m trying to filter the page title to match the URLs, but I just can\'t get it to work with this code

`add_filter( \'wp_title\', \'new_listing_title\', 10, 1 );

      function new_listing_title($title)

{

if ( is_page(\'used-cars\') && $id = get_queried_object_id() )

{

$locations = get_query_var(\'area\');

$location = get_term_by(\'slug\', $locations, \'area\');

$models = get_query_var(\'serie\');

$model = get_term_by(\'slug\', $models, \'serie\');

$title = \'\';

if($model && $model) $title .= $model->name . \' Used\';

else $title .= \'Used\';

$title .= \' Cars For Sale\';

if($location && $location) $title .= \' In \' . $location->name;

return $title;

}

return $title;

}`

However when I use this. it works

global $wp_query;

echo \'Car : \' . $wp_query->query_vars[\'serie\'];

echo \'<br />\';

echo \'Area : \' . $wp_query->query_vars[\'area\'];

So how can I Incorporate these two solutions to filter the title of this page\'s title?

网友观点:

For anybody in search for this solution. This is how I finally resolved this problem.

I hooked into wpseo_title instead of wp_title which has been discontinued since Wordpress 4.4

Hope this helps.

标签:

提交需求或反馈

Demand feedback