Search code examples
phpwordpressfiltertaxonomy

How to hook into a pages title to filter it in 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?


Solution

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