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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)