【问题标题】:How to hook into a pages title to filter it in wordpress如何挂钩到页面标题以在 wordpress 中过滤它
【发布时间】:2019-05-04 04:54:34
【问题描述】:

我有一个名为 used-cars 的页面,我正在尝试过滤其标题并将其显示为 H1 标题。我有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'];

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

【问题讨论】:

    标签: php wordpress filter taxonomy


    【解决方案1】:

    适用于寻找此解决方案的任何人。这就是我最终解决这个问题的方法。

    我使用 wpseo_title 而不是 wp_title,后者自 Wordpress 4.4 起就已停用

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 2011-06-02
      • 2019-08-22
      • 1970-01-01
      • 2021-10-01
      相关资源
      最近更新 更多