在两列中显示其类别下的帖子

时间:2014-05-07 14:28:32

标签: php wordpress

我创建了一个会员目录页面,我试图在两个列(div.members-colA和div.members-colB)中按字母顺序显示其类别名称下的成员列表。成员应填充div.members-colA,直到圆形帖子计数达到1/2,此时帖子应填充div.members-colB。我的代码没有以这种方式显示内容,我很难过。显然是一个PHP新手所以任何帮助将非常感谢!!您可以在此处查看开发服务器上的页面。 http://www.airwaysdigital.com/sauganash/member-directory-2/

CSS:
.members-colA{
width:49%;
float:left;
}
.members-colB{
width:49%;
float:right;
}
.members-colC{
width:100%;
}


PHP:
<div class="members-colA">

               <?php
                //get all categories then display all posts in each term
                $taxonomy = 'category';
                $param_type = 'category__in';
                $term_args = array(
                  'orderby' => 'name',
                  'order' => 'ASC'
                );
                $terms = get_terms($taxonomy, $term_args);
                if ($terms) {
                  foreach( $terms as $term ) {
                    $args=array(
                      "$param_type" => array($term->term_id),
                      'post_type' => 'members',
                      'post_status' => 'publish',
                      'posts_per_page' => -1,
                      'caller_get_posts'=> 1,
                      'orderby' => 'title',
                      'order' => 'ASC'
                      );
                    $my_query = null;
                    $my_query = new WP_Query($args); 
                    $i = 0;
                    if( $my_query->have_posts() ) { 
                        if ($i == 0) ?>
                        <h3 class="member-category"><?php echo $term->name; ?></h3>
                        <?php 
                            while ($my_query->have_posts()) : $my_query->the_post(); 
                                echo '<div class="members-colC">'; ?>
                                <p class="member-cat bold"><?php the_title(); ?></p>
                                <?php the_content(); 
                                echo '</div>';
                                if ($i == round($my_query->post_count / 2)) echo '</div><div class="members-colB">';
                                if ($i == round($my_query->post_count)) echo '</div>';
                                $i++;                            
                            endwhile;
                    }
                  }
                } ?>
                <?php wp_reset_postdata(); ?>

               </div><!-- end .members-colB -->

1 个答案:

答案 0 :(得分:0)

CSS:

.members-column {width:49%;}
.members-wrapper {width:100%; clear:both; float:none;}

/**
 * @info Float
 */
.float-left {float:left;}
.float-right {float:right;}

/**
 * @info Clearfix: clear all the floated elements
 */
.clearfix:after {
    visibility:hidden;
    display:block;
    font-size:0;
    content:" ";
    clear:both;
    height:0;
}
.clearfix {display:inline-table;}

/**
 * @hack Display the Clearfix as a block element
 * @hackfor Every browser except IE for Macintosh
 */
/* Hides from IE-mac \*/
* html .clearfix {height:1%;}
.clearfix {display:block;}
/* End hide from IE-mac */

PHP:

<?php 
$iterator = 0;
$column1_html = '';
$column2_html = '';
while ($my_query->have_posts()) : $my_query->the_post();
$title = the_title('','', false);
$html_str = '<p class="member-cat bold">' . $title . '</p>';
if($iterator % 2){
   // write in one column
   $column1_html .= $html_str;
} else {
   // write in the other column
   $column2_html .= $html_str;
}
$iterator++;                        
endwhile;
?>
<div class="members-wrapper">
<div class="members-column float-left"><?php echo htmlspecialchars($column1_html); ?></div>
<div class="members-column float-right"><?php echo htmlspecialchars($column2_html); ?></div>
<div class="clearfix"></div>
</div>