WordPress显示现有的下3个相邻的自定义帖子数 [英] Wordpress show next 3 x number adjacent custom posts from existing

查看:83
本文介绍了WordPress显示现有的下3个相邻的自定义帖子数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单个/详细的自定义帖子页面上,我想在侧边栏中显示li或div的自定义导航,同时显示自定义帖子系列中接下来的3个帖子的标题,摘要和永久链接.因此,如果我们在自定义帖子3上,那么它将在边栏中显示4、5、6.

On a single/detail Custom Post Page I would like to display a custom nav of li's or divs in a sidebar that displays both title, excerpt and permalink for the next 3 posts within the custom post series. So if we are on custom post 3 then it would show 4, 5, 6 in the sidebar.

我找到的最接近的是:-=

The closest I've found to this is :-=

global $post;
$current_post = $post; // remember the current post

for($i = 1; $i <= 3; $i++){
 $post = get_previous_post(); // this uses $post->ID
setup_postdata($post);

// do your stuff here       
the_title();

}

$post = $current_post; // restore

问题是,这仅显示第一篇下一篇文章,我需要显示3篇.

Problem is this only shows the first next post and I need to show 3.

谢谢 格兰尼男孩

推荐答案

您上面的代码非常接近.看来您只是缺少了endfor;,也许还有一些html.尝试打击代码:

Your code from above is very close. It looks like you're just missing an endfor; and maybe a bit of html. Try the blow code:

<ul>
    <?php
        global $post;
        $current_post = $post;

        for($i = 1; $i <= 3; $i++):
        $post = get_previous_post();
        setup_postdata($post);

        echo '<li>';
        echo '<h3>' . get_the_title() . '</h3>';
        the_excerpt();
        echo get_post_meta($post->ID, 'your_metafield_name', true);
        echo '</li>';

        endfor;
        $post = $current_post; 

    ?>
</ul>

使用the_excerpt()时,该帖子应已包含指向该帖子的更多信息"链接,因此您无需使用the_permalink()功能.如果您没有3或更多以前的帖子,它将仅显示有多少以前的帖子.我在single.php模板文件中对此进行了测试,但是如果您使用的是自定义模板,则它应该可以在自定义帖子类型的单模板文件中使用.如果您有任何问题,请告诉我.

When using the_excerpt() it should include a "read more" link already to the post, so you won't need to use the_permalink() function. If you don't have 3 or more previous posts, it will only show how many previous posts there are. I tested this in the single.php template file, but it should work in a custom post type single template file if you are using a custom one. Let me know if you have any questions.

这篇关于WordPress显示现有的下3个相邻的自定义帖子数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆