WP-PageNavi

外掛說明

想要將佈景主題中的舊式 [← 較舊的文章 | 較新的文章 →] 連結取代為頁面連結嗎?

這個外掛提供了可產生精美分頁連結的 wp_pagenavi() 範本標籤。

使用方式

針對使用中的佈景主題,必須找到呼叫 next_posts_link()previous_posts_link() 函式的程式碼,並將他們取代為外掛指定的程式碼。

在 Twentyten 佈景主題中,程式碼看起來大致如下:

<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>

請將上述兩行程式碼取代為以下程式碼:

<?php wp_pagenavi(); ?>

針對多頁分頁連結,請搜尋類似以下的程式碼:

<?php wp_link_pages( ... ); ?>

並取代為下列程式碼:

<?php wp_pagenavi( array( 'type' => 'multipart' ) ); ?>

前往 WordPress 管理後台→[設定]→[PageNavi] 進行設定。

變更 CSS

如需變更 WP-PageNavi 的 CSS 樣式,請從外掛目錄複製 pagenavi-css.css 至使用中的佈景主題的目錄中,然後修改佈景主題目錄中的副本檔案。使用這個方式,便不會在這個外掛更新時遺失之前進行的變更。

另外,網站管理員可以在外掛設定頁取消核取 [使用 pagenavi-css.css],並直接在使用中的佈景主題的 style.css 檔案中加入相關樣式。

變更類別名稱

以下列出的篩選器,均可用於變更指派給頁面導覽元素的預設類別名稱。

篩選器

  • wp_pagenavi_class_pages
  • wp_pagenavi_class_first
  • wp_pagenavi_class_previouspostslink
  • wp_pagenavi_class_extend
  • wp_pagenavi_class_smaller
  • wp_pagenavi_class_page
  • wp_pagenavi_class_current
  • wp_pagenavi_class_larger
  • wp_pagenavi_class_nextpostslink
  • wp_pagenavi_class_last

篩選器使用方式

// Simple Usage - 1 callback per filter
add_filter('wp_pagenavi_class_previouspostslink', 'theme_pagination_previouspostslink_class');
add_filter('wp_pagenavi_class_nextpostslink', 'theme_pagination_nextpostslink_class');
add_filter('wp_pagenavi_class_page', 'theme_pagination_page_class');

function theme_pagination_previouspostslink_class($class_name) {
  return 'pagination__control-link pagination__control-link--previous';
}

function theme_pagination_nextpostslink_class($class_name) {
  return 'pagination__control-link pagination__control-link--next';
}

function theme_pagination_page_class($class_name) {
  return 'pagination__current-page';
}


// More Concise Usage - 1 callback for all filters
add_filter('wp_pagenavi_class_previouspostslink', 'theme_pagination_class');
add_filter('wp_pagenavi_class_nextpostslink', 'theme_pagination_class');
add_filter('wp_pagenavi_class_page', 'theme_pagination_class');

function theme_pagination_class($class_name) {
  switch($class_name) {
    case 'previouspostslink':
      $class_name = 'pagination__control-link pagination__control-link--previous';
      break;
    case 'nextpostslink':
      $class_name = 'pagination__control-link pagination__control-link--next';
      break;
    case 'page':
      $class_name = 'pagination__current'
      break;
  }
  return $class_name;
}

開發工作

致謝

贊助

開發者用了許多時間建立外掛,並加以更新、維護及進行技術支援,如果喜歡這個外掛並贊助幾美元給開發者,開發者會非常感激。沒有贊助亦無妨,這個外掛可供網站隨時使用,不須有任何負擔。

螢幕擷圖

  • 套用自訂樣式的頁面導覽
  • 管理後台的外掛設定頁面

常見問題集

外掛啟用錯誤:「Parse error: syntax error, unexpected…」

請確認網站主機是否執行 PHP 5。最簡單的確認方式是在 wp-config.php 加入以下程式碼 (加在檔案開頭 <?php 標籤後方):

var_dump(PHP_VERSION);

前往第 2 頁後,發現與第 1 頁相同的文章!

這是因為使用 query_posts() 的方式有誤。請參考〈使用 query_posts() 的正確方式〉一文。

WP-PageNavi 能執行於次要的 WP_Query 執行實體嗎?

可以,請參考這份教學文件

如何不套用外掛設定頁面中的設定?

如果網站使用了多語系外掛,網站便不太需要套用外掛設定頁中的相關本地化字串值。

請在對應的檔案中加入類似下方的程式碼。

<?php wp_pagenavi( array( 'options' => PageNavi_Core::$options->get_defaults() ) ); ?>

使用者評論

2023 年 8 月 17 日
Simples e totalmente funcional. Resolvendo o problema de paginação com extrema facilidade. Parabéns aos devs.
2022 年 11 月 22 日
I am using this plugin + Divi. It works just fine, but there is a bug with the slugs of the pages and the post types. If you have, for example, a post type with the slug "speakers" and your page is also call "speakers", both slugs will be the same and this is causing the plugin to break. Most people on the internet are looking for this error as the page/2 error. Be sure the slugs does not match and it will work just fine.
2022 年 8 月 17 日
A simple and excellent plugin. The display of the blog list becomes much smarter, so it is very useful. Currently, I've been using in combination with "Styles For WP Pagenavi Addon".
2022 年 6 月 24 日
Pure code and so simple to customize. The simplest plugin I ever installed. A couple of minutes to get it working. Couldn't be happier 🙂 I only added this snippet to replace my Woocommerce pagination: remove_action('woocommerce_pagination', 'woocommerce_pagination', 10); function woocommerce_pagination() { wp_pagenavi(); } add_action( 'woocommerce_pagination', 'woocommerce_pagination', 10);
2022 年 5 月 9 日
Thanks for that plugins I use till long years to add a navigation bar box inside my blog including more than 2000 entries 😉
閱讀全部 146 則使用者評論

參與者及開發者

以下人員參與了開源軟體〈WP-PageNavi〉的開發相關工作。

參與者

〈WP-PageNavi〉外掛目前已有 53 個本地化語言版本。 感謝全部譯者為這個外掛做出的貢獻。

將〈WP-PageNavi〉外掛本地化為台灣繁體中文版

對開發相關資訊感興趣?

任何人均可瀏覽程式碼、查看 SVN 存放庫,或透過 RSS 訂閱開發記錄

變更記錄

2.94.1

  • FIXED: PHP 8.2 warnings

2.94.0

  • NEW: Add args param on wp_pagenavi filter. Props @asadowski10
  • NEW: Improve accessibility of nav links. Props @carlabobak

2.93.4

  • FIXED: Update SCB Framework To Support PHP 8

2.93.3

  • FIXED: Update SCB Framework To Remove contextual_help

2.93.2

  • NEW: Bumped to WordPress 5.4
  • FIXED: Ensure Action Links is always an array

2.93.1

  • FIXED: Duplicated Settings Saved admin_notices

2.93

  • Remove screen_icon from SCB.

2.92

  • Add title attr to pages link. Props @Mahjouba91.

2.91

  • Validate text option against kses
  • Update SCB Framework

2.90

  • Remove po/mo files from the plugin
  • Use translate.wordpress.org to translate the plugin

2.89.1

  • FIXED: before and after args

2.89

  • NEW: wrapper_tag option to allow other HTML tag besides DIV and wrapper_class option to allow other class name besides wp-pagenavi. Props @Mahjouba91.

2.88

  • NEW: Added filters for altering class names. Props @bookwyrm

2.87

  • NEW: Uses WordPress native uninstall.php

2.86

  • NEW: Bump to 4.0
  • NEW: Added rel=next and rel=previous

2.85

  • FIXED: “Use pagenavi-css.css” & “Always Show Page Navigation” in the options are not being saved

2.84

  • FIXED: Updated scb framework to fix scbAdminPage incompatible error

2.83

  • added ‘echo’ parameter
  • added Estonian and Bengali translations
  • updated scbFramework

2.82

  • fixed prev/next links not appearing in some conditions
  • added Hebrew, Georgian and Azerbaijani translations
  • updated scbFramework

2.81

  • require an explicit type; fixes bugs with multipart pages

2.80

  • support for multi-part pages and user queries
  • moved prev/next links before/after first/last links
  • more info

2.74 (2011-02-17)

  • added ‘smaller’ and ‘larger’ classes
  • added $query arg to wp_pagenavi()
  • updated translations
  • more info