Neil Reardon
Neil Reardon

Reputation: 65

How to customise title on Wordpress (running Yoast)

I am trying to customise a title for a Wordpress page. In the page I have some php to obtain data from the database. I would like to output a custom title based on the data and the page id. I have tried this:

add_filter('wpseo_title', 'property_title', 10, 1);
function property_title() {
global $post;
$postid = $post->ID;
if ($postid == '72616') {
   $title['title'] = "Property number $propertyid";
   return $title;
}
}

How can I check the postid and if it is the right page, output the data from the sql query?

Many thanks,

Neil.

Upvotes: 0

Views: 102

Answers (1)

Kevin Greene
Kevin Greene

Reputation: 830

The title is passed in for that filter. Make sure you return title outside of the if statement.

add_filter('wpseo_title', 'property_title', 10, 1);

function property_title($title) {
   global $post;

   $propertyid = get_query_var('reference');

   $postid = $post->ID;
   if ($postid == '72616') {
      $title = "Property number $propertyid";
   }
   return $title;
}

add_filter( 'query_vars', 'add_property_query_vars' );

function add_property_query_vars( $query_vars ) {
    $query_vars[] = 'reference';

    return $query_vars;
}

Upvotes: 1

Related Questions