我正在尝试创建类别选项卡,以显示所选类别选项卡中的帖子。我已经用我的方式尝试过了,但我每个类别只能得到一个帖子,而它应该返回该类别中的所有帖子。我正在粘贴下面的代码,有人能帮我解决这个问题吗?
<ul id="download-tab" class="nav nav-tabs resp-tabs-list">
<?php
$args = array(
\'orderby\' => \'name\',
\'parent\' => 13
);
$categories = get_categories( $args );
$i=0;
foreach ( $categories as $category ) {
$i++;
echo \'<li><a id="#\'.strtolower(str_replace(\' \', \'\', $category->name)).\'" href="dtab\'.$i.\'">\' . $category->name . \'</a></li>\';
}
?>
</ul>
<div class="tab-content resp-tabs-container" id="download-content">
<?php
$pargs = array(
\'orderby\' => \'name\',
\'parent\' => 13
);
$categories_loop = get_categories( $pargs );
foreach ( $categories_loop as $category_loop ) {
query_posts(\'category_name=\'.$category_loop->slug.\'&posts_per_page=-1&order=desc\'); $j=0;
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post();
$j++ ?>
<div class="tab-pane" id="dtab<?php echo $j ?>">
<table class="table table-hover">
<tr>
<th>DATE</th>
<th>TITLE</th>
<th>SIZE</th>
<th></th>
</tr>
<?php
$file_size = rwmb_meta(\'file_size\');
$file_url = rwmb_meta( \'file_url\');
?>
<tr>
<td><?php the_time(\'d/m/Y\'); ?></td>
<td><?php the_title(); ?></td>
<td><?php echo $file_size; ?></td>
<td><a href="<?php echo $file_url ?>" class="button3 download_btn" target="_blank">DOWNLOAD</a></td>
</tr>
</table>
</div>
<?php
endwhile; ?>
<div style="width:45%; float:left; font-weight:bold; margin-top:20px;"><?php //previous_posts_link(\'PREV\'); ?></div>
<div style="width:45%; float:right; text-align:right; font-weight:bold; margin-top:20px;"><?php //next_posts_link(\'NEXT\',$download_query->max_num_pages); ?></div>
<?php wp_reset_query();?>
<?php else: ?>
<p><?php _e( \'Sorry, no content found.\' ); ?></p>
<?php endif;
} ?>
最合适的回答,由SO网友:helgatheviking 整理而成
我不知道为什么你只收到了1个回复。您的查询参数看起来是正确的,即使正如我在注释中提到的,您应该使用WP_Query
. 因此,如果我不得不猜测,我会说你的循环html标记被关闭了?并且可能没有正确打印结果。我已将您的代码修改为只有1个get_categories()
和类别循环。我不知道这种方式是否或多或少“可读”,但它似乎对我有用。
$args = array(
\'orderby\' => \'name\',
\'parent\' => 13
);
$categories = get_categories( $args );
$tabs = \'\';
$panes = \'\';
if( $categories ){
$tabs .= \'<ul id="download-tab" class="nav nav-tabs resp-tabs-list">\';
$panes .= \'<div class="tab-content resp-tabs-container" id="download-content">\';
$i=0;
foreach ( $categories as $category ) {
$i++;
$tabs .= \'<li><a id="#\'.$category->slug.\'" href="dtab\'.$i.\'">\' . $category->name . \'</a></li>\';
$post_args = array( \'category_name\' => $category->slug,
\'posts_per_page\' => -1,
\'order\' => \'desc\' );
$category_posts = new WP_Query( $post_args );
$panes .= \'<div class="tab-pane" id="dtab\' .$i . \'">\';
if ( $category_posts->have_posts() ) :
$panes .= \'<table class="table table-hover">
<tr>
<th>DATE</th>
<th>TITLE</th>
<th>SIZE</th>
<th></th>
</tr>\';
while ( $category_posts->have_posts() ) : $category_posts->the_post();
$file_size = function_exists( \'rwmb_meta\' ) ? rwmb_meta(\'file_size\') : \'\';
$file_url = function_exists( \'rwmb_meta\' ) ? rwmb_meta( \'file_url\') : \'\';
$panes .= \'<tr>
<td>\'. get_the_time(\'d/m/Y\') .\'</td>
<td>\'. get_the_title() .\'</td>
<td>\'. $file_size .\'</td>
<td><a href="\'. $file_url .\'" class="button3 download_btn" target="_blank">DOWNLOAD</a></td>
</tr>\';
endwhile;
$panes .= \'</table>\';
else:
$panes .= \'<p>\' . __( \'Sorry, no content found.\' ) . \'</p>\';
endif;
$panes .= \'</div><!--#dtab\' .$i . \'-->\';
}
$panes .= \'</div><!--#download-content-->\';
$tabs .= \'</ul><!--#download-tab-->\';
}
// print it all out
echo $tabs;
echo $panes;