【问题标题】:Wordpress: Show Latest Post on Post Template, but don't when on the Latest PostWordpress:在帖子模板上显示最新帖子,但在最新帖子上不显示
【发布时间】:2021-05-16 19:01:26
【问题描述】:

我有一个自定义博客页面content-single.php,它输出自定义模板。

我已经输出了特定类别的最新帖子,但问题是,如果您在该类别的最新帖子中,您会看到指向该帖子的链接,这没有任何好处。

所以基本上,

标题:“为什么设计很重要” 内容:输出帖子内容 最新设计博客:“为什么设计很重要”

这是我的代码(我的 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())
);

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    使用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

    【讨论】:

    • 感谢 Alexandru,它正是我想要的。将更新我的帖子并将其标记为答案。
    猜你喜欢
    • 2019-08-24
    • 1970-01-01
    • 2017-05-28
    • 2017-10-11
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多