欧美日韩不卡一区二区三区,www.蜜臀.com,高清国产一区二区三区四区五区,欧美日韩三级视频,欧美性综合,精品国产91久久久久久,99a精品视频在线观看

網(wǎng)頁設(shè)計(jì)

WordPress單頁面上一頁下一頁實(shí)現(xiàn)方法

時(shí)間:2025-04-07 06:04:08 網(wǎng)頁設(shè)計(jì) 我要投稿
  • 相關(guān)推薦

WordPress單頁面上一頁下一頁實(shí)現(xiàn)方法

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

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

  Themes(父級(jí)頁面)

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

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

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

  如果當(dāng)前頁面是 zBorder,那么就要上一篇鏈接是 zBench 的,下一篇鏈接是 zSofa 的。

  把下面函數(shù)代碼放入 functions.php(注:函數(shù)隨手寫的,可能不夠精簡(jiǎn))

  /**

  * 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];

  }

  } 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: 為前一篇文章鏈接文字,為空時(shí)默認(rèn)是頁面標(biāo)題

  $nextText: 為下一篇文章鏈接文字,為空時(shí)默認(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', '') 這樣的話,前一篇文章鏈接文章會(huì)變成“oo 頁面名 xx”

  wordpress提供的顯示上一篇、下一篇文章的函數(shù)代碼是按照發(fā)布順序調(diào)用的,前幾天做的wordpress小說模板,由于使用每個(gè)分類添加一部小說《博客吧首款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)文章處于首篇或末篇時(shí),會(huì)顯示空白,但可以通過增加判斷還填補(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)過測(cè)試雖然顯示同分類下的文章,但首篇文章和末尾的文章會(huì)不顯示對(duì)應(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,在要顯示的位置添加代碼,保存文件即可。 

【W(wǎng)ordPress單頁面上一頁下一頁實(shí)現(xiàn)方法】相關(guān)文章:

PHP列表頁實(shí)現(xiàn)的方法05-24

word顯示兩頁怎么變成一頁06-02

javasc ript 返回上一頁及刷新頁面的實(shí)現(xiàn)方法08-02

網(wǎng)站設(shè)計(jì)單頁還是多頁好04-06

人生這本書我們必須一頁一頁地看「英語美文」08-07

word怎么刪除一頁03-28

單頁網(wǎng)站設(shè)計(jì)技巧08-14

php靜態(tài)頁生成方法03-11

Word刪除空白頁的方法05-19