国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress 單頁面上一頁下一頁實現方法

2024-09-07 00:52:15
字體:
來源:轉載
供稿:網友

WordPress的文章頁頁有實現上一篇下一篇的功能函數,不過我們想在單頁page.php里面實現上一頁下一頁的功能,previous_post_link()和next_post_link() 函數還不能完全滿足我的需要,所以就自己寫函數實現.

頁面有分級功能,需求是按 menu order 排序的子級頁面之間有上一篇、下一篇鏈接,如:

Themes(父級頁面)

---- zBench(子級頁面1)

---- zBorder(子級頁面2)

---- zSofa(子級頁面3)

如果當前頁面是 zBorder,那么就要上一篇鏈接是 zBench 的,下一篇鏈接是 zSofa 的,把下面函數代碼放入 functions.php(注:函數隨手寫的,可能不夠精簡).

  1. /** 
  2.  * get subpage previous/next page link by zwwooooo 
  3.  */ 
  4. function subpage_nav_link($prevText=''$nextText='') { 
  5.     global $post
  6.     if ( !$post->post_parent ) return null; //如果不是子頁面返回Null 
  7.     $args = array
  8.         'sort_order' => 'ASC'
  9.         'sort_column' => 'menu_order'
  10.         'child_of' => $post->post_parent, 
  11.         'post_type' => 'page' 
  12.     ); 
  13.     $pages = get_pages($args); 
  14.     $num = count($pages); 
  15.     $i = 0; 
  16.     $index = -1; 
  17.     foreach ($pages as $page) { 
  18.         if ($page->ID == $post->ID) { 
  19.             $index = $i
  20.             break
  21.         } 
  22.         ++$i
  23.     } 
  24.     if ($i == 0) { 
  25.         $prev = ''
  26.         $next = $pages[$index+1]; 
  27.     } elseif ($i == $num-1) { 
  28.         $prev = $pages[$index-1]; 
  29.         $next = ''
  30.     } else { 
  31.         $prev = $pages[$index-1]; 
  32.         $next = $pages[$index+1]; 
  33.     } 
  34.     if ($prev) { 
  35.         if ($prevText) { 
  36.             if ( substr_count($prevText'%title') > 0 ) { 
  37.                 $explode = explode('%title'$prevText); 
  38.                 $prevText = $explode[0] . get_the_title($prev->ID) . $explode[1]; 
  39.             } 
  40.         } else { 
  41.             $prevText = get_the_title($prev->ID); 
  42.         } 
  43.         $prevlink = '<a class="previous-page-link" href="' . get_page_link($prev->ID). '">' . $prevText . '</a>'
  44.     } 
  45.     if ($next) { 
  46.         if ($nextText) { 
  47.             if ( substr_count($nextText'%title') > 0 ) { 
  48.                 $explode = explode('%title'$nextText); 
  49.                 $nextText = $explode[0] . get_the_title($next->ID) . $explode[1]; //Vevb.com 
  50.             } 
  51.         } else { 
  52.             $nextText = get_the_title($next->ID); 
  53.         } 
  54.         $nextlink = '<a class="next-page-link" href="' . get_page_link($next->ID). '">' . $nextText . '</a>'
  55.     } 
  56.     return array($prevlink$nextlink); 

[函數]

subpage_nav_link($prevText, $nextText)

[參數]

$prevText:為前一篇文章鏈接文字,為空時默認是頁面標題

$nextText:為下一篇文章鏈接文字,為空時默認是頁面標題;

例如:一般的主題是在 page.php 的 loop 循環里面,不知道就在 the_content(); 下面吧,插入調用代碼:

  1. <?php 
  2. if ( function_exists('subpage_nav_link') ) { 
  3.     if ( $subpage_nav_link = subpage_nav_link() ) { 
  4.         echo $subpage_nav_link[0]; //上一篇(頁面)鏈接 
  5.         echo $subpage_nav_link[1]; //下一篇(頁面)鏈接 
  6.     } 
  7. ?> 

注:可以用 if (!$subpage_nav_link[0]) 來判斷有沒有上一篇,同樣 if (!$subpage_nav_link[1]) 來判斷有沒有下一篇.

PS:$prevText 和 $nextText 還支持字符組合,如 subpage_nav_link('oo %title xx', '') 這樣的話,前一篇文章鏈接文章會變成“oo 頁面名 xx”

另一篇實用文章:實現wordpress文章頁調用同分類上/下一篇文章

wordpress提供的顯示上一篇、下一篇文章的函數代碼是按照發布順序調用的,前幾天做的wordpress小說模板,由于使用每個分類添加一部小說《博客吧首款wordpress小說網站主題模板wpnovel》,如果使用這樣的上下篇文章調用順序顯示不合適,讓文章頁顯示同分類下的上一篇、下一篇文章才是正道,wordpress是強大的,總能滿足用戶的想法,通過搜索找到了相關的函數代碼.

默認直接調用的代碼:

  1. <?php previous_post_link('上一篇: %link') ?> 
  2. <?php next_post_link('下一篇: %link') ?> 

當文章處于首篇或末篇時,會顯示空白,但可以通過增加判斷還填補空白.

  1. <?php if (get_previous_post()) { previous_post_link('上一篇: %link');} else {echo "已是最后文章";} ?> 
  2. <?php if (get_next_post()) { next_post_link('下一篇: %link');} else {echo "已是最新文章";} ?> 

經過測試雖然顯示同分類下的文章,但首篇文章和末尾的文章會不顯示對應的提示信息“已是最后文章”和“已是最后文章”,只要在get_previous_post()函數中指定一下文章所屬分類ID便能使代碼完全有效.

下面是完整的代碼:

  1. <?php 
  2. $categories = get_the_category(); 
  3.         $categoryIDS = array(); 
  4.         foreach ($categories as $category) { 
  5.             array_push($categoryIDS$category->term_id); 
  6.         } 
  7.         $categoryIDS = implode(","$categoryIDS); 
  8. ?> 
  9. <?php if (get_previous_post($categoryIDS)) { previous_post_link('上一篇: %link','%title',true);} else { echo "已是最后文章";} ?> 
  10. <?php if (get_next_post($categoryIDS)) { next_post_link('上一篇: %link','%title',true);} else { echo "已是最新文章";} ?> 

打開主題目錄下的文章頁single.php,在要顯示的位置添加代碼,保存文件即可.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定日县| 嘉荫县| 丘北县| 扶沟县| 霍林郭勒市| 牟定县| 安塞县| 剑川县| 长沙市| 禄丰县| 莎车县| 蒙阴县| 江油市| 紫阳县| 香河县| 行唐县| 基隆市| 云林县| 枣阳市| 郧西县| 霸州市| 太原市| 白玉县| 荔浦县| 尚义县| 安丘市| 富蕴县| 光泽县| 玛多县| 沁阳市| 新津县| 天祝| 太原市| 兰坪| 孝义市| 清苑县| 红原县| 顺平县| 桂林市| 介休市| 介休市|