简体   繁体   English

WordPress侧边栏:使自定义循环显示与单个帖子相同类别的帖子,从列表中排除当前单个帖子

[英]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=的行中,在postnot单词之间还有一个下划线。

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.

相关问题 Wordpress:随机帖子但不包括单个帖子类别 - Wordpress: Random Posts but excluding a single post category 在wordpress帖子中,显示与当前帖子属于同一类别但不包括当前帖子本身的帖子列表 - In a wordpress post, display the list of posts that belong to the same category as the current post but exclude the current post itself 如何从侧边栏中的单页自定义帖子类型获取帖子以显示在页面中? - How do I get the posts from single page custom post type from a sidebar to display in a page? 如何在WordPress单个帖子中添加类别(帖子) - How to add category(posts) in wordpress single post 使用Have_posts()WordPress从单个类别获取帖子 - Get post from a single category with have_posts () WordPress 从“其他相同类别的帖子”循环中排除当前帖子 - Exclude current post from “Other posts of same category” loop 显示来自Wordpress自定义帖子类型类别的帖子 - Display posts from a Wordpress custom post type category 如何在wordpress中以自定义帖子类型循环进入单个附件帖子? - how to loop in single attachment posts in custom post type in wordpress? 当用户点击single.php页面中的下一个/上一个帖子时,显示来自同一类别的帖子 - Display the posts from the same category when user clicks on next/previous post in single.php page Wordpress从单个帖子,多个类别中获取当前类别 - Wordpress Get the CURRENT Category from Single Post, Multiple Categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM