doumengjing1500 2015-10-14 02:36
浏览 32
已采纳

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

I'm trying to get 2 different styled posts to show up one after the other and then back around. So it should look like
DB Uncategorized DB Uncategorized etc

And to have them both styled differently. I'm not great with PHP and the best I got so far was all in one category and then all in the other.

<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条回答 默认 最新

  • doudou348131346 2015-10-20 20:21
    关注

    A co-worker took a look at it and got it working. Figured I would post the solution.

    <?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;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?