[英]WordPress sidebar: make custom loop display posts in same category as single post, EXCLUDING current single post from the list
這是從側邊欄設置的,以顯示與正在查看的當前單個帖子相同類別的10個最新帖子。 不幸的是,它還包括列表中當前單個帖子的標題和摘錄。
有誰知道如何更改它,使其不包括當前的單個帖子? 除此之外,它工作正常。
<?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>
希望這個答案不會太晚。 戴夫(Dave),側欄中的代碼只有一個小錯誤:
在顯示&post__not_in=
的行中,在post
和not
單詞之間還有一個下划線。
刪除它,它將起作用。
感謝Poelinca的代碼段。
在您的主要查詢中(來自您的查詢是single.php文件):
<?php global $mainPostID; $mainPostID = get_the_id(); ?>
然后,您將在側邊欄代碼中看到以下內容:
<?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.