dongxiaoxing3058
2018-12-02 20:59 阅读 120
已采纳

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

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongzhen6554 dongzhen6554 2018-12-08 09:00

    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.

    点赞 评论 复制链接分享

相关推荐