簡體   English   中英

Wordpress:在帖子模板上顯示最新帖子,但在最新帖子上時不顯示

[英]Wordpress: Show Latest Post on Post Template, but don't when on the Latest Post

我有一個自定義博客頁面content-single.php輸出自定義模板。

我已經輸出了特定類別的最新帖子,但問題是,如果您在該類別的最新帖子上,您會看到指向該帖子的鏈接,這沒有任何好處。

所以基本上,

標題:“為什么設計很重要” 內容:Output 發布內容 最近的設計博客:“為什么設計很重要”

這是我的代碼(我的 PHP 知識並不是那么好,所以很抱歉)。 如果有人可以幫助讓它不顯示此塊,那么它是最新的帖子。

非常感謝,

$args = array(
  'posts_per_page' => 1,
  'category_name' => 'design'
);

$q = new WP_Query( $args);

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();        
?>
<div class="col-12">
  <?php include("inc/components/blog-card-snippet.php"); ?>
</div>

<?php
    } // end while
    wp_reset_postdata();
} else {
?>
    <div class="col">
        <p>No Blogs marked as <strong>Design</strong> to show</p>
    </div>
<?php
}

回答

Alexandru Burca提供的修復是更新$args以包含post__not_in

$args = array(
  'posts_per_page' => 1,
  'category_name' => 'design',
  'post__not_in' => array(get_the_ID())
);

使用post__not_in $args更改為

$args = array(
  'posts_per_page' => 1,
  'category_name' => 'design',
  'post__not_in' => array(get_the_ID())
);

更多信息在這里https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM