微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Wordpress中多次使用$ do_not_duplicate来避免重复帖子

如何解决在Wordpress中多次使用$ do_not_duplicate来避免重复帖子

|| 我想知道您是否可以帮助我解决一个易于解决wordpress问题? 我在http://www.totalbackpacker.co.uk上创建了一个杂志风格的主题,该主题页面顶部具有三个“精选”故事,其次是下面的最新文章。 使用
$do_not_duplicate
方法(http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action),我已经能够忽略在最新帖子列表中显示为主要特色故事的帖子...但是我试图省略第二和第三个专题故事时遇到困难,大概是因为ѭ0仅打算使用一次。 我正在使用以下方法显示主要精选故事:
<?PHP query_posts(\'category_name=editors-picks-main&posts_per_page=1\'); ?>
<?PHP if (have_posts()) : ?>
<?PHP while (have_posts()) : the_post();
$do_not_duplicate = $post->ID; ?>
因此,它基本上是从
editors-picks-main
类别中提取最新帖子。 使用以下内容显示“第二”个专题故事(from4ѭ类别的最新帖子):
<?PHP query_posts(\'category_name=editors-picks-sub-1&posts_per_page=1\'); ?>
<?PHP if (have_posts()) : ?>
<?PHP while (have_posts()) : the_post(); ?>
而“第三”精选故事(来自
editors-picks-sub-2
类别的最新帖子)则使用:
<?PHP query_posts(\'category_name=editors-picks-sub-2&posts_per_page=1\'); ?>
<?PHP if (have_posts()) : ?>
<?PHP while (have_posts()) : the_post(); ?>
对于最新的帖子部分(请注意:帖子分布在两列中,因此重复此代码)我正在使用:
<?PHP query_posts(\'category_name=blog&posts_per_page=4\'); ?>
<?PHP if (have_posts()) : ?>
<?PHP $count = 0; ?>
<?PHP while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<?PHP $count++; ?>
<?PHP if ($count == 1) : ?>
<h5 class=\"smaller\"><a href=\"<?PHP the_permalink(); ?>\" title=\"<?PHP the_title(); ?>\"><?PHP the_title(); ?></a></h5>
<p class=\"editorspickssml\"><?PHP the_excerpt(); ?></p>
<?PHP elseif ($count == 2) : ?>
Second post here
<?PHP elseif ($count == 3) : ?>
Third post here
<?PHP elseif ($count == 4) : ?>
Fourth post here
<?PHP endif; ?>
<?PHP endwhile; ?>
<?PHP endif; ?>
有人对我如何使它起作用有任何建议吗?我知道我需要在
editors-picks-sub-1
editors-picks-sub-2
代码添加类似于
$do_not_duplicate = $post->ID; ?>
内容...我只是不确定! 我真的很感谢任何提示! 在此先感谢您的帮助, 马丁     

解决方法

        将前三个“精选帖子”的ID收集到一个变量中。 创建一个数组,在循环之前保存要排除的帖子ID:
$exclude = new Array();
在第一个,第二个和第三个循环中,将帖子的ID添加到数组中:
array_push($exclude,$post->ID);
然后从您最近的帖子查询中排除那些ID。
query_posts(\'category_name=blog&posts_per_page=4&post__not_in=\'.$exclude);
注意:我不确定您是否应该使用这样的查询帖子,它正在进行很多查询编辑。同样,如果您希望分页工作,则需要包括分页的变量。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。