显示多个下一篇和上一篇帖子

时间:2012-11-07 作者:rwzdoorn

我创建了一个包含单个帖子的投资组合。查看帖子时,我想显示下一篇/前两篇帖子(公文包项目)。

目前我正在使用以下代码,但只显示一篇帖子:

<?php $prevPost = get_previous_post(true);
    if($prevPost) {?>
    <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(114,114) );}?>
    <?php previous_post_link(\'%link\',"$prevthumbnail <h3>%title</h3>", TRUE ); ?>
我要创建以下内容:

[next post] | [next post] | [current post (active state)] | [previous post] | [previous post].
有人知道如何将其更改为工作代码吗?

3 个回复
SO网友:Mridul Aggarwal

这不是一个很好的方法,但很容易实现

global $post;
$original = $post;
$next = get_next_post(true);
$prev = get_previous_post(true);
$post = $next;
next_post_link();
$post = $original;
next_post_link();
// display current post
previous_post_link();
$post = $prev;
previous_post_link();
$post = $original;
为了提高代码的效率,可以从“wp includes/link template.php”中的核心代码中复制相应的函数;然后根据需要修改它们

在最基本的层面上get_adjacent_post, 改变LIMIT 1LIMIT 2 &;$wpdb->get_row$wpdb->get_results, 然后,直接/间接使用此功能的所有其他功能将相应更改

注意:不要更改核心代码,而是将函数复制到主题/插件,重命名,修改,然后使用新函数显示链接

SO网友:Daniel

get_previous_post()get_next_post() 两者都使用get_adjacent_post(). 该函数通过检查全局$post对象来确定下一个/上一个post。您可以更改$post 实现目标的目标:

$current_post = $post; // keep a copy of the current post;

$post = get_previous_post(false); 
echo $post->post_title;
$post = get_previous_post(false);
echo $post->post_title;

$post = $current_post;

$post = get_next_post(false); 
echo $post->post_title;
$post = get_next_post(false);
echo $post->post_title;

SO网友:rwzdoorn

不得不把一些东西混在一起,但这是我的结果:

这对于更多想要用强大的下一个/上一个系统创建投资组合的人来说非常方便。

谢谢大家的帮助!

<?php
$current_post = $post; // keep a copy of the current post;

$post = get_previous_post(false); 
$attr = array(
                                \'title\' => $post->post_title,
                                \'alt\' => $post->post_title,
                                \'class\' => \'meta_icon\'
                            );
echo "<a href=\'".$post->guid."\'>";
echo "<div class=\'meta_apps\'>";
echo "<div class=\\"meta-icon\\">".get_the_post_thumbnail($post->post_id, \'thumbnail\', $attr)."</div>";
echo "<div class=\\"meta-title\\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";

$post = get_previous_post(false);
$attr = array(
                                \'title\' => $post->post_title,
                                \'alt\' => $post->post_title,
                                \'class\' => \'meta_icon\'
                            );
echo "<a href=\'".$post->guid."\'>";
echo "<div class=\'meta_apps\'>";
echo "<div class=\\"meta-icon\\">".get_the_post_thumbnail($post->post_id, \'thumbnail\', $attr)."</div>";
echo "<div class=\\"meta-title\\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";

$post = get_previous_post(false);
$attr = array(
                                \'title\' => $post->post_title,
                                \'alt\' => $post->post_title,
                                \'class\' => \'meta_icon\'
                            );
echo "<a href=\'".$post->guid."\'>";
echo "<div class=\'meta_apps\'>";
echo "<div class=\\"meta-icon\\">".get_the_post_thumbnail($post->post_id, \'thumbnail\', $attr)."</div>";
echo "<div class=\\"meta-title\\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";

$post = $current_post;
$post = get_next_post(false);

$attr = array(
                                \'title\' => $post->post_title,
                                \'alt\' => $post->post_title,
                                \'class\' => \'meta_icon\'
                            );
echo "<a href=\'".$post->guid."\'>";
echo "<div class=\'meta_apps\'>";
echo "<div class=\\"meta-icon\\">".get_the_post_thumbnail($post->post_id, \'thumbnail\', $attr)."</div>";
echo "<div class=\\"meta-title\\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";

$post = get_next_post(false);
$attr = array(
                                \'title\' => $post->post_title,
                                \'alt\' => $post->post_title,
                                \'class\' => \'meta_icon\'
                            );
echo "<a href=\'".$post->guid."\'>";
echo "<div class=\'meta_apps\'>";
echo "<div class=\\"meta-icon\\">".get_the_post_thumbnail($post->post_id, \'thumbnail\', $attr)."</div>";
echo "<div class=\\"meta-title\\">".$post->post_title."</div>";
echo "</div>";
echo "</a>";



// $post = get_next_post(false); 
// echo $post->post_title;
// $post = get_next_post(false);
// echo $post->post_title;

get_post_thumbnail();
next_post_link();
previous_post_link();
?>

结束

相关推荐

在具有背景图像的类的HREF中嵌套NEXT和PREVER

如何将下一个和上一个链接嵌套在a href属性中,该属性在单个帖子页面中具有类和背景图像?我想让它像下面的东西,这是不工作的。<a class=\"next\" href=\"<?php echo $prev; ?>\">Previous</a> <a class=\"prev\" href=\"<?php echo $next; ?>\">Next</a> 以下是指向示例站点的链接:http://ifunnypic