0

I have a small issue:

The search results page returns perfectly my crafted title, but the problem i am facing is that the title changes on paged results. If results are all on page then its fine, if it's paged with results occurring on the 2nd page and more then the title changes to the first post of the paged result.

Does the above make any sense?

Ok, so here is the entire title code I am using.

if( get_post_type()=='page' || is_single() ){
    echo '<h1 class="h2">'. $post->post_title .'</h1>';
} elseif (is_category()){
    echo '<h1 class="h2">Category:'. wp_title( '', false ).'</h1>';
} elseif (is_tag()){
    echo '<h1 class="h2">Tag:'. wp_title( '', false ).'</h1>';
} elseif (is_author()){
    echo '<h1 class=h2">Author:'. wp_title( '', false ).'</h1>';
} elseif (is_search()){
    echo '<h1 class="h2">'.$wp_query->found_posts.' Results Found For: '.get_search_query( false ).'</h1>';                                 
} else {
    echo '<h1 class="h2">'. wp_title( '', false ) .'</h1>';
}       

1 Answer 1

1

The problem is here:

if( get_post_type()=='page' || is_single() ){
    echo '<h1 class="h2">'. $post->post_title .'</h1>';
}

get_post_type() is using the global $post, which will contain the first post in your result set. If this is a page, then your other conditions will never be tested and you'll never reach the is_search() test.

Use is_page() instead:

if( is_page() || is_single() ){
    echo '<h1 class="h2">'. wp_title( '', false ) .'</h1>';
}

or, use is_singular(), which will be true for page, post, or attachment, or any singular post type:

if( is_singular() ){
    echo '<h1 class="h2">'. wp_title( '', false ) .'</h1>';
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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