我們在用wordpress開發網站的時候會在文章頁中引入上一篇下一篇,但是發現新聞頁的上下文章有可能是產品分類的post,這個就不太合理,如何顯示同一分類下的上一篇下一篇文章呢?隨ytkah一起來看看
我們知道普通的調用上下篇文章的代碼是
<div class="prev"><?php previous_post_link('« %link') ?></div> <div class="next"><?php next_post_link('%link »') ?></div>
我們進行改造一下
<div class="prev"><?php previous_post_link('« %link' ,' %title' , true) ?></div> <div class="next"><?php next_post_link('%link »','%title' , true) ?></div>
函數用法說明:
//上一頁 <?php next_post_link('format', 'link', 'in_same_cat', 'excluded_categories'); ?> //下一頁 <?php next_post_link('format', 'link', 'in_same_cat', 'excluded_categories'); ?>
參數說明:
format
(字符串)鏈接的格式字符串。用該參數控制鏈接前后內容。字符串中的’%link’會被聲明為’link’的內容(見下一個參數)取代。’Go to %link’ 將生成”Go to <a href=…” ,在其中加上相應的HTML標簽,生成最終結果。默認值為 ‘%link »’。
link
(字符串)所顯示的鏈接文本。默認為上一篇文章的標題(’%title’)。
in_same_cat
(布爾型)指明上一篇文章是否與當前文章在同一分類目錄中。如果該參數值為TRUE,只顯示當前文章所屬分類目錄下的文章。有效值包括:
TRUE
FALSE(默認值)
excluded_categories
(字符串)上一篇文章所不屬於的分類目錄的數值型ID。用and分隔多個分類ID,如’1 and 5 and 15’。無默認值。
參考資料http://www.shouce.ren/post/view/id/3682