循环两个不同类别的WP_QUERY

时间:2020-06-15 作者:sialfa

我想循环两个不同的类别,以在自定义主题中创建两列的布局。我使用这段代码和bootstrap 4作为前端框架,但我无法使用所有可用于bootstrap的12col。在循环过程中,元素只会正确堆叠一次,我可以通过嵌套循环来解决这个问题吗?

<?php 
$main_news = new WP_Query(
    array(
    \'post_type\' => \'post\',
    \'category_name\' => \'News\',
    \'posts_per_page\' => 6    
    )
);
    if( $main_news->have_posts() ): 
        while( $main_news->have_posts() ): $main_news->the_post(); 
?>
        <div class="col-sm-12 col-md-6 col-lg-6 p-4 mt-3">
            <img class="card-img-top" src="<?php echo the_post_thumbnail_url(); ?>">
            <h4 class="card-title"><?php the_title(); ?></h4>
            <p class="card-text"><?php echo get_the_excerpt( get_the_ID() ); ?></p> 
            <a class="" href="<?php the_permalink(); ?>"><?php _e(\'Read\', \'\'); ?></a> 
        </div>
<?php 
        endwhile; 
    endif;
    wp_reset_postdata();

$side_news = new WP_Query( 
    array( 
    \'post_type\' => \'post\', 
    \'category_name\' => \'Not important\', 
    \'posts_per_page\' => 6 
    ) 
); 
    if( $side_news->have_posts() ): 
        while( $side_news->have_posts() ): $side_news->the_post();
?>
        <div class="col-md-6 col-lg-6 card shadow-lg d-none d-md-block mt-3">
            <div class="card-body">
                <img class="card-img-top" src="<?php echo the_post_thumbnail_url(); ?>">
                <h4 class="card-title"><?php echo get_the_title( get_the_ID() ); ?></h4>
                <p class="card-text"><?php echo get_the_excerpt( get_the_ID() ); ?></p> 
                <a class="" href="<?php the_permalink(); ?>"><?php _e(\'Read\', \'\'); ?></a> 
            </div>
        </div>
<?php 
        endwhile; 
    endif;
    wp_reset_postdata(); 
?>  

我所期望的是,这两个循环将创建一列,如上例所示:

//
<div class="row m-0">
<div class="col-8">content</div><div class="col-4">other content</div> // these two divs aligned 
<div class="col-8">content</div><div class="col-4">other content</div> // etc...
</div>

UPDATE: 这就是我所期望的布局。我嵌套了两个循环以获得所需的类别,我刚刚注意到嵌套循环的内容将在某个点重复。有什么办法吗?

enter image description here

1 个回复
最合适的回答,由SO网友:Sabbir Hasan 整理而成

使用传统方式实现最终的outlook有点棘手。假设两个类别都至少有6篇文章。现在,首先使用get_posts() 不<罢工>WP_Query

$side_news = get_posts(array(
    \'post_type\' => \'post\',
    \'category_name\' => \'category-slug-1\',
    \'posts_per_page\' => 6
));

$side_news2 = get_posts(array(
    \'post_type\' => \'post\',
    \'category_name\' => \'category-slug-2\',
    \'posts_per_page\' => 6
));

now that we know we have 6 post each let\'s merge the result as alternative position.

$c_result = array();
while(!empty($side_news) || !empty($side_news2)) {
    if(!empty($side_news)) {
        $result[] = array_shift($side_news);
    }
    if(!empty($side_news2)) {
        $result[] = array_shift($side_news2);
    }
}

Now we have an array with 12 posts. Let\'s loop through it and achieve the final layout:

echo \'<div class="row m-0">\';
$i = 0;
foreach ($c_result as $post) {
    $i++;

    $id = $post->ID;
    $title = $post->post_title;
    $content = $post->post_content;

    if($i%2 == 0):
        echo"<div class=\'col-8\'>$id $title $content</div>";
    else:
        echo"<div class=\'col-4\'>$id $title $content</div>";
    endif;
}
echo \'</div>\';

get_posts() returns post as object with following keys:

WP_Post Object
(
    [ID] =>
    [post_author] =>
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] =>
    [comment_status] =>
    [ping_status] => 
    [post_password] => 
    [post_name] =>
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] =>
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] =>
    [post_type] =>
    [post_mime_type] => 
    [comment_count] =>
    [filter] =>
)

相关推荐

Get Current User ID Inside a Loop返回0表示短码

我有一个函数,它对给定作者的所有帖子都有一个循环。我使用get_current_user_id() 但这在循环中似乎不起作用,或者可能是它的短代码问题。我的函数在shortcode的帮助下运行。当前用户总是返回0,因此它会显示我网站上所有帖子的元数据。function get_meta_value_by_meta_key(){ $author_id = \'get_current_user_id()\'; // do stuff to get user I $author