PHP*_*per 1 php wordpress custom-post-type
$posts = query_posts(array('post_type'=>'sedan', 'category'=>'1', 'posts_per_page'=>'4'));
Run Code Online (Sandbox Code Playgroud)
category上述查询中的参数似乎无法按预期工作。它显示了帖子Sedan类型中的所有帖子,但是我只想指定category ID = 1内的类别Sedan post type。
试试看
$posts = query_posts(array('post_type'=>'sedan', 'cat'=>'1', 'posts_per_page'=>'4'));
Run Code Online (Sandbox Code Playgroud)
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'));
Run Code Online (Sandbox Code Playgroud)
然后修改主查询的更安全方法。
我总是喜欢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;
Run Code Online (Sandbox Code Playgroud)