4

I am working on a blog and most of my posts are so long they require pagination. See one of my posts. This post has a lot of pages.

The only problem is that I can't find a way to show a table of contents that will list all the headers consistently across multiple pages.

I've demarcated the single post into different pages with the tag: <!--nextpage-->.

I'm currently using the "Table of Contents Plus (TOC+)" plugin and it works pretty well in single pages, but fails when the page is paginated using <!--nextpage-->.

I am actually hoping to find assistance in writing a plugin that can do the trick.

EDITED - PROPOSED SOLUTION/HACK

In a bid to solve this problem, I have mapped the following steps to resolve this issue:

  1. Check if the page is paginated - wordpress WordPress has a global variable, $multipage that will denote whether or not a post is paginated.

  2. if the post is paginated, then the table of content code (to be written or modified) will check for headers across each page.

  3. Headers found across each page will be stored in an array and will be used to display the TOC

  4. Also, for Paginated single Post the code will be set to display the same TOC consistently across every page as opposed to displaying different TOCs per page

THE CODE

<?php
/**
 * Determines whether or not the current post is a paginated post.
 * @return   boolean    True if the post is paginated; false, otherwise.
 */
function acme_is_paginated_post() {
    global $multipage;
    return 0 !== $multipage; 
} 
?>

If post is paginated, the following checks will be made in the single.php file:

<?php if ( acme_is_paginated_post() ) { ?>
<div class="post-pagination-toc">
        <!-- paginated post TOC  display that checks headers across different poages and the displays all headers in as TOC's content uniformly across all paginated pages -->
</div>
<?php else { ?>
<div class="normal-singlepage-toc">
    <!-- normal TOC display for single non-paginated post -->
</div>
<?php } ?>

In fact, I reduced the above code into a simple function TOC_new()

<?php 
function TOC_new () {
global $numpages;
if ( is_singular() && $numpages > 1 ) {
    // This is a single post
    // and has more than one page;
    // Implement a General TOC to be displayed consistently multiple Pages and return values
} else {
    // This is a single post
    // and has only one page;
    // implement TOC code for single pages.
}
?>
  • 1
    I find this question pretty interesting. However, asking to recommend a product (plugin) is off-topic in this site and your question already got 3 close votes. I removed the plugin recommendation part, now your question is in-topic, but I think you can get better answers if you show some research effort and/or show us your attempts to code that thing... – gmazzap Mar 30 '15 at 20:04
  • im really new to coding... but i edited my question to show the steps to a possible solution... but i'm not sure on how to implement. since i already had already have the TOC+ plugin, Is it ok to modify the code with a hack to detect paginated post and uniformly display TOC for all Pages across all pages? – Tamara Apr 1 '15 at 7:00
  • 1
    Please correct your mark up, it is really difficult to read. No idea why you rolled back my changes after I have corrected the mark up for you :-) – Pieter Goosen Apr 1 '15 at 10:34
  • @PieterGoosen thank you for correcting the markup appearance. im sorry, im new to wordpress.stackexchange.com; Please, how do you suggest i go about this problem? i have searched a lot of forum and i have not found a single resolution to the General TOC Across single Paginated post. – Tamara Apr 1 '15 at 16:10
0

Just inspect the current post content for <!--nextpage-->:

function wpse_check_multi_page()
{
    $num_pages    = substr_count(
        $GLOBALS['post']->post_content,
        '<!--nextpage-->'
    ) + 1;
    $current_page = get_query_var( 'page' );
    return array ( $num_pages, $current_page );
}

On page 2 of 3 that returns:

Array
(
    [0] => 3
    [1] => 2
)

On an unpaged post it returns:

Array
(
    [0] => 1
    [1] => 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.