0

When paginating a blog archive, pages 2+ of a post archive are currently just displaying the single_post_title and not also the page number in the <title> tag.

Is there a straightforward way to display the page number when using a custom page title function in a plugin? Using something like get_query_var wont work because this function is outside the loop (correct?)

My function:

function create_page_title() {
            if ( is_front_page() )  { 
                $page_title = bloginfo('name');
                $page_title .= print(' | Company Name'); 
            } 
            elseif ( is_home() ) {
                $page_title = single_post_title();
                $page_title .= print(' | ');
                $page_title .= print(' '); 
                $page_title .= bloginfo('name');
                $page_title .= print(' | Company Name'); 
            }
            elseif (is_page() ) { ...
            }
            return $page_title;
}

I know I need to put something after $page_title = single_post_title();, but just don't know what...

  • there is some related code in functions.php of the default themeTwenty Twelve – Michael Feb 1 '17 at 4:05
0

You can use this inside your is_page() conditional :

elseif ( is_page() ) { 
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 echo $page_title . ' ' . $paged . ' of '.$wp_query->max_num_pages;
}

The other way to achieve the same thing. The conditional statement serves to avoid adding a 0 besides the title at the first page :

elseif ( is_page() ) { 
   global $paged;
    if ( $paged != 0 ) {
      $custom_title = $page_title . ' Page Number :  ' . $paged;
      echo $custom_title;
    } else {
      echo $page_title;
    }
 }
  • Use return instead of echo if you want a return. – Gonzoarte Feb 3 '17 at 15:56

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.