网络营销电子商务研究中心

网络营销电子商务研究中心 (https://www.0058.net/index.php)
-   WordPress (https://www.0058.net/forumdisplay.php?f=68)
-   -   为WordPress添加上一篇下一篇功能 (https://www.0058.net/showthread.php?t=4757)

Albertville 2014-06-01 10:11 AM

为WordPress添加上一篇下一篇功能
 
WordPress没有内置“上一篇 下一篇”功能,可是这个功能却是我们经常用到的,它不仅可以提高网站的用户体验度,而且能留住访客的脚步,增加网站的PV值。

其实这个功能实现起来很简单,只需在文章页面(一般都是Single.php文件)添加一些代码即可。

一、添加常用的“上一篇 下一篇”代码如下:


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

二、每个网站都有不同的分类目录,如何给文章页面添加同类别文章的“上一篇 下一篇”功能呢?也很简单,代码如下:
Code:

<?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 "没有了,已经是最新新闻";} ?>



All times are GMT +8. The time now is 10:38 PM.

Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.