Casey
Casey

Reputation: 881

Modify WordPress Page Title Outside of Header.php

Is there a way to programmatically assign the page title outside of the header.php file?

In one of my page template files, I would like to dynamically assign the page title based on a value in a custom field. Is this possible?

Upvotes: 0

Views: 1870

Answers (2)

Casey
Casey

Reputation: 881

I was able to use a filter hook to assign a value to wp_title:

function assignPageTitle(){
  return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');

Upvotes: 1

michaelkoss
michaelkoss

Reputation: 516

Here is some code I use in header.php to set the title dynamically. You could use something similar to get your custom field value.

if (is_single() && have_posts()) {
  the_post();
  echo "OKC ThunderCast: ".get_the_title();
  rewind_posts(); //needed so that the call to The Loop on single.php will find the posts
} else {
  bloginfo('name');
}

Upvotes: 0

Related Questions