WordPress的文章頁頁有實(shí)現(xiàn)上一篇下一篇的功能函數(shù),不過我們想在單頁page.php里面實(shí)現(xiàn)上一頁下一頁的功能,previous_post_link()和next_post_link() 函數(shù)還不能完全滿足我的需要,所以就自己寫函數(shù)實(shí)現(xiàn).
頁面有分級功能,需求是按 menu order 排序的子級頁面之間有上一篇、下一篇鏈接,如:
Themes(父級頁面)
---- zBench(子級頁面1)
---- zBorder(子級頁面2)
---- zSofa(子級頁面3)
如果當(dāng)前頁面是 zBorder,那么就要上一篇鏈接是 zBench 的,下一篇鏈接是 zSofa 的,把下面函數(shù)代碼放入 functions.php(注:函數(shù)隨手寫的,可能不夠精簡).
- /**
- * get subpage previous/next page link by zwwooooo
- */
- function subpage_nav_link($prevText='', $nextText='') {
- global $post;
- if ( !$post->post_parent ) return null; //如果不是子頁面返回Null
- $args = array(
- 'sort_order' => 'ASC',
- 'sort_column' => 'menu_order',
- 'child_of' => $post->post_parent,
- 'post_type' => 'page'
- );
- $pages = get_pages($args);
- $num = count($pages);
- $i = 0;
- $index = -1;
- foreach ($pages as $page) {
- if ($page->ID == $post->ID) {
- $index = $i;
- break;
- }
- ++$i;
- }
- if ($i == 0) {
- $prev = '';
- $next = $pages[$index+1];
- } elseif ($i == $num-1) {
- $prev = $pages[$index-1];
- $next = '';
- } else {
- $prev = $pages[$index-1];
- $next = $pages[$index+1];
- }
- if ($prev) {
- if ($prevText) {
- if ( substr_count($prevText, '%title') > 0 ) {
- $explode = explode('%title', $prevText);
- $prevText = $explode[0] . get_the_title($prev->ID) . $explode[1];
- }
- } else {
- $prevText = get_the_title($prev->ID);
- }
- $prevlink = '<a class="previous-page-link" href="' . get_page_link($prev->ID). '">' . $prevText . '</a>';
- }
- if ($next) {
- if ($nextText) {
- if ( substr_count($nextText, '%title') > 0 ) {
- $explode = explode('%title', $nextText);
- $nextText = $explode[0] . get_the_title($next->ID) . $explode[1]; //Vevb.com
- }
- } else {
- $nextText = get_the_title($next->ID);
- }
- $nextlink = '<a class="next-page-link" href="' . get_page_link($next->ID). '">' . $nextText . '</a>';
- }
- return array($prevlink, $nextlink);
- }
[函數(shù)]
subpage_nav_link($prevText, $nextText)
[參數(shù)]
$prevText:為前一篇文章鏈接文字,為空時默認(rèn)是頁面標(biāo)題
$nextText:為下一篇文章鏈接文字,為空時默認(rèn)是頁面標(biāo)題;
例如:一般的主題是在 page.php 的 loop 循環(huán)里面,不知道就在 the_content(); 下面吧,插入調(diào)用代碼:
- <?php
- if ( function_exists('subpage_nav_link') ) {
- if ( $subpage_nav_link = subpage_nav_link() ) {
- echo $subpage_nav_link[0]; //上一篇(頁面)鏈接
- echo $subpage_nav_link[1]; //下一篇(頁面)鏈接
- }
- }
- ?>
注:可以用 if (!$subpage_nav_link[0]) 來判斷有沒有上一篇,同樣 if (!$subpage_nav_link[1]) 來判斷有沒有下一篇.
PS:$prevText 和 $nextText 還支持字符組合,如 subpage_nav_link('oo %title xx', '') 這樣的話,前一篇文章鏈接文章會變成“oo 頁面名 xx”
另一篇實(shí)用文章:實(shí)現(xiàn)wordpress文章頁調(diào)用同分類上/下一篇文章
wordpress提供的顯示上一篇、下一篇文章的函數(shù)代碼是按照發(fā)布順序調(diào)用的,前幾天做的wordpress小說模板,由于使用每個分類添加一部小說《博客吧首款wordpress小說網(wǎng)站主題模板wpnovel》,如果使用這樣的上下篇文章調(diào)用順序顯示不合適,讓文章頁顯示同分類下的上一篇、下一篇文章才是正道,wordpress是強(qiáng)大的,總能滿足用戶的想法,通過搜索找到了相關(guān)的函數(shù)代碼.
默認(rèn)直接調(diào)用的代碼:
- <?php previous_post_link('上一篇: %link') ?>
- <?php next_post_link('下一篇: %link') ?>
當(dāng)文章處于首篇或末篇時,會顯示空白,但可以通過增加判斷還填補(bǔ)空白.
- <?php if (get_previous_post()) { previous_post_link('上一篇: %link');} else {echo "已是最后文章";} ?>
- <?php if (get_next_post()) { next_post_link('下一篇: %link');} else {echo "已是最新文章";} ?>
經(jīng)過測試雖然顯示同分類下的文章,但首篇文章和末尾的文章會不顯示對應(yīng)的提示信息“已是最后文章”和“已是最后文章”,只要在get_previous_post()函數(shù)中指定一下文章所屬分類ID便能使代碼完全有效.
下面是完整的代碼:
- <?php
- $categories = get_the_category();
- $categoryIDS = array();
- foreach ($categories as $category) {
- array_push($categoryIDS, $category->term_id);
- }
- $categoryIDS = implode(",", $categoryIDS);
- ?>
- <?php if (get_previous_post($categoryIDS)) { previous_post_link('上一篇: %link','%title',true);} else { echo "已是最后文章";} ?>
- <?php if (get_next_post($categoryIDS)) { next_post_link('上一篇: %link','%title',true);} else { echo "已是最新文章";} ?>
打開主題目錄下的文章頁single.php,在要顯示的位置添加代碼,保存文件即可.
新聞熱點(diǎn)
疑難解答
圖片精選