[英]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.