問題描述

我已經使用<! - nextpage -> 程式碼將我的帖子內容分成多個頁面。我想給我的分頁連結自己的標題,而不是常規的 1,2,3 。我該怎麼做?原因在這個檔案 https://codex.wordpress.org/Styling_Page-Links 它只提到新增字尾或字首的方法。我只想給每個分頁號自己的自定義標題

最佳解決方案

以下是支援表單分頁標題的方法:

<!--nextpage(.*?)?-->

以核心方式支援<!--more(.*?)?-->

以下是一個例子:

<!--nextpage Planets -->
Let's talk about the Planets
<!--nextpage Mercury -->
Exotic Mercury
<!--nextpage Venus-->
Beautiful Venus
<!--nextpage Earth -->
Our Blue Earth
<!--nextpage Mars -->
The Red Planet

其輸出類似於:

這是在二十六主題測試,我不得不調整填充和寬度一點點:

.page-links a, .page-links > span {
    width:   auto;
    padding: 0 5px;
}

演示外掛

這是一個演示外掛,它使用 content_paginationwp_link_pages_linkpre_handle_404wp_link_pages_args 過濾器來支援下一頁標記 (PHP 5.4+) 的擴充套件:

<?php
/**
 * Plugin Name: Content Pagination Titles
 * Description: Support for &lt;!--nextpage(.*?)?--&gt; in the post content
 * Version:     1.0.1
 * Plugin URI:  http://wordpress.stackexchange.com/a/227022/26350
 */

namespace WPSEQuestion202709;

add_action( 'init', function()
{
    $main = new Main;
    $main->init();
} );

class Main
{
    private $pagination_titles;

    public function init()
    {
        add_filter( 'pre_handle_404',       [ $this, 'pre_handle_404' ],        10, 2       );
        add_filter( 'content_pagination',   [ $this, 'content_pagination' ],    -1, 2       );
        add_filter( 'wp_link_pages_link',   [ $this, 'wp_link_pages_link' ],    10, 2       );
        add_filter( 'wp_link_pages_args',   [ $this, 'wp_link_pages_args' ],    PHP_INT_MAX );
    }

    public function content_pagination( $pages, $post )
    {
        // Empty content pagination titles for each run
        $this->pagination_titles = [];

        // Nothing to do if the post content doesn't contain pagination titles
        if( false === stripos( $post->post_content, '<!--nextpage' ) )
            return $pages;

        // Collect pagination titles
        preg_match_all( '/<!--nextpage(.*?)?-->/i', $post->post_content, $matches );
        if( isset( $matches[1] ) )
            $this->pagination_titles = $matches[1];

        // Override $pages according to our new extended nextpage support
        $pages = preg_split( '/<!--nextpage(.*?)?-->/i', $post->post_content );

        // nextpage marker at the top
        if( isset( $pages[0] ) && '' == trim( $pages[0] ) )
        {
            // remove the empty page
            array_shift( $pages );
        }
        // nextpage marker not at the top
        else
        {
            // add the first numeric pagination title
            array_unshift( $this->pagination_titles, '1' );
        }
        return $pages;
    }

    public function wp_link_pages_link( $link, $i )
    {
        if( ! empty( $this->pagination_titles ) )
        {
            $from  = '{{TITLE}}';
            $to    = ! empty( $this->pagination_titles[$i-1] ) ? $this->pagination_titles[$i-1] : $i;
            $link  = str_replace( $from, $to, $link );
        }

        return $link;
    }

    public function wp_link_pages_args( $params )
    {
        if( ! empty( $this->pagination_titles ) )
        {
            $params['next_or_number'] = 'number';
            $params['pagelink'] = str_replace( '%', '{{TITLE}}', $params['pagelink'] );
        }
        return $params;
    }

    /**
     * Based on the nextpage check in WP::handle_404()
     */
    public function pre_handle_404( $bool, WP_Query $q )
    {
        global $wp;

        if( $q->posts && is_singular() )
        {
            if ( $q->post instanceof WP_Post )
                $p = clone $q->post;

            // check for paged content that exceeds the max number of pages
            $next = '<!--nextpage';
            if (   $p
                 && false !== stripos( $p->post_content, $next )
                 && ! empty( $wp->query_vars['page'] )
            ) {
                $page = trim( $wp->query_vars['page'], '/' );
                $success = (int) $page <= ( substr_count( $p->post_content, $next ) + 1 );

                if ( $success )
                {
                    status_header( 200 );
                    $bool = true;
                }
            }
        }
        return $bool;
    }

} // end class

安裝:建立/wp-content/plugins/content-pagination-titles/content-pagination-titles.php 檔案並啟用外掛。在測試任何外掛之前,總是很好的備份。

如果頂部 nextpage 標記丟失,則第一個分頁標題是數字。

另外,如果一個內容分頁標題缺少,即<!--nextpage-->,那麼它將是數字,正如預期的那樣。

我首先忘記了 WP 類中的下一頁錯誤,如果我們透過 content_pagination 過濾器修改頁數,則會顯示該錯誤。這是最近由 @PieterGoosen 在 #35562 報道的。

我們嘗試透過 pre_handle_404 過濾器回撥在 WP 過濾器回撥中克服這一點,基於 WP 類檢查 here,我們檢查<!--nextpage 而不是<!--nextpage-->

Tests

這裡有一些進一步的測試:

測試#1

<!--nextpage-->
Let's talk about the Planets
<!--nextpage-->
Exotic Mercury
<!--nextpage-->
Beautiful Venus
<!--nextpage-->
Our Blue Earth
<!--nextpage-->
The Red Planet

輸出 1 選擇:

如預期。

測試#2

Let's talk about the Planets
<!--nextpage-->
Exotic Mercury
<!--nextpage-->
Beautiful Venus
<!--nextpage-->
Our Blue Earth
<!--nextpage-->
The Red Planet

輸出 5 選擇:

如預期。

測試#3

<!--nextpage-->
Let's talk about the Planets
<!--nextpage Mercury-->
Exotic Mercury
<!--nextpage-->
Beautiful Venus
<!--nextpage Earth -->
Our Blue Earth
<!--nextpage Mars -->
The Red Planet

輸出 3 選擇:

如預期。

測試#4

Let's talk about the Planets
<!--nextpage Mercury-->
Exotic Mercury
<!--nextpage Venus-->
Beautiful Venus
<!--nextpage Earth -->
Our Blue Earth
<!--nextpage Mars -->
The Red Planet

輸出選擇地球:

如預期。

Alternatives

另一種方法是修改它以支援新增標題:

<!--pt Earth-->

支援所有分頁標題 (pts) 的單一評論也可能很方便:

<!--pts Planets|Mercury|Venus|Earth|Mars -->

或者可能透過自定義欄位?

次佳解決方案

您可以使用過濾器 wp_link_pages_link

首先傳遞我們的自定義字串佔位符 (這可以是任何你喜歡的,除了包含% 的字串,現在我正在使用 #custom_title#) 。

wp_link_pages( array( 'pagelink' => '#custom_title#' ) );

然後在 functions.php 中新增我們的過濾器。在回撥函式中,建立一個標題陣列,然後檢查當前頁碼,並用對應於當前頁碼的值替換 #custom_title#

例:-

add_filter('wp_link_pages_link', 'wp_link_pages_link_custom_title', 10, 2);
/**
 * Replace placeholder with custom titles
 * @param string $link Page link HTML
 * @param int $i Current page number
 * @return string $link Page link HTML
 */
function wp_link_pages_link_custom_title($link, $i) {

    //Define array of custom titles
    $custom_titles = array(
        __('Custom title A', 'text-domain'),
        __('Custom title B', 'text-domain'),
        __('Custom title C', 'text-domain'),
    );

    //Default title when title is not set in array
    $default_title = __('Page', 'text-domain') . ' ' . $i;

    $i--; //Decrease the value by 1 because our array start with 0

    if (isset($custom_titles[$i])) { //Check if title exist in array if yes then replace it
        $link = str_replace('#custom_title#', $custom_titles[$i], $link);
    } else { //Replace with default title
        $link = str_replace('#custom_title#', $default_title, $link);
    }

    return $link;
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。