[英]WordPress sidebar: make custom loop display posts in same category as single post, EXCLUDING current single post from the list
This is from a sidebar set up to display 10 recent posts from the same category as the current single post being viewed. 这是从侧边栏设置的,以显示与正在查看的当前单个帖子相同类别的10个最新帖子。 Unfortunately, it also includes the title and excerpt of the current single post in the list.
不幸的是,它还包括列表中当前单个帖子的标题和摘录。
Does anyone know how to change it so it EXCLUDES the current single post? 有谁知道如何更改它,使其不包括当前的单个帖子? Other than that, it works fine.
除此之外,它工作正常。
<?php
$query = "showposts=10&orderby=date&cat=";
foreach((get_the_category()) as $category)
{
$query .= $category->cat_ID .",";
}
query_posts($query);
?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title() ?></a>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
</ul>
Hope this answer doesn't come too late. 希望这个答案不会太晚。 Dave, there is just a minor mistake in the code that goes in the sidebar:
戴夫(Dave),侧栏中的代码只有一个小错误:
In the line that says &post__not_in=
there is an additional underscore between the word post
and not
. 在显示
&post__not_in=
的行中,在post
和not
单词之间还有一个下划线。
Delete it and it will work. 删除它,它将起作用。
Thanks Poelinca for the code snippet. 感谢Poelinca的代码段。
In you're main query do ( the query from you're single.php file ) : 在您的主要查询中(来自您的查询是single.php文件):
<?php global $mainPostID; $mainPostID = get_the_id(); ?>
Then you're sidebar code would become this : 然后,您将在侧边栏代码中看到以下内容:
<?php
$query = "showposts=10&orderby=date&cat=";
foreach((get_the_category()) as $category) {
$query .= $category->cat_ID .",";
}
#magic happens here
global $mainPostID;
if ( !empty($mainPostID) && is_single() )
$query .= "&post__not_in=" . $mainPostID;
query_posts($query);
?>
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.