简体   繁体   English

Wordpress 获取不包含类别 ID 的博客文章

[英]Wordpress fetch blog posts excluding a category ID

I am trying to show the latest blog posts on my wordpress website.我正在尝试在我的 wordpress 网站上显示最新的博客文章。 I am working with this code, which shows the latest 3 blog posts with any category.我正在使用这段代码,它显示了任何类别的最新 3 篇博客文章。

<?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();    
                        
?>

Now I'd like to exclude a post category from this script (all posts with the category ID 17 should be excluded. I've tried the following scripts, but either way it won't show any posts (all the code stays the same, except the first few lines I changed and tried)现在我想从此脚本中排除一个帖子类别(应排除类别 ID 为 17 的所有帖子。我尝试了以下脚本,但无论哪种方式它都不会显示任何帖子(所有代码保持不变,除了我更改并尝试过的前几行)

This one did not show anything这个没有显示任何东西

$the_query = new WP_Query( 'posts_per_page=3', 'cat=-17'); 

Tried with array:尝试使用数组:

$the_query = new WP_Query($argsQuery); 

$argsQuery = array(
    'posts_per_page' => 3,
    'cat' => '-17',
);

Am I missing something here?我在这里错过了什么吗?

Try this:尝试这个:

$the_query = new WP_Query( array( 'cat' => '-17', 'posts_per_page' => '3' ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM