Hi can anyone help with the below, please?

I want to be able to change the contents of my HTML <title> tag only, so need to write an if / else statement.

I'm getting myself a bit muddled as my PHP isn't great... but basically something along the lines of the below but combined...

<?php if ( ! is_page_template('boatDetails.php') ) { ?>
<title><?php bloginfo('name'); ?><?php wp_title('|'); ?></title>
<?php } ?>
<?php if ( is_page_template('boatDetails.php') ) { ?>
<title>I'm the boat details page</title>
<?php } ?>

Thanks :)

  • For a more precise answer, can we see the contents of boatDetails.php? Aug 20, 2012 at 16:12
  • Hiya - unfortunately it's on client protected IP filtered server as the site is in development. I'm not sure whether it's necessary though is it? I can get the 'I'm the boat details page' title to appear on that page - and the other title tag to render on all other pages, I just want to know how to combine the two statements into an if/else...
    – V Neal
    Aug 20, 2012 at 16:15
  • I think it can be answered generally (see my answer below), but you should always include relevant code - or, as appears is the case here, at least say that you're adding the relevant code from header.php. It just helps us understand/answer the question better. Aug 20, 2012 at 16:19
  • Ok sorry - yes I was attempting to write it in header.php. The below solution does work - thank you - although I may need to add it to my boatDetails.php template itself as the boat info is fed via xml and therefore needs to be dynamically generated from the content pulled in. Sorry - should have mentioned that to begin with.
    – V Neal
    Aug 20, 2012 at 16:31
  • 1
    Can you clarify your question with this information, so that it can be addressed? Please explain what you're doing, where the dynamically generated content is coming from, and what specific data you want to use in the document title. Aug 20, 2012 at 16:38

2 Answers 2


I think you will want to use the wp_title filter. Add the following to functions.php:

function wpse62415_filter_wp_title( $title ) {
    // Return a custom document title for
    // the boat details custom page template
    if ( is_page_template( 'boatDetails.php' ) ) {
        return 'I\'m the boat details page';
    // Otherwise, don't modify the document title
    return $title;
add_filter( 'wp_title', 'wpse62415_filter_wp_title' );

Make no other changes anywhere, including in header.php.


Add this code to your-page-template.php file before get_header() function:

function my_page_title() {
    return 'Your value is '; // add dynamic content to this title (if needed)
add_filter( 'pre_get_document_title', 'my_page_title' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.