[英]Show categories in H3 then show sub-categories in a list
好。 我嘗試了另一個問題的代碼:
<?php
$subcategories = get_categories('&child_of=4&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
但是沒有用。 由於我將在H3中顯示主要類別,因此我以非動態方式進行了操作。 是的,看起來很多PHP。 並且:它不起作用。
<h3>Automação</h3>
<?php
$subcategories = get_categories('&child_of=13&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
<h3>Balanças</h3>
<?php
$subcategories = get_categories('&child_of=34&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
<h3>Caixas Registradoras</h3>
<?php
$subcategories = get_categories('&child_of=42&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
<h3>Gavetas de Dinheiro</h3>
<?php
$subcategories = get_categories('&child_of=45&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
<h3>Impressoras</h3>
<?php
$subcategories = get_categories('&child_of=49&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
<h3>Informática</h3>
<?php
$subcategories = get_categories('&child_of=57&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
<h3>Leitores</h3>
<?php
$subcategories = get_categories('&child_of=61&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
<h3>Marcas</h3>
<?php
$subcategories = get_categories('&child_of=70&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
echo '<ul>';
foreach ($subcategories as $subcategory) {
echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
}
echo '</ul>';
?>
有沒有辦法在H3中顯示主要類別,在單向PHP中顯示子類別? 因為當我放置許多PHP時,我知道性能和安全性問題。
例:
H3> AUTOMACAO
UL
LI> SUB-CATEGORY
LI> SUB-CATEGORY
我相信我得到了您想要的。 基本上,我創建的是獲取所有類別ID的列表,並過濾數組中的父類別,然后根據您的格式進行顯示。
<?php //Get list of parent category IDs and put it inside of $parent_categories array
$category_ids = get_all_category_ids();
$parent_categories = array();
foreach($category_ids as $cat_id) {
$category = get_category($cat_id);
if ($category->parent == 0) {
$parent_categories[] = $category->cat_ID;
}
}
?>
<?php //Display each parent <h3>category</h3> and <ul>subcategories</ul>
foreach ($parent_categories as $pcat) :
$category = get_category($pcat);
?>
<h3><?php echo ucfirst($category->name); ?> </h3>
<?php /* if you want to show the full link you can use this instead
<?php
$parentArgs = array(
'title_li' => '',
'include' => $pcat
);
?>
<h3><?php wp_list_categories($parentArgs); ?></h3>
*/ ?>
<ul>
<?php
$childArgs = array(
'title_li' => '', //You could put the parent category title in here if you want
'child_of' =>$category->cat_ID
);
wp_list_categories($childArgs);
?>
</ul>
<?php endforeach; ?>
要求的其他信息:
您可以根據分類顯示,這是一個示例。
<?php //Get list of parent category IDs and put it inside of $parent_categories array
$category_ids = get_all_category_ids();
$parent_categories = array();
foreach($category_ids as $cat_id) {
$category = get_category($cat_id);
if ($category->parent == 0) {
$parent_categories[] = $category->cat_ID;
}
}
?>
<?php //Display each parent <h3>category</h3> and <ul>subcategories</ul>
foreach ($parent_categories as $pcat) :
$category = get_category($pcat);
if ($category->taxonomy == 'category') : //You change the 'category' to your taxonomy.
?>
<h3><?php echo ucfirst($category->name); ?> </h3>
<ul>
<?php
$childArgs = array(
'title_li' => '', //You could put the parent category title in here if you want
'child_of' =>$category->cat_ID
);
wp_list_categories($childArgs);
?>
</ul>
<?php endif; ?>
<?php endforeach; ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.