[英]Loop through categories and create tab for each Wordpress and Bootstrap 3
我正在嘗試創建一個頁面,其中包含我的自定義帖子類型的所有類別作為標簽,帶有標簽內容。
我能夠將所有類別名稱顯示為選項卡,但我需要在每個選項卡內容區域中運行查詢到相應的類別。
因此,當我單擊名為“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' => $cat_name
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<h1><?php the_title(); ?></h1>
<?php
endwhile;
?>
<?php }
echo '</div>';
?>
問題是每個內容區域顯示每個類別的所有帖子。
我想要實現的目標: 鏈接
有什么建議么?
'category_name'參數將value作為類別slug。 因此,您應該在查詢中使用category slug代替類別名稱。
$cat_slug = $category->slug;
$the_query = new WP_Query(array(
'post_type' => 'acme_product',
'posts_per_page' => 100,
'category_name' => $cat_slug
));
我在前一次問過這個問題,但經過一些嘗試解決我的問題后,我通過這個完美的鱈魚解決了它,與我一起工作100%100
<?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->slug.'" role="tab" data-toggle="tab">
'.$category->name.'
</a>
</li>';
}
echo '</ul>';
echo '<div class="tab-content">';
foreach($categories as $category) {
echo '<div class="tab-pane" id="' . $category->slug.'">';
$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();
echo '<h1>';
the_title();
echo '</h1>';
endwhile;
}
echo '</div>';
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.