开发者

Getting posts from categories array

开发者 https://www.devze.com 2023-03-02 12:28 出处:网络
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.

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开发者_Go百科($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>


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


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


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' ) );


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消