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

How to hook into a pages title to filter it in wordpress

我有一个名为 used-cars 的页面,我正在尝试过滤其标题并将其显示为 H1 header。我有 rewrite 永久链接结构来传递双项,例如 example.com/used-cars/term1/term2,它就像一个魅力

所以在这一点上。我正在尝试过滤页面标题以匹配 URL,但我无法使用此代码

`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;
      }`

但是当我用这个的时候。有效

global $wp_query; echo 'Car : ' . $wp_query->query_vars['serie']; echo '<br />'; echo 'Area : ' . $wp_query->query_vars['area'];

那么如何合并这两个解决方案来过滤此页面标题的标题?

对于正在寻找此解决方案的任何人。这就是我最终解决这个问题的方式。

我连接到 wpseo_title 而不是自 Wordpress 4.4

以来已停产的 wp_title

希望对您有所帮助。