最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pagination - Implementing a general Table of Content across single paginated post pages

programmeradmin1浏览0评论

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.
}
?>

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.
}
?>
Share Improve this question edited Apr 3, 2015 at 6:29 Tamara asked Mar 30, 2015 at 15:51 TamaraTamara 3021 silver badge6 bronze badges 4
  • 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 Commented Mar 30, 2015 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 Commented Apr 1, 2015 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 Commented Apr 1, 2015 at 10:34
  • @PieterGoosen thank you for correcting the markup appearance. im sorry, im new to wordpress.stackexchange; 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 Commented Apr 1, 2015 at 16:10
Add a comment  | 

1 Answer 1

Reset to default 1

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
)
发布评论

评论列表(0)

  1. 暂无评论