Most of the related questions are still unanswered or have very little information. I have a custom theme for my website and use WordPress SEO for titles.

All the article pages work fine, except for the pages which are created by template, i.e. /adopt-a-dog and /dog-breeds. These pages have a list of adoption entries and dog breeds. Adopt-a-dog uses adoption-search.php as a template and adopt-details.php to display individual results. Where as dog-breeds uses breeds.php to display the dog breeds page, as well as individual breeds.

I tried adding the title in header.php using condition is_page_template('adopt-details.php') but that doesn't work. I also tried to modify functions.php but that didn't work either.

It shows the title as set in WordPress SEO, i.e. "Adopt a dog, save a life". Instead, if the adoption result is for puppy Fodo, I want to set the title to something like: "Adoption details for " . pet_name . ", " . pet_city

Is there a way to set the title from a page template, or set the title based on URL?

Code that I have tried:

if ( ( get_permalink() == 'http://animalswecare.com/dog-breeds/' || 
    get_permalink() == 'http://animalswecare.com/cat-breeds/' ) && 
    uri_segment( 2 ) != '0' ) 
    ?><title><?php echo uri_segment( 2 ); ?></title><?php 
} else {
    ?><title><?php wp_title( '|', true, 'right' ); ?></title><?php

Second option that I tried is disabling WP SEO when the page is adopt-detail-dog. On adding this code, the URL shows as title. That is okay if somehow the URL could be cleaned to show as text.

if ( is_page( 589 ) ) { 
    global $wpseo_front;
    remove_action( 'wp_head', array( $wpseo_front, 'head' ), 1 );
} else {
    ?><title><?php wp_title( '|', true, 'right' ); ?></title><?php

This changes the title to show page URL, but doesn't show "hiiiiiiiiiiii" as title.


2 Answers 2


For dog breeds, I added the following code to template pages, overwriting the WP SEO functions:

function assignPageTitle() { // To set page title
    global $resultarray;
    return $resultarray->breed_title;

if ( uri_segment( 2 ) != '0' ) {
    add_filter( 'wpseo_title', 'assignPageTitle' ); // WP SEO function overwritten

For a list of other WP SEO functions, see this.


Add the following code to your functions.php file:

function wpse62415_filter_wp_title( $title ) {
    // Return a custom document title for
    // the my-custom-page custom page template
    if ( is_page_template( 'my-custom-page.php' ) ) {
        return 'I\'m the my-custom-page details page';
    // Otherwise, don't modify the document title
    return $title;
add_filter( 'wp_title', 'wpse62415_filter_wp_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.