从类别数组中获取帖子

问题描述 投票:1回答:5

我有某些类别的ID。我想循环一次此类别,最后一次发表3个帖子。我尝试这种方法,但只能从数组中选择一个类别。

<?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; ?>
wordpress post categories
5个回答
3
投票

这段代码不起作用:'cat' => 48,43,49,46,47,44,51,50,42,

您必须使用数组'cat' => array(48,43,49,46,47,44,51,50,42),


3
投票

出于某种原因,“猫”无效。我们用过

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

并且工作正常。

完整的工作代码:

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

0
投票

您可以在要发布的类别中获得所有帖子。

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

您可以根据自己的期望找到帖子。

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

0
投票

根据您的代码。更新-

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

-1
投票

实际上应该是:'cat'=> '48,43,49,46,47,44,51,50,42'

© www.soinside.com 2019 - 2024. All rights reserved.