Ok. I tried the code from another question:
<?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>';
?>
But didn't worked. Since I'll display the main categories in a H3, I did in a non-dynamic way. So yeah, It will look a LOT of PHP. And also: it's not working.
<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>';
?>
There is any way to display the main category in a H3 and the sub-categories in a one-way PHP? Because I know the problems of performance and security when I place A LOT of PHP.
Example:
H3> AUTOMACAO
UL
LI> SUB-CATEGORY
LI> SUB-CATEGORY
I believe I got what you are looking for. Basically what I created was obtaining a list of all category IDs and filtering the parent categories in an array then going through and displaying it based on your formatting.
<?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; ?>
ADDITIONAL INFORMATION REQUESTED:
You could display based on the taxonomy here is an example.
<?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; ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.