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>';
}
?>
Related
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).
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/
I'm stuck getting terms into a list and class at the same time. I can return the list no problem, but I need the slug to also be the class.
Here is my code
$terms = get_the_terms($post->ID, 'events17groups' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
endif;
$listclass = implode(" ", $tslugs_arr);
$listgroup = "<li class='$listclass'>" . implode("</li><li
class='$listclass'>", $tslugs_arr) . "</li>";
echo "<ul class='eventgroup'>$listgroup</ul>";
This produces the following HTML
<ul class="eventgroup"><li class="exhibitions home">exhibitions</li><li
class="exhibitions home">home</li></ul>
So what I want is
<ul class="eventgroup"><li class="exhibitions">exhibitions</li><li
class="home">home</li></ul>
Any suggestions?
Why not use a simple readable foreach:
$listgroup = '';
foreach ($tslugs_arr as $item) {
$listgroup .= '<li class="' . $item . '">' . $item . '</li>';
}
echo "<ul class='eventgroup'>$listgroup</ul>";
Have you tried like this?
$terms = get_the_terms($post->ID, 'events17groups' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
endif;
$listclass = implode(" ", $tslugs_arr);
$listgroup = "<li class='".tslugs_arr[0]."'>" . implode("</li><li
class='".tslugs_arr[1]."'>", $tslugs_arr) . "</li>";
echo "<ul class='eventgroup'>$listgroup</ul>";
If you want to do a fancy one-liner then try out:
<?php
$terms = get_the_terms($post->ID, 'events17groups' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
endif;
echo "<ul class='eventgroup'>".implode('', array_map( function( $tslug ){ return "<li class='$tslug'>$tslug</li>"; }, $tslugs_arr ) )."</ul>";
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>";
}; ?>
I have found a WooCommerce code snippet that adds sorting on Category pages, and it works.
But the problem is that the sorting subcategory is only displayed on the Parent category page, but not in the subcategories pages.
This is the category page:
The problem is that the sorting is not working on subcategory pages:
What do I need to change in my code to achieve that?
Here is my code:
function tutsplus_product_subcategories( $args = array()) {
$parentid = get_queried_object_id();
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms) {
echo '<ul class="wooc_sclist">';
foreach ( $terms as $term ) {
echo '<li class="category">';
echo '<h2>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}
}
add_action( 'woocommerce_before_shop_loop', 'tutsplus_product_subcategories', 50 );
Reference: Display WooCommerce Categories, Subcategories, and Products in Separate Lists
Well i found out how to make it so i will share it with you:
function sub_fillter(){
$sub = wp_get_post_terms(get_the_ID(),'product_cat');
if ( is_subcategory()){
$cat = array_shift( $sub );
$cat=get_term_top_most_parent($cat->term_id,'product_cat');
tutsplus_product_subcategories($cat);
}
}
add_action('woocommerce_before_shop_loop', 'sub_fillter', 50);
function is_subcategory (){
$cat = get_query_var('cat');
$category = get_category($cat);
$category_gt_parent="";
return ( $category_gt_parent == '0' ) ? false : true;
}
function tutsplus_product_subcategories( $cat) {
$parentid = $cat->term_id;
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms || is_subcategory()) {
echo '<ul class="wooc_sclist">';
foreach ( $terms as $term ) {
echo '<li class="category">';
echo '<h2>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}
}