Php 在两列中显示其类别下的帖子
我已经创建了一个成员目录页面,并试图在其类别名称下显示一个成员列表,按字母顺序分为两列(div.members-colA和div.members-colB)。成员应填充div.members-colA,直到四舍五入的post计数达到1/2,此时post应填充div.members-colB。我的代码没有以这种方式显示内容,我被难住了。显然是一个PHP新手,所以任何帮助都将不胜感激!!您可以在这里查看开发服务器上的页面Php 在两列中显示其类别下的帖子,php,wordpress,Php,Wordpress,我已经创建了一个成员目录页面,并试图在其类别名称下显示一个成员列表,按字母顺序分为两列(div.members-colA和div.members-colB)。成员应填充div.members-colA,直到四舍五入的post计数达到1/2,此时post应填充div.members-colB。我的代码没有以这种方式显示内容,我被难住了。显然是一个PHP新手,所以任何帮助都将不胜感激!!您可以在这里查看开发服务器上的页面 CSS: .会员可乐{ 宽度:49%; 浮动:左; } .柯尔布议员{ 宽度:
CSS:
.会员可乐{
宽度:49%;
浮动:左;
}
.柯尔布议员{
宽度:49%;
浮动:对;
}
.委员会成员{
宽度:100%;
}
PHP:
CSS:
PHP:
感谢您的快速回复!我将列出大约70个成员(以及更多在成为成员时动态显示的成员)。我希望它们均匀地填充这两列(而它们的类别“标题”是按字母顺序排列的)。这给了你足够的信息吗?我希望这些列以两个偶数的列显示(每个列的内容区域宽度为49%,彼此相邻浮动),与这里看到的开发阶段类似,我仍然有一个问题,我需要类别标题只列出一次,而该类别内的职位列在他们下面。因此,类别标题和帖子填充在两个div中,这两个div占其父类的49%,并向左浮动。任何更多的帮助都将不胜感激!我不知道数据库的结构,但您需要在For循环中移动我给您的代码。您需要查询所有类别,然后使用for循环。在For循环中,您必须创建一个不同的查询,该查询只获取与特定类别相关的帖子。您将在For循环的开头显示类别标题。这两个答案可能对DB查询有所帮助:-关于第二个链接,我建议使用Wordpress函数来查询数据库,而不是构建您自己的函数。
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 -->
.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
$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>