2015-10-14 35 views
0

我試圖讓2個不同風格的職位一個接一個出現,然後回來。因此,它應該像
DB 未歸類 DB 未歸類 等2 WP_Querys重複一個接一個 - 風格不同

而且有他們兩個風格不同。我對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> 

回答

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; 
?> 
相關問題