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',
); ?>
Related
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>
how to remove or(exclude) current page category(single_cat_title) from wp_list_categories?
I want to remove the current page category from my category list
<?php
$cat = single_cat_title( '', false );
function text_replace( $output ) {
$output = str_replace( '$cat', '', $output );
return $output;
}
add_filter('wp_list_categories', 'text_replace'); ?>
<?php
wp_list_categories( array(
'child_of' => 1208,
'current_category' => 0,
'depth' => 0,
'echo' => 1,
'hide_empty' => 1,
'hide_title_if_empty' => false,
'hierarchical' => true,
'order' => 'DESC',
'orderby' => 'count',
'show_count' => 0,
'show_option_none' => __( 'No categories' ),
'style' => 'list',
'taxonomy' => 'category',
'title_li' => 0,
'use_desc_for_title' => 0,
) );
?>
You can use the 'exclude' parameter to exclude certain categories. You could get the terms for the post, put them in an array, and pass that to your wp_list_categories query.
<?php
$terms_to_exlude = array();
$terms = get_the_terms( get_the_ID(), 'category' );
if ($terms) {
foreach ($terms as $term) {
$terms_to_exclude[] = $term->term_id;
}
} else {
$terms_to_exclude = '';
}
wp_list_categories( array(
'child_of' => 1208,
'current_category' => 0,
'depth' => 0,
'echo' => 1,
'exclude' => $terms_to_exclude,
'hide_empty' => 1,
'hide_title_if_empty' => false,
'hierarchical' => true,
'order' => 'DESC',
'orderby' => 'count',
'show_count' => 0,
'show_option_none' => __( 'No categories' ),
'style' => 'list',
'taxonomy' => 'category',
'title_li' => 0,
'use_desc_for_title' => 0,
) );
?>
fix the problem :
<?php
function text_replace($output) {
$current_category = single_cat_title("", false);
$cat = array("$current_category");
$output = str_replace($cat, '', $output);
return $output;
}
add_filter('wp_list_categories', 'text_replace');
?>
<?php wp_list_categories( array(
'child_of' => 1208,
'current_category' => 0,
'depth' => 0,
'echo' => 1,
'hide_empty' => 1,
'hide_title_if_empty' => false,
'hierarchical' => true,
'order' => 'DESC',
'orderby' => 'count',
'show_count' => 0,
'show_option_none' => __( 'No categories' ),
'style' => 'list',
'taxonomy' => 'category',
'title_li' => 0,
'use_desc_for_title' => 0,
) );
?>
How can I get all pages that meet the criteria?
get_page_by_title()
The above only brings the first one. Is it possible for get_page_by_title to return an array of pages?
Here is the code to loop all pages with title:
<?php
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages( $args);
foreach ($pages as $page_data) {
$title = $page_data->post_title;
echo $title;
}
?>
You can change args to filter pages.
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');
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);