Wordpress 获取不包含类别 ID 的博客文章
Wordpress fetch blog posts excluding a category ID
我想在我的 wordpress 网站上显示最新的博客 post。我正在使用这段代码,它显示了任何类别的最新 3 个博客 posts。
<?php
$the_query = new WP_Query( 'posts_per_page=3');
while ($the_query -> have_posts()) : $the_query -> the_post();
?>
<!-- main content here -->
<div class="blog-post">
<?php echo wp_trim_words( get_the_excerpt(), 20, ' ...' ); ?>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
现在我想从此脚本中排除一个 post 类别(应排除类别 ID 为 17 的所有 post。我已经尝试了以下脚本,但无论哪种方式它不会显示任何 posts(所有代码保持不变,除了我更改和尝试的前几行)
这个没有显示任何东西
$the_query = new WP_Query( 'posts_per_page=3', 'cat=-17');
尝试使用数组:
$the_query = new WP_Query($argsQuery);
$argsQuery = array(
'posts_per_page' => 3,
'cat' => '-17',
);
我是不是漏掉了什么?
试试这个:
$the_query = new WP_Query( array( 'cat' => '-17', 'posts_per_page' => '3' ) );
我想在我的 wordpress 网站上显示最新的博客 post。我正在使用这段代码,它显示了任何类别的最新 3 个博客 posts。
<?php
$the_query = new WP_Query( 'posts_per_page=3');
while ($the_query -> have_posts()) : $the_query -> the_post();
?>
<!-- main content here -->
<div class="blog-post">
<?php echo wp_trim_words( get_the_excerpt(), 20, ' ...' ); ?>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
现在我想从此脚本中排除一个 post 类别(应排除类别 ID 为 17 的所有 post。我已经尝试了以下脚本,但无论哪种方式它不会显示任何 posts(所有代码保持不变,除了我更改和尝试的前几行)
这个没有显示任何东西
$the_query = new WP_Query( 'posts_per_page=3', 'cat=-17');
尝试使用数组:
$the_query = new WP_Query($argsQuery);
$argsQuery = array(
'posts_per_page' => 3,
'cat' => '-17',
);
我是不是漏掉了什么?
试试这个:
$the_query = new WP_Query( array( 'cat' => '-17', 'posts_per_page' => '3' ) );