Wordpress categories hierarchy - wordpress

I can't seem to find out why this code doesn't output the categories in hierarchy:
<ul>
<?php
$args = array(
'show_option_all' => '',
'container' => false,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'use_desc_for_title' => 0,
'child_of' => 0,
'hierarchical' => 1,
'number' => null,
'echo' => 1,
'depth' => -1,
'taxonomy' => 'category'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<li>' . $category->name . '(' . $category->count . ')</li>';
}
?>
</ul>
Instead all the list items are outputting as parents like this...
<ul>
<li>Media(1)</li>
<li>Movies(1)</li>
<li>TV Shows(1)</li>
<li>Uncategorised(1)</li>
</ul>
...but they should be like this...
<ul>
<li>Media(1)
<ul>
<li>Movies(1)</li>
<li>TV Shows(1)</li>
</ul>
</li>
<li>Uncategorised(1)</li>
</ul>
As you can see 'hierarchical' is set to 1, but it doesn't work as expected.
PS: I can't use the standard wp_list_categories method (http://codex.wordpress.org/Template_Tags/wp_list_categories) because I will need to be able to customise the markup in the list.
Any suggestions will be helpful.

You can use following code:
<ul>
<?php
$args = array(
'show_option_all' => '',
'container' => false,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'use_desc_for_title' => 0,
'child_of' => 0,
'hierarchical' => 1,
'number' => null,
'echo' => 1,
'depth' => -1,
'taxonomy' => 'category'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
if($category->parent==0)
echo '<li>' . $category->name . '(' . $category->count . ')</li>';
else
echo '<ul>' . $category->name . '(' . $category->count . ')</li></ul>';
}
?>
</ul>
UPDATE
$args = array(
'hide_empty' => 0,
'echo' => 1,
'taxonomy' => 'category',
'hierarchical' =>1,
'show_count' => 1,
);
function add_class_wp_list_categories($wp_list_categories) {
$pattern = '/<li class="/is';
$replacement = '<li class="first ';
return preg_replace($pattern, $replacement, $wp_list_categories);
}
add_filter('wp_list_categories','add_class_wp_list_categories');
echo wp_list_categories( $args );

Use 'orderby' => 'parent'
$args = array(
'taxonomy' => 'your_tax',
'orderby' => 'parent'
);
$cats = get_categories($args);

Related

How to get child categories from parent category of custom post type and display in dropdown?

Finally, I can get subcategories from a custom post-type category and everything works fine. The problem is when I want to get the same but with a dropdown for responsive. Is possible? I need to hide empty terms too. thanks!
here the code:
<ul>
<?php $tax = get_term_by('slug', 'cursos', 'portfolio_category');
$tax_id = $tax->term_id;
$args = array(
'child_of' => $tax_id,
'taxonomy' => 'portfolio_category',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 0,
'title_li' => '',
'hide_title_if_empty' => 0
);
wp_list_categories($args); ?></ul>
You can use get_categories to get portfolio_category. Try the below code.
<ul class="show_on_desktop">
<?php
$tax = get_term_by('slug', 'cursos', 'portfolio_category');
$tax_id = $tax->term_id;
$args = array(
'child_of' => $tax_id,
'taxonomy' => 'portfolio_category',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 0,
'title_li' => '',
'hide_empty' => true
);
wp_list_categories($args);
?>
</ul>
<?php
$cats = get_categories( array(
'child_of' => $tax_id,
'taxonomy' => 'portfolio_category',
'hide_empty' => true
) );
?>
<select class="show_on_mobile">
<?php foreach ( $cats as $cat ) : ?>
<option value="<?php echo get_term_link( $cat->term_id ); ?>"><?php echo $cat->name; ?></option>
<?php endforeach; ?>
</select>

Wordpress - taxonomy dropdown is not working with hierarchical

Any hope to make this Taxonomy dropdown to work with hierarchical.
I added 'hierarchical' => 1 but it seems doesn't work for me!
<?php
if( $terms = get_terms([ 'taxonomy' => 'category', 'hierarchical' => 1, 'hide_empty' => false, 'child_of' => 233 ]) ) :
echo '<select name="categoryfilter4"><option>Downloads...</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
use wp_dropdown_categories function
https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
<?php wp_dropdown_categories( ['name'=>'categoryfilter4', 'show_option_none' => 'Downloads...','hierarchical' => 1, 'hide_empty' => false, 'child_of' => 233, 'order_by' => 'parent'] ); ?>
<?php wp_dropdown_categories( $args ); ?>
<?php $args = array(
'show_option_all' => '',
'show_option_none' => '',
'option_none_value' => '-1',
'orderby' => 'ID',
'order' => 'ASC',
'show_count' => 0,
'hide_empty' => 1,
'child_of' => 0,
'exclude' => '',
'include' => '',
'echo' => 1,
'selected' => 0,
'hierarchical' => 0,
'name' => 'cat',
'id' => '',
'class' => 'postform',
'depth' => 0,
'tab_index' => 0,
'taxonomy' => 'category',
'hide_if_empty' => false,
'value_field' => 'term_id',
); ?>

Woocommerce How to show all product catgories and there subcategories?

I am working on product import using csv file. I need to show the list of all products first from csv and then need to show all categories from woocommerce. All categories and there subcategories are coming but i need to show them in multi check select box in hierarical order e.g
Clothes
-- Jeans
------ Blue
------ Black
-- T-shirt
Jwellery
-- Diamond
------ Chain
------ Ring
-- Gold
Phone
But all these are coming in single line. I am not able to find which is parent and child category.This is my code.Please help.
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<ul id="test" class="test">';
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
if($sc->category_parent == 0)
{
echo '<li><input type="checkbox" name="product_cat[]" value="'.$sc->name.'"/>'.$sc->name.'</li>';
}else
{
echo '<li> <input type="checkbox" name="product_cat[]" value="'.$sc->name.'"/>'.$sc->name.'</li>';
}
}
echo '</ul>';
try this code brother
$args = array(
'taxonomy' => 'product_cat',
'parent' => false, // get top level categories
'orderby' => 'name',
//'order' => 'ASC',
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => false,
);
$categories = get_categories( $args );
echo "<ul>";
foreach ( $categories as $category ){
$link=get_category_link( $category->term_id );
echo "<label><input type=checkbox value=". $category->name . " name=". $category->name . ">". $category->name ."" ."</label>";
$sub_args = array(
'taxonomy' => 'product_cat',
'parent' => $category->term_id, // get child categories
'orderby' => 'name',
//'order' => 'ASC',
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => false,
);
$sub_categories = get_categories( $sub_args );
?>
<ul>
<?php
foreach ( $sub_categories as $sub_category ){
$links=get_category_link( $sub_category->term_id );
?>
<li> <?php
echo '<label>'. $sub_category->name .''. '</label>';
?>
</li>
<?php
////////////////////////
$sub_arg = array(
'taxonomy' => 'product_cat',
'child_of' => $sub_category->term_id, // get child categories
'orderby' => 'name',
//'order' => 'ASC',
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => false,
);
$su_categories = get_categories( $sub_arg );
?>
<ul>
<?php
foreach ( $su_categories as $su_category ){
$cat_link=get_category_link( $su_category->term_id );
?>
<li> <?php
echo '<label>'. $su_category->name .''. '</label>';
?>
</li>
<?php
}
//////////////////////////////
}
?>
</ul>
<?php
}
echo "</ul>";
Use below code
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br />'. $cat->name .'';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
Use This shortcode in your Place:-[woocommerce_nested_categories1]
Add this code in your function.php:-
function woocommerce_nested_categories() {
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
echo'<div class="outer-nested-woo-catg"><ul>';
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><li>'. $cat->name .'</li>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
echo'<ul>';
foreach($sub_cats as $sub_category) {
echo '<li>'.$sub_category->name.'</li>' ;
$args3 = array(
'taxonomy' => $taxonomy,
'child_of' => 1,
'parent' => $sub_category->term_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats2= get_categories( $args3 );
if($sub_cats2){
echo'<ul>';
foreach($sub_cats2 as $sub_category2) {
echo '<li>'.$sub_category2->name.'</li>' ;
$args4 = array(
'taxonomy' => $taxonomy,
'child_of' => 1,
'parent' => $sub_category2->term_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats3= get_categories( $args4 );
if($sub_cats3){
echo'<ul>';
foreach($sub_cats3 as $sub_category3) {
echo '<li>'.$sub_category3->name.'</li>' ;
$args5 = array(
'taxonomy' => $taxonomy,
'child_of' => 1,
'parent' => $sub_category3->term_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats4= get_categories( $args5 );
if($sub_cats4) {
echo'<ul>';
foreach($sub_cats4 as $sub_category4) {
echo '<li>'.$sub_category4->name.'</li>' ;
}
echo'</ul>';
}
}
echo'</ul>';
}
}
echo'</ul>';
}
}
echo'</ul>';
}
}
}
echo'</ul></div>';
}
add_shortcode('woocommerce_nested_categories1', 'woocommerce_nested_categories');

How to List all Child pages in wordpress?

I have a parent page A with with child pages X, Y, Z, D
I want to display link to all child pages on page X and Y
When you go to page X and Y, you should see a list of all child pages X, Y, Z, D
For some reason I get only the child page (page D)
I am using this function.
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
//'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => A,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
if ( $pages)
{
foreach($pages as $page)
{
$page = '<ul> <li> ' .$page->post_title . ' </li></ul>';
}
return $page;
}
What am I doing wrong?
In the example, you are failing to close your if statement. Also you can use echo with your foreach instead of return. I cleaned up your code a little bit.
<ul>
<?php
$args = array(
'sort_order' => 'asc',
'child_of' => '1',
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
if ($pages) {
foreach ($pages as $page) :
echo ' <li> ' . $page->post_title . ' </li>';
endforeach;
}
?>
</ul>
I fixed it and it worked
I modified my foreach
$links = '';
foreach($pages as $page) {
$links .= '<ul> <li> ' .$page->post_title . ' </li></ul>';
}
return $links;
Before I used
$links = instead of $links .=

whats wrong with the use of this wordpress function: wp_list_categories()

Cant see where I am going wrong with this, any help please?
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
//then set the args for wp_list_categories
$args = array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 0,
'hierarchical' => true,
'depth' => 1,
'title_li' => __(''),
'echo' => '0'
);
$terms_list = wp_list_categories( $args);
if( $terms_list ){
?>
<div id="terms_list">
<ol>
<?php echo $terms_list; ?>
</ol>
</div>
<?php
};
// this line is just here for debug to confirm that we have the right item - it is not a part of the design or normal functionality
echo '<h1>terms_id: ' . $current_term->term_id . ' name: ' . $current_term->name . ' and: ' . $current_term->taxonomy . '</h1>';
?>
so the function causing the problem is wp_list_categories() and it is returning nothing at all. I've checked the term is correct and it does have posts, and it does have children which also have posts. Andy help appreciated!
$current_term->term_id is of type string. The child_of parameter expects an integer.
Try doing this:
$args = array(
'child_of' => (int) $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 0,
'hierarchical' => true,
'depth' => 1,
'title_li' => __(''),
'echo' => '0'
);

Resources