gpt4 book ai didi

php - 类别 ID 上的 query_posts 不起作用

转载 作者:行者123 更新时间:2023-12-03 23:15:29 25 4
gpt4 key购买 nike

$posts = query_posts(array('post_type'=>'sedan', 'category'=>'1', 'posts_per_page'=>'4'));
category上面查询中的参数似乎没有按预期工作。它显示了来自 Sedan 的所有帖子帖子类型,但我只想用 category ID = 1 指定类别内 Sedan post type .

最佳答案

试试

$posts = query_posts(array('post_type'=>'sedan', 'cat'=>'1', 'posts_per_page'=>'4'));
cat而不是 category .

还有 不要使用 query_posts()为您的查询。

https://codex.wordpress.org/Function_Reference/query_posts

使用 get_posts()WP_Query()
您可以通过以下方式实现相同的目标:
$posts = get_posts(array('post_type'=>'sedan', 'category'=>'1', 'posts_per_page'=>'4'));

更安全的方法是修改主查询。

我总是喜欢 WP_Query我。
$args = array(
'post_type'=>'sedan',
'cat'=>'1',
'posts_per_page'=>'4'
);

$posts = new WP_Query($args);

$out = '';

if ($posts->have_posts()){
while ($posts->have_posts()){
$posts->the_post();
$out .= 'stuff goes here';
}
}
wp_reset_postdata();

return $out;

关于php - 类别 ID 上的 query_posts 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31957801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com