Skip to content

Instantly share code, notes, and snippets.

@jmcclellan
Last active December 22, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmcclellan/6453407 to your computer and use it in GitHub Desktop.
Save jmcclellan/6453407 to your computer and use it in GitHub Desktop.
Paginated WordPress posts with an option to view as a single post
<?php
/*
----------------------------------------------------------------------------------------------------------
On themes that already have built in support, all you have to do is add <!--nextpage--> via the html
editor on WordPress where you want to add a new page to your post. If your theme doesn't have
intrisic support already built in for this then you can add it with one line:
<?php wp_link_pages(); ?>
References:
http://codex.wordpress.org/Styling_Page-Links
http://www.nathanrice.net/blog/creating-a-blog-page-with-paging/
http://wordpress.stackexchange.com/questions/32119/single-page-view-for-paginated-posts
----------------------------------------------------------------------------------------------------------
/*
// Once you have this set up, you can also add a single page view option with the code below:
/*
* Custom query variable for single-page views on multi-page posts
*
* ------ Add this to your functions.php file -------
*/
function somename_parameter_queryvars( $qvars ) {
$qvars[] = 'all';
return $qvars;
}
add_filter( 'query_vars', 'somename_parameter_queryvars' );
/*
* ------ Replace your the_content() call with lines 29-40 -------
*/
// check to see if there is pagination and if the single page view is loaded or not
global $wp_query;
$no_pagination = false;
if ( isset( $wp_query->query_vars['all'] ) ) {
$no_pagination = $wp_query->query_vars['all'];
}
if( $no_pagination ) {
echo apply_filters( 'the_content', $post->post_content );
$page=$numpages+1;
} else {
the_content('Read the rest of this entry');
}
/*
* ------ Add lines 48-52 where you want to display the link to view as one page -------
*/
// display a link to the single page
global $numpages;
if ( is_singular() && $numpages > 1 ) { ?>
<a href="<?php echo add_query_arg( array( 'all' => '1'), get_permalink() ); ?>" title="single-page view">View on one page</a>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment