我正在尝试创建一个页面,该页面将我的自定义帖子类型的所有类别作为选项卡保存,并带有选项卡内容。
我可以将所有类别名称显示为选项卡,但我需要在每个选项卡内容区域中对相应类别进行查询。
因此,当我单击名为“1”的选项卡时,选项卡内容区域应该只显示属于名为“1”的选项卡类别的帖子。
迄今为止我的代码:
<?php
echo \'<ul class="nav nav-tabs" role="tablist">\';
$args = array(
\'hide_empty\'=> 1,
\'orderby\' => \'name\',
\'order\' => \'ASC\'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo \'<li><a href="#\' . $category->name.\'" role="tab" data-toggle="tab">\' .
$category->name.\'</a></li>\';
$cat_name = $category->name;
}
echo \'</ul>\';
echo \'<div class="tab-content">\';
foreach($categories as $category) {
echo \'<div class="tab-pane" id="\' . $category->name.\'">\';
?>
<?php
$the_query = new WP_Query(array(
\'post_type\' => \'acme_product\',
\'posts_per_page\' => 100,
\'category_name\' => $category->slug
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<h1><?php the_title(); ?></h1>
<?php
endwhile;
wp_reset_postdata();
?>
<?php }
echo \'</div>\';
?>
问题是,每个内容区域都会显示每个类别的所有帖子。
html输出:
<div class="container">
<ul class="nav nav-tabs" role="tablist"><li><a href="#Audi" role="tab" data-toggle="tab">Audi</a></li><li><a href="#Skoda" role="tab" data-toggle="tab">Skoda</a></li></ul><div class="tab-content"><div class="tab-pane" id="Audi">
<h1>fffffffffffff</h1>
<div class="tab-pane" id="Skoda">
<h1>qqqqqqqqqqqqqqqqqqq</h1>
</div>
</div>
有什么建议吗?
最合适的回答,由SO网友:deflime 整理而成
尝试\'category_name\' => $category->slug
而不是\'category_name\' => $cat_name
.
你也失踪了wp_reset_postdata();
就在之后endwhile;
.
SO网友:user206223
<?php
echo \'<ul class="nav nav-tabs" role="tablist">\';
$args = array(
\'taxonomy\' => \'annualreports_category\',
\'hide_empty\'=>0,
\'orderby\' => \'name\',
\'order\' => \'ASC\'
);
$categories = get_categories($args);
foreach($categories as $category) {
echo
\'<li>
<span data-href="\'.$category->slug.\'" role="tab" data-toggle="tab">
\'.$category->name.\'
</span>
</li>\';
}
echo \'</ul>\';
echo \'<div class="investors-content">\';
foreach($categories as $category) {
echo \'<div class="tab-pane" id="\' . $category->slug.\'">\';
$catslug = $category->slug;
$the_query = new WP_Query(array(
\'post_type\' => \'annualreports_post\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'annualreports_category\',
\'field\' => \'slug\',
\'terms\' => array( $catslug ),
\'operator\' => \'IN\'
),
),
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo \'<h1>\';
the_title();
echo \'</h1>\';
endwhile;
echo \'</div>\';
}
echo \'</div>\';
?>
jQuery
$(".investors-content .tab-pane").each(function(){
$(this).hide();
$(\'.investors-content .tab-pane:first-child()\').show();
});
$(\'.nav-tabs li>span\').on( "click", function(e) {
e.preventDefault();
var id = $(this).attr(\'data-href\');
$(".investors-content .tab-pane").each(function(){
$(this).hide();
if($(this).attr(\'id\') == id) {
$(this).show();
}
});
});