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 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题