Get Third Level Categories WooCommerce - wordpress

I am Trying To get Third Level WooCommerce Categories Here Is My Code:
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' =>18,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<div class="wooc_sclist">';
foreach ($subcats as $sc) {
$link = get_term_link($sc->slug, $sc->taxonomy);
echo '<ul>' . $sc->name . '</ul>';
}
echo '</div>';
wp_reset_query();

i Solved it
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $number = 16,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
echo '<div class="wooc_sclist">';
foreach ($subcats as $sc) {
$link = get_term_link($sc->slug, $sc->taxonomy);
echo '<ul>' . $sc->name . ''.$sc->term_id;
$args2 = get_terms('product_cat',array(
'child_of' => $sc->term_id,
'hierarchical' => 1,
'hide_empty' => 1,
));
foreach ($args2 as $subsubCats) {
$SubLinks = get_term_link($subsubCats->slug, $subsubCats->taxonomy);
echo '<li>sub - <a href=' . $SubLinks . '>' . $subsubCats->name . '</a></li>';
}
echo '</ul>';
}
echo '</div>';
wp_reset_query();

Related

Getting Subcategories of specific category and display subcategories in the li tag

I use this code to display the main categories of my woocommerce store Now, I want to display the subcategories of that category using the category name
my code
<?php
function woocommerce_subcats_from_parentcat_by_NAME($parent_cat_NAME) {
$IDbyNAME = get_term_by('name', $parent_cat_NAME, 'product_cat');
$product_cat_ID = $IDbyNAME->term_id;
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $product_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($args);
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo '<div class="col-md-4" title="'.$sc->name.'">
'.$sc->name.'</div>
<ul class="section-header-ul">
<li><i class="fas fa-caret-left"></i>لبتاب </li>
</ul>
</div>';
}
}
?>
<?php woocommerce_subcats_from_parentcat_by_NAME("electronic_components"); ?>
<?php
$taxonomyName = "com_category";
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false));
foreach ($parent_terms as $pterm) {
$terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
foreach ($terms as $term) {
echo '<div class="single_cat col-md-3">';
echo '<h3>'.$pterm->name.'</h3>';
echo "<ul>";
echo '<li>' . $term->name . '</li>';
echo "</ul>";
echo '</div>';
}
}
?>

Get parent category link

I am trying to get the parent category link - this is my code but it doesnt seem to be working.
<?php
$taxonomyName = "equipment_categories";
$parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => false));
echo '<div class="subcats"><a class="subcatseach" href="' . $cat = get_the_category(); $cat = $cat[0]; echo get_category_link($cat->cat_ID) . '">All Equipment</a>';
foreach ($parent_terms as $pterm) {
$terms = get_terms($taxonomyName, array('parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false));
foreach ($terms as $term) {
echo '<a class="subcatseach" href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a>';
}
}
echo '</div>';
?>
Any suggestions?
try using 'child_of' => $pterm->term_id instead of 'parent' => $pterm->term_id in line 6

I want to show all subcategories under category or taxonomy in Wordpress

Here is my code. It works fine but it only shows maximum 5 subcategories under category. There are 10 to 20+ subcategories for every category. Now what is fault in my code?
I want to print all subcategories under every category. How?
<?php
$args = array(
'taxonomy' => 'custom_taxo',
'parent' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'hide_empty' => 1,
'pad_counts' =>0
);
$categories = get_categories( $args );
foreach ( $categories as $category ){
echo "<li class='biz-cat'>";
echo '<label><input type="checkbox" id="type-'. $category->name . '" rel="'. $category->name . '">' . $category->name . '</label>';
$sub_args = array(
'taxonomy' => 'custom_taxo',
'parent' => $category->term_id,
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'hide_empty' => 1,
'pad_counts' => 0
);
$sub_categories = get_categories( $sub_args );
echo "<ul class='children'>";
foreach ( $sub_categories as $sub_category ){
echo "<li>";
echo '<label><input type="checkbox" id="type-'. $sub_category->name . '" rel="'. $sub_category->name . '"> '. $sub_category->name . '</label>';
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
When i change 'hide_empty' => true
Then it works fine.
'hide_empty' => 0,

Wordpress: hierarchical list of taxonomy terms

I am hitting a wall here, although it sounds pretty simple: I want to return a hierarchical list of custom post type taxonomy terms. What I get is the first level of terms and nested uls. But the sub terms are not showing. Any ideas?
Here's the code:
function return_terms_index() {
$taxonomies = array(
'taxonomy_name',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'all',
'parent' => 0,
'hierarchical' => true,
'child_of' => 0,
'pad_counts' => false,
'cache_domain' => 'core'
);
$terms = get_terms($taxonomies, $args);
$return .= '<ul>';
foreach ( $terms as $term ) {
// return terms (working)
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$term->term_id,
$term->name,
$term->description
);
$subterms = get_terms( array(
'parent' => $term->term_id,
'hide_empty' => false
));
$return .= '<ul>';
foreach ( $subterms as $subterm ) {
//return sub terms (not working :( )
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$subterm->term_id,
$subterm->name,
$subterm->description
);
$return .= '</li>'; //end subterms li
}
$return .= '</ul>'; //end subterms ul
$return .= '</li>'; //end terms li
} //end foreach term
$return .= '</ul>';
return $return;
}
Thanks!
Edit: here's the output.
<ul>
<li id="category-176">
1. <span class="post-count">0</span><span class="cat-description" style="display: none;">Description</span>
<ul id="subTerm-176" style="display: block;"></ul>
</li>
<li id="category-49">
2. <span class="post-count">0</span><span class="cat-description" style="display: none;">Langtitel/Beschreibung</span>
<ul id="subTerm-49" style="display: none;"></ul>
</li>
</ul>
Edit: taxonomies are returned in hierarchical list now, YAY!
But I want to query and display posts of third level taxonomy terms as well and this bit of code doesn't do the trick.
$post_query = new WP_Query($taxonomies, array(
'term' => $subsubterm->term_id
)); ?>
<?php if ( $post_query->have_posts() ) :
$return .= '<ul>';
while ( $post_query->have_posts() ) : $post_query->the_post();
$return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
endwhile;
$return .= '</ul>';
wp_reset_postdata();
else:
endif;
It has to be dynamic, so I can't specify a term by name/slug. But is this even possible?
'term' => $subsubterm->term_id
Thanks again.
You have missed to pass $taxonomies in
$subterms = get_terms($taxonomies, array(
'parent' => $term->term_id,
'hide_empty' => false
));
Try following code
function return_terms_index() {
$taxonomies = array(
'taxonomy_name',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'all',
'parent' => 0,
'hierarchical' => true,
'child_of' => 0,
'pad_counts' => false,
'cache_domain' => 'core'
);
$terms = get_terms($taxonomies, $args);
$return .= '<ul>';
foreach ( $terms as $term ) {
// return terms (working)
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$term->term_id,
$term->name,
$term->description
);
$subterms = get_terms($taxonomies, array(
'parent' => $term->term_id,
'hide_empty' => false
));
$return .= '<ul>';
foreach ( $subterms as $subterm ) {
//return sub terms (not working :( )
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$subterm->term_id,
$subterm->name,
$subterm->description
);
$return .= '</li>'; //end subterms li
}
$return .= '</ul>'; //end subterms ul
$return .= '</li>'; //end terms li
} //end foreach term
$return .= '</ul>';
return $return;
}
Just use the wp_list_categories() function :
https://developer.wordpress.org/reference/functions/wp_list_categories/
$tax_args = array(
'taxonomy' => 'my_custom_taxonomy',
'orderby' => 'name',
'show_count' => 1,
'hierarchical' => 1,
'title_li' => 'My list title'
);
wp_list_categories($tax_args);

Wordpress list pages from parent

I need help listing all pages when on a third level (grandchild) Page.
E.g I have Page 1(grandparent) Page 2(Parent) Page 3(Child)
I need to show all these pages listed the same on all three pages such as:
Page1
Page2
Page3
I have successfully shown the right list of pages on page 1 and 2. (see below)
Can anyone please help
function widget($args, $instance) {
global $post;
extract( $args );
if($post->post_parent == 0) {
$title = ''.$post->post_title.'';
$id_to_query = $post->ID;
}
elseif($post->ancestors) {
$page = get_page($post->ancestors[0]);
$title = ''.$page->post_title.'';
$id_to_query = $post->ancestors[0];
} else {
$page = get_page($post->post_parent);
$title = ''.$page->post_title.'';
$id_to_query = $page->ID;
}
$children = get_pages('post_type='.get_post_type().'&child_of='.$id_to_query);
if(empty($children) || is_page( array(17,125) ) ) return; // excludes contact us etc...
wp_reset_query();
$widget_title = $title;
echo $before_widget;
echo $before_title . $widget_title . $after_title; ?>
<ul>
<?php wp_list_pages('title_li=&post_type='.get_post_type().'&child_of='.$id_to_query); ?>
</ul>
<?php
echo $after_widget;
wp_reset_postdata();
Try getting the top ancestor id with $topid = get_top_ancestor($postid); then using the arguement 'child_of' => $topid and be mindful of your depth
Full example:
<?php
$postid = get_the_ID();
$topid = get_top_ancestor($postid);
$args = array(
'depth' => 2,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => $topid,
'exclude' => '',
'include' => '',
'echo' => 1,
'authors' => '',
'sort_order' => 'ASC',
'link_before' => '',
'link_after' => '',
'walker' => '',
'post_type' => 'page',
'post_status' => 'publish'
); ?>
<ul><?php wp_list_pages( $args ); ?></ul>
Something for you to work on [Untested]:
function widget($args, $instance) {
global $post;
$postid = get_the_ID();
$topid = get_top_ancestor($postid);
$args = array(
'depth' => 2,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => $topid,
'exclude' => '',
'include' => '',
'echo' => 1,
'authors' => '',
'sort_order' => 'ASC',
'link_before' => '',
'link_after' => '',
'walker' => '',
'post_type' => 'page',
'post_status' => 'publish'
);
// if(empty($children) || is_page( array(17,125) ) ) return; // excludes contact us etc...
// wp_reset_query();
$widget_title = $title;
echo $before_widget;
echo $before_title . $widget_title . $after_title; ?>
<ul>
<?php wp_list_pages( $args ); ?>
</ul>
<?php
echo $after_widget;
wp_reset_postdata();
}

Resources