0

So I've optimized one of my archive page title to this

       add_filter('wpseo_title', 'new_filter_title');

      function new_filter_title($title)

      {
        if ( is_page('1637') && $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;

          $title .= ' on ' . get_bloginfo('name');



          return $title;
        }

        return $title;
      }

and this works perfectly. So what I'm trying to do now is echo the same title in the page before the loop. how do I this?

I've already tried this solution

echo get_post_meta(get_the_ID(), '_yoast_wpseo_title', true);

That didn't work. it instead returned variables %%title%% ........%%sitename%%

  • i help you try this code – vikrant zilpe Dec 8 '18 at 9:34
  • $titles = get_option( 'wpseo_titles' ); $title = $titles['title-ptarchive-POST_TYPE']; echo apply_filters( 'the_title', $title ); – vikrant zilpe Dec 8 '18 at 9:34
  • To display all the wpseo_title variables, you can use: printf( '<pre>%s</pre>', print_r( get_option( 'wpseo_titles' ), 1 ) ); – vikrant zilpe Dec 8 '18 at 9:35
  • function yoastVariableToTitle($post_id) { $yoast_title = get_post_meta($post_id, '_yoast_wpseo_title', true); $title = strstr($yoast_title, '%%', true); if (empty($title)) { $title = get_the_title($post_id); } return $title; } – vikrant zilpe Dec 8 '18 at 9:36
  • Thanks man! I tried your code. The first one still displays variables like this %%pt_plural%% Archive %%page%% %%sep%% %%sitename%% . the second one does exactly as it should, it displays all wpseo titles, it helped me alot for something else. the third one does not show anything at all. – joe city Dec 8 '18 at 10:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.