Show children of custom taxonomy terms - php

I have the WP Jb manager plugin which I believe is just a custom post type for jobs. I have enabled categories created some categories.
I need to just show the children of each category instead of the whole list of categories.
I have the following code:
<?php
$terms = get_terms( 'job_listing_category', 'orderby=count&hide_empty=0' );
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
?>
Which outputs a list of all categories (parent and children) as so:
Office
Warehouse
Manufacturing
Industrial
Construction
Building Services Engineers
The parent categories are the bold ones: Office, Industrial and Construction. I want to take one of them and display the children of that category only.
For example: get_category('industrial', 'children_of') (I know that's not the correct syntax), so it would result in showing the children only of the industrial category:
Warehouse
Manufacturing
I can't seem to find a way to do this - Could anyone help?

I managed to do this by using the following code:
<?php
$terms = get_terms( 'job_listing_category', 'parent=59' );
$count = count($terms);
if ( $count > 0 ){
echo "<ul>";
foreach ( $terms as $term ) {
echo "<li>" . $term->name . "</li>";
}
echo "</ul>";
}
?>

You can fetch the parent categories and then construct a list for each subcategory like so:
<?php
$taxonomies = get_terms(array(
'taxonomy' => 'job_listing_category',
'hide_empty' => false,
'parent' => 0,
));
if (!empty($taxonomies)):
foreach ($taxonomies as $parent) {
$output = '<ul>';
$children = get_terms(array(
'taxonomy' => 'job_listing_category',
'parent' => $parent->term_id,
'hide_empty' => false,
));
foreach ($children as $child) {
$output .= '<li>' . esc_html($child->name) . '</li>';
}
$output = '</ul>';
}
echo $output;
endif;
Note that the code wasnt tested. Find more info here: https://developer.wordpress.org/reference/functions/get_terms/

Related

WooCommerce custom sidebar for category page (Identify parent cat)

I've put together a custom filter sidebar for my WooCommerce Category page.
Some info about the sidebar:
It pulls in all active product categories, allowing flexibility for the client to add more down the road.
Each item clicks through to its appropriate category page.
One parent category is hidden from the loop (id 122).
The initial loop calls in parent categories
The sub loop pulls in child categories (If there are any & if they have products).
I'm using $product_cat to identify if the current category page is the same as the cat in the sidebar, allowing a class to be added so it can be highlighted when on the page.
Q
Much like with what I've done with $product_cat, I would like to be able to identify within the loop IF the parent category on the sidebar is the parent of the child category page the user is currently on. With this in place, I can then add a class to the parent category li.
I've spent some time crawling the website in an attempt to find a solution but sadly not.
My full code below:
global $wp_query;
$cat_name = "product_cat";
$parent_cat_terms = get_terms($cat_name, array('parent' => 0, 'orderby' => 'slug', 'hide_empty' => true, 'exclude' => 122 ));
// IF Current declaration
global $wp_query;
$product_cat = $wp_query->query_vars['product_cat'];
echo '<ul class="cat-filters">';
foreach ($parent_cat_terms as $parent_term) {
if( $parent_term->count > 0 ):
echo '<li class="cat-parent';
if( $parent_term->slug == $product_cat ){
echo ' class="page-cat"';
}
echo '"><a href="' . get_term_link($parent_term) . '"';
if (strpos($parent_term->slug, $product_cat ) !== false) {
echo ' class="page-cat"';
}
echo'>' . $parent_term->name . '</a>';
$child_cat_terms = get_terms($cat_name, array('parent' => $parent_term->term_id, 'orderby' => 'slug', 'hide_empty' => true));
if ( !empty( $child_cat_terms ) && !is_wp_error( $child_cat_terms ) ){
echo '<ul class="cat-child">';
foreach ($child_cat_terms as $child_term) {
if( $child_term->count > 0 ):
echo '<li><a href="' . get_term_link($child_term) . '"';
if( $child_term->slug == $product_cat ){
echo ' class="page-cat"';
}
echo'>' . $child_term->name . '</a></li>';
endif;
}
echo "</ul>";
}
echo '</li>';
endif;
}
echo '</ul>';

Display current category first then child categories in WooCommerce

I'm trying to display in the sidebar the current page's category and it's subcategories. The title should be the current category's name and linked to the current category as well. An example of what I'm trying to achieve can be seen here in the sidebar: https://food52.com/shop/pantry
This is my current site as an example:https://farmtofrank.wpengine.com/product-category/prepared-foods/
This is the code I've created so far:
<?php
$terms = get_terms([
'taxonomy' => get_queried_object()->taxonomy,
'parent' => get_queried_object_id(),
]);
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
echo '<div>';
foreach ( $terms as $term) {
echo '<p class="filters">' . $term->name . '</p>';
}
echo '</div>';
?>
It works but it puts the parent link at the bottom of the list. How can I keep the parent link at the top above the subcategories?
I suppose that this is related to product category archive pages. In this case, you are very near, try:
$queried_object = get_queried_object();
if ( is_product_category() && is_a($queried_object, 'WP_Term') ) {
$taxonomy = $queried_object->taxonomy;
echo '<h2 class="shop__sidebar-heading">
' . $queried_object->name . '
</h2>';
$children_terms = get_terms(['taxonomy' => $taxonomy, 'parent' => $queried_object->term_id]);
if ( ! empty($children_terms) ) {
echo '<ul class="'.$queried_object->slug.' child-terms">';
// Loop through children terms
foreach ( $children_terms as $term) {
echo '<li class="filters">' . $term->name . '</li>';
}
echo '</ul>';
}
}
Tested and works. It will require some styling (CSS).

Adapting PHP code to split up a list into approximately equal comuns

I have existing PHP code to list product categories as a list of links:
<?php // as per https://phptechnologytutorials.wordpress.com/2015/11/07/get-
list-of-all-woocommerce-categories/
$orderby = 'name';
$order = 'asc';
$hide_empty = false;
$excluded = '32';
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'exclude' => $excluded,
);
$product_categories = get_terms( 'product_cat', $cat_args );
if( !empty($product_categories) ){
echo '<ul class="linked-product-category-list">';
foreach ($product_categories as $key => $category) {
echo '<li>';
echo '<a href="'.get_term_link($category).'" >';
echo $category->name;
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
?>
I found another Stack Overflow solution which demonstrates how to do what I want to achieve - to arrange list of items into 4 equally populated columns.
However, I'm new to PHP and I don't know how to adapt the solution into my own code above. Please help me to adapt my code above using the principles of the solution below to split up my list into columns.
<?php
$col = 3;
$projects = array_chunk($projects, ceil(count($projects) / $col));
foreach ($projects as $i => $project_chunk)
{
echo "<ul class='pcol{$i+1}'>";
foreach ($project_chunk as $project)
{
echo "<li>{$project->name}</li>";
};
echo "</ul>";
}; ?>

List of showing categories and sub-categories of post in Wordpress

This seems to be very simple but I don't know why the below code is not working. I have searched all over in Google, there are many solutions but not working for me. Guys, please let me know what I am missing.
My Code below:
<ul class="category-sidebar">
<?php
$get_parent_cats = array(
'parent' => '0' //get top level categories only
);
$all_categories = get_categories( $get_parent_cats );//get parent categories
foreach( $all_categories as $single_category ){
//for each category, get the ID
$catID = $single_category->cat_ID;
echo '<li>' . $single_category->name . ''; //category name & link
$get_children_cats = array(
'child_of' => $catID //get children of this parent using the catID variable from earlier
);
$categories = get_categories($args);
$child_cats = get_categories( $get_children_cats );//get children of parent category
echo '<ul class="children">';
foreach( $child_cats as $child_cat ){
//for each child category, get the ID
$childID = $child_cat->cat_ID;
//for each child category, give us the link and name
echo '' . $child_cat->name . '';
}
echo '</ul></li>';
} //end of categories logic ?>
</ul><!--end of category-sidebar-->
This is only giving me Categories but not sub-categories in them.
Please help anyone.
Thanks in advance.
Hey I found the solution:
<ul class="category-sidebar">
<?php
$get_parent_cats = array(
'parent' => '0','hide_empty' => false //get top level categories only
);
$all_categories = get_categories( $get_parent_cats );//get parent categories
foreach( $all_categories as $single_category ){
//for each category, get the ID
$catID = $single_category->cat_ID;
echo '<li>' . $single_category->name . ''; //category name & link
$get_children_cats = array(
'child_of' => $catID,'hide_empty' => false //get children of this parent using the catID variable from earlier
);
$categories = get_categories($args);
$child_cats = get_categories( $get_children_cats );//get children of parent category
echo '<ul class="children">';
foreach( $child_cats as $child_cat ){
//for each child category, get the ID
$childID = $child_cat->cat_ID;
//for each child category, give us the link and name
echo '' . $child_cat->name . '';
}
echo '</ul></li>';
} //end of categories logic ?>
</ul>
Thanks everyone for your time. :)

Get Children Terms from an Array of ID's?

I am using this code to post a list of all children terms for my taxonomy called 'location'
<?php
$termID = 10;
$taxonomyName = "location";
$termchildren = get_term_children( $termID, $taxonomyName );
echo '<ul>';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
Currently the code is using term id 10 and getting the children for that and displaying in a list.
However I want to add multiple term id's and display a list of all the children of the id's I add.
My id's are 4,6,8,10,14,18,22
Looking for some help to figure out how to do this?
Cheers
I figured out the answer to my own question. using get_terms will get all the terms retured, so there is no need to specify individual id's of my terms.
My code is
<?php
$args = array( 'taxonomy' => 'location' );
$terms = get_terms('location', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="location-archive">';
foreach ($terms as $term) {
$i++;
$term_list .= '<li><span class="feature">' . $term->name . '</li>';
if ($count != $i) $term_list .= ' ยท '; else $term_list .= '</p>';
}
echo '<ul class="list">';
echo $term_list;
echo '</ul>';
}
?>

Categories