Getting posts from categories array

2019-08-01 09: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; ?>

4条回答
We Are One
2楼-- · 2019-08-01 09:47

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' ) );
查看更多
一夜七次
3楼-- · 2019-08-01 09:51

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),

查看更多
ら.Afraid
4楼-- · 2019-08-01 09:52

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

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-08-01 10:14

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 );
?>
查看更多
登录 后发表回答