admin管理员组

文章数量:1279008

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 :)

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 :)

Share Improve this question edited Aug 20, 2012 at 16:17 Chip Bennett 55.1k8 gold badges90 silver badges170 bronze badges asked Aug 20, 2012 at 16:11 V NealV Neal 2271 gold badge5 silver badges12 bronze badges 5
  • For a more precise answer, can we see the contents of boatDetails.php? – Chip Bennett Commented 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 Commented 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. – Chip Bennett Commented 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 Commented 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. – Chip Bennett Commented Aug 20, 2012 at 16:38
Add a comment  | 

2 Answers 2

Reset to default 5

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' );

本文标签: filtersChanging document title only on a custom page template