How to add body class to paginated pages when using nextpage quicktag in WordPress

In this article I share the code to add a custom class name to the list of body classes for paginated pages i.e., static Pages and single Posts that are divided into several pages using the WordPress

<!--nextpage-->
view raw gistfile2.txt hosted with ❤ by GitHub

quicktag.

Adding the following in child theme’s functions.php will add “paged” body class to paginated pages except the first one.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php
//* Do NOT include the opening php tag
 
//* Add custom body class to the head
add_filter( 'body_class', 'sk_body_class' );
function sk_body_class( $classes ) {
 
// If the current page is not a static Page or single Post, move on
if ( ! is_page() && ! is_single() ) {
return;
}
 
// Assign a custom class of "paged" to body element on 2nd paginated page onwards
if ( (int) get_query_var( 'page' ) > 0 ) {
$classes[] = 'paged';
}
 
return $classes;
 
}
view raw functions.php hosted with ❤ by GitHub

2014-08-30_00-45-59

When is this useful? Let’s say you want to show the entry title to be visible on first page, but not when on second, third etc. paginated pages.

Then we simply add this CSS:

1 2 3
.paged .entry-header {
display: none;
}
view raw style.css hosted with ❤ by GitHub

If you are using Genesis (and you should, it’s an awesome WordPress framework), the same cam be done w/o adding a body class like so:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
//* Do NOT include the opening php tag
 
add_action( 'genesis_before_entry', 'remove_title_paged_pages' );
function remove_title_paged_pages() {
 
// If the current page is not a static Page or single Post, move on
if ( ! is_page() && ! is_single() ) {
return;
}
 
// For 2nd paginated page onwards
if ( (int) get_query_var( 'page' ) > 0 ) {
//* Remove the entry title
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
}
 
}
view raw gistfile1.php hosted with ❤ by GitHub

Thanks to Gary Jones for the if conditional.