循环使用PHP和WordPress按类别组织和显示自定义帖子

时间:2019-05-27 16:45:20

标签: php wordpress bootstrap-4

我正在开发一个页面,该页面要求我以手风琴式下拉菜单显示自定义帖子类型的类别列表。类别名称将充当手风琴标题,其内容将作为与每个特定类别相关的职位。下图总结了我最终将完成的工作。

Bootstrap Accordion drop-down for categories mock-up

我已经能够成功检索类别名称并将其分配给手风琴下拉列表,但是发生的是,即使两个帖子都与相似的类别名称相关联,我的代码仍在添加新的单元格。

Bootstrap Accordion drop-down duplicating categories

Arrrrrg,我感觉我很亲近!这是到目前为止我的代码的摘要。

<div id="accordion" class="col-8" role="tablist" aria-multiselectable="true">
              <?php 
                    $args = array(
                        'post_type' => 'our_work',
                        'posts_per_page' => -1,
                        'orderby' => 'category',
                        'hide_empty' => 0,
                    );  
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();
                ?>
        <div class="card box-shadow">

            <div class="card-header" role="tab" id="<?php the_ID(); ?>">
                <h5 class="mb-0">
                    <a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php the_ID(); ?>"
                        aria-expanded="false" aria-controls="collapseOne">
                        <?php
                          foreach((get_the_category()) as $category) { 
                              echo $category->cat_name . ' '; 
                          }
                        ?>
                    </a>
                </h5>
            </div>

            <div id="collapse<?php the_ID(); ?>" style="transition: all 0.5s ease 0s;" class="collapse nomnom"
                role="tabpanel" aria-labelledby="heading<?php the_ID(); ?>">
                <div class="card-block">
                    <h1><?php the_title(); ?></h1>
                    <p><?php the_Content(); ?></p>
                </div>
            </div>

        </div>
        <?php endwhile; wp_reset_query(); ?>
    </div>

我怀疑这是因为我没有正确设置循环,因此添加了一个新单元格。

我对使用“ WordPress循环”还很陌生,因此任何建议将不胜感激!!!

1 个答案:

答案 0 :(得分:2)

已编辑为改用echo。这应该更清洁,并希望能正常工作(未试用)

  <?php 
   $args = array(
      'post_type' => 'our_work',
      'posts_per_page' => -1,
      'orderby' => 'category',
      'hide_empty' => 0,
   );  
  $loop = new WP_Query( $args );
  $cat = '';//set a variable to catch category
  $first = 0;
  echo '<div id="accordion" class="col-8" role="tablist" aria-multiselectable="true">';
  while ( $loop->have_posts() ) : $loop->the_post();
    $post_cat = '';
    foreach(( get_the_category() ) as $category) { 
      $post_cat = $category->cat_name . ' '; 
    }
    if($first == 0){
      $first = 1;
      $cat = $post_cat;
      echo '<div class="card box-shadow">';
      echo'<div class="card-header" role="tab" id="'.get_the_id().'">';
      echo'<h5 class="mb-0">';
      echo'<a data-toggle="collapse" data-parent="#accordion" href="#collapse'.get_the_id().'"aria-expanded="false" aria-controls="collapseOne">'.$post_cat.'</a>';
      echo'</h5>';
      echo'</div>';
      //start collapse pannel
      echo'<div id="collapse'.get_the_id().'" style="transition: all 0.5s ease 0s;" class="collapse nomnom" role="tabpanel" aria-labelledby="heading'.get_the_id().'">';
    }
    if($cat != $post_cat){
      $cat = $post_cat;
      echo'</div>';//close collapse
      echo'</div>';//close box shadow
      echo '<div class="card box-shadow">';
      echo'<div class="card-header" role="tab" id="'.get_the_id().'">';
      echo'<h5 class="mb-0">';
      echo'<a data-toggle="collapse" data-parent="#accordion" href="#collapse'.get_the_id().'"aria-expanded="false" aria-controls="collapseOne">'.$post_cat.'</a>';
      echo'</h5>';
      echo'</div>';
      //start collapse pannel
      echo'<div id="collapse'.get_the_id().'" style="transition: all 0.5s ease 0s;" class="collapse nomnom" role="tabpanel" aria-labelledby="heading'.get_the_id().'">';
    }
    echo'<div class="card-block">';
    echo'<h1>'.get_the_title().'</h1>';
    echo'<p>'.get_the_content().'</p>';
    echo'</div>';  
    if (($loop->current_post +1) == ($loop->post_count)) { 
      echo '</div>';//close collapse 
      echo '</div>';//box shadow close
    } 
  endwhile; 
  wp_reset_query();
  echo '</div>';//close accordian close
    ?>