2 WP_Querys一个接一个地重复 - 样式不同

时间:2015-10-14 02:36:11

标签: php wordpress themes wp-query

我正试图让两个不同风格的帖子一个接一个地出现然后再回来。所以它应该看起来像 D B 未分类 D B 未分类 等等

让它们的风格不同。我对PHP并不擅长,到目前为止我得到的最好的是一个类别,然后是另一个类别。

<section class="home-middle">
<div class="container">
    <div class="row">
        <div class="col-sm-8">

  <?php
   $args = array('category_name' => 'designer backstage',
              'post_status' => 'publish',
              'posts_per_page' => '3' );
    $category_posts = new WP_Query($args);

  if($category_posts->have_posts()) : 
  while($category_posts->have_posts()) : 
     $category_posts->the_post();
?>       
          <div class="stylists-book">
                <div class="image">
                  <div class="meta-designer">
                        <div class="pro-pic"><img src="images/stylists-pro1.jpg"></div>
                        <h3>Designer<hr></h3>
                        <p><a href="#"><?php the_author(); ?></a></p>
                        <span><?php the_date(); ?></span>
                  </div>
                    <img><?php the_post_thumbnail(); ?>
                </div>

           <?php

   $args = array('category_name' => 'uncategorized',
              'post_status' => 'publish',
              'posts_per_page' => '3');
   $category_posts = new WP_Query($args);

   if($category_posts->have_posts()) : 
  while($category_posts->have_posts()) : 
     $category_posts->the_post();
 ?>  

 <div class="look" style="max-height: 200px">
              <div class="row">
               <div class="col-md-4">
                    <div class="team-modster">
                          <span><?php the_author(); ?></span>
                        <?php the_title(); ?>
                    </div>
               </div>
     <div class="col-md-8">
      <a href="<?php the_permalink() ?>">
       <img style="height:200px" src="<?php echo catch_that_image() ?>" />
       </a>
     </div>
              </div>
                </div>
                     <?php endwhile; else: >

  Oops, there are no posts.

 <?php endif; ?>

          </div> 

         </div>
  <?php get_sidebar(); ?>           

    </div>
   </div>
  </section> 

1 个答案:

答案 0 :(得分:0)

一位同事看了看它并让它运转起来。想我会发布解决方案。

<?php

$args = array('category_name' => 'designer-backstage',
            'post_status' => 'publish',
            'posts_per_page' => '5' );
 $designer_posts = new WP_Query($args);

 $args = array('category_name' => 'uncategorized',
            'post_status' => 'publish',
            'posts_per_page' => '5' );
 $uncategorized_posts = new WP_Query($args);

 if ($designer_posts->have_posts() && $uncategorized_posts->have_posts()) :
// If a new category is added, add it to this array
$category_posts = [$designer_posts, $uncategorized_posts];
$cat_posts_idx = 0;

$new_category_posts = [];

$post_count = 0;
$max_posts = 10;

// Alternate categories and store into a new posts array
while ($post_count < $max_posts) {
    // Iterate the post
    $category_posts[$cat_posts_idx]->the_post();

    // get_the_category() returns an array with one item in this case
    $category = get_the_category()[0];

    if ($category->slug == 'designer-backstage') { ?>

        <div class="stylists-book">
            <div class="image">
                <div class="meta-designer">
                    <div class="pro-pic"><img src="images/stylists-pro1.jpg"></div>
                        <h3>Designer<hr></h3>
                        <p><a href="#"><?php the_author(); ?></a></p>
                        <span><?php the_date(); ?></span>
                </div>
                <a href="<?php the_permalink() ?>"><img><?php the_post_thumbnail(); ?></a>
            </div>


    <?php }
    else if ($category->slug == 'uncategorized') { ?>

        <div class="look">
            <div class="row">
                <div class="col-md-4">
                    <div class="team-m">
                        <span><?php the_author(); ?></span>
                        <?php the_title(); ?>
                    </div>
                </div>
                <div class="col-md-8" style="max-height: 225px; overflow: hidden;"><a href="<?php the_permalink() ?>"><img style="" src="<?php echo catch_that_image() ?>" /></a></div>
            </div>
        </div>
 </div>

    <?php }



else:
 ?>
Oops, there are no posts.
<?php
endif;
?>