Getting posts from categories array

懵懂的女人 提交于 2019-12-24 11:26:46

问题


I have some certain categories' ids. I want to loop this categories and last 3 posts in one time. I try this but only come one category from array.

<?php
    $args = array(
    'cat'      => 48,43,49,46,47,44,51,50,42,
    'order'    => 'ASC',
    'showposts' => 3
        );
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

回答1:


This piece of code won't work: 'cat' => 48,43,49,46,47,44,51,50,42,

You'll have to use an array 'cat' => array(48,43,49,46,47,44,51,50,42),




回答2:


For some reason 'cat' did not work. We used

'category__in' => array( 2, 6 ),

and it workined fine.

The completed working code:

<?php
// -----------------------------
$args = array(
    'post_type' => 'post',
    'order' => 'ASC',
    'category__in' => array(2,6)
    );
$query = new WP_Query( $args );
?>



回答3:


You can get All Posts in a Category which one you want publish.

query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );

You can find post as per your expectation by.

query_posts( array ( 'category_name' => 'carousel', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC' ) );



回答4:


According to your code. Update --

<?php
    $args = array(
    'cat'      => [48,43,49,46,47,44,51,50,42], //change here array
    'order'    => 'ASC',
    'posts_per_page' => 3 //showposts deprecated now
        );
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?> // you should reset your query



回答5:


should actually be: 'cat' => '48,43,49,46,47,44,51,50,42'



来源:https://stackoverflow.com/questions/5879904/getting-posts-from-categories-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!