當前位置:學者齋 >

設計製作 >網頁設計 >

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

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

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

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

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

Themes(父級頁面)

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

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

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

如果當前頁面是 zBorder,那麼就要上一篇連結是 zBench 的,下一篇連結是 zSofa 的。

把下面函式程式碼放入 (注:函式隨手寫的,可能不夠精簡)

/**

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

}

[函式]

subpage_nav_link($prevText, $nextText)

[引數]

$prevText: 為前一篇文章連結文字,為空時預設是頁面標題

$nextText: 為下一篇文章連結文字,為空時預設是頁面標題;

標籤: 一頁 WordPress 頁面
  • 文章版權屬於文章作者所有,轉載請註明 https://xuezhezhai.com/zh-tw/sjzz/sjzz/z46dew.html