Wordpress custom taxonomy with hierachical layout for a specific post - wordpress

I am trying to pull a posts custom taxonomy into the page. The categories have subcategories and I am struggling to workout how to get the subcategories listed in hierachical way. Currently I am using the below which is showing the categories properly, but they are coming in all jumbled.
$terms = get_the_terms( $post_id, 'listing_category' );
if ( $terms && ! is_wp_error( $terms ) ) :
$categories = array();
foreach ( $terms as $term ) {
$categories[] = $term->name;
}
$categories_list = join( ", ", $categories ); ?>
<tr>
<td><strong>Categories: </strong></td>
<td><?php esc_html_e( $categories_list ) ?></td>
</tr>
<?php endif; ?>
I have attempted numerous options but they all seem to be showing all of the categories and subcategories within that custom taxonomy, not only the categories relating to this post.
Any help or guidance would greatly appreciated.

you should get parent taxonomy and then for every parent get their children
with a loop(foreach).
1- Find the parent taxonomies by this query:
$args = array (
'taxonomy' => 'listing_category',
'parent' => 0,
'hide_empty' => false
);
$allTaxParent = get_categories($args);
2- Now create a function for get children from parent:
function getChildren( $termId=0, $taxonomy='category' ) {
$children = get_categories(
[
'child_of' => $termId,
'taxonomy' => $taxonomy,
'hide_empty' => false
]
);
return ($children);
}
3- Now you have taxonomies in hierachical way:
foreach ( $allTaxParent as $tax ) {
$children = getChildren($tax->term_id, $tax->taxonomy);
echo $tax->name . '>';
foreach ($children as $child){
echo $child->name . '>';
}
echo '<br>';
}

Related

Echo category name if checked in a list of posts

I am trying to get all categories that are attached to a custom post type in Wordpress.
I have a custom post type named doc_pipeline and a custom taxonomy named tax_pipeline. I have been searching for hours on this and I can't put the right functions together to make this happen. To repeat I need to get a category name that is attached to the custom post type only if it is checked. Should be fairly simple, but I am quite lost.
$args = array(
'post_type' => 'doc_pipeline',
'taxonomy' => 'tax_pipeline',
'posts_per_page' => -1
);
$categories = get_categories( $args );
$posts = get_posts($args);
foreach($posts as $post) {
var_dump(is_object_in_term( $post->ID, 'tax_pipeline', 'doc_pipeline'));
}
To get the category name of the post by post id you should use like this.
you already have post ID so, you don't have to pass post type.
$cat_array = wp_get_post_terms($post->ID, 'tax_pipeline', array('fields'=>'names'));
and
// To Remove dublicate category from loop, try this.
$unique_terms = array(); // instead of $dupe = 0;
while ( $wp_query->have_posts() ) : $wp_query->the_post();
global $post;
//Ok, Then you have to use like this in your loop.
$cat_array = wp_get_post_terms( $post->ID, 'tax_pipeline', array( 'fields' => 'all' ) );
foreach( $cat_array as $term ) :
if( ! in_array( $term->term_id, $unique_terms ) ):
array_push( $unique_terms, $term->term_id );
echo $term->name;
endif;
endforeach;
endwhile;

Get Terms Count including Sub taxonomy - Wordpress

I want to count the number of posts in each taxonomy. The below code only works if there is on 2nd level taxonomy. Based on the image. It's returning BMW Post count 2 but for Mercedez, it should return Post Count 4. Adding Model 1 & Model 2 post count.
$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_terms($taxonomy_name,array('parent' => $term_id));
foreach ( $termchildren as $child ) { ?>
<h3>
<?php echo $child->name;?>
<span class="blog_bg_blue">Post Count: <?php echo $child->count; ?></span>
</h3>
<?php} ?>
Are you looking for something like this?
Script:
$args = array(
'orderby' => 'slug',
'parent' => 0
);
$categories = get_categories( $args );
foreach( $categories as $category ){
print $category->name.' - '.$category->category_count.'</br>';
}
Post arrangement in CMS:
Output:

Taxonomy list of specific post type

How to get list of custom taxonomy using custom post type name.
let say there is one post type called "products" and in that there are list of categories(ie. shirt, tshirt, jeans etc.)
So I want that categories list using post type name "products".
you can get post list by taxonomy in taxonomy-{post_type}.php In this file by default gt list of post for specified taxonomy.
REF : Custom Post Type taxonomy page still showing all posts
OR in Custom templete,
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'<br>';
endwhile;
}
}
You could use something like this:
$terms = get_terms( 'products' );
$count = count( $terms );
if ( $count > 0 ) {
echo '<h3>Total products: '. $count . '</h3>';
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>'.$term->name.'</li>';
}
echo '</ul>';
}
get_term_link() will give you a link to your term as well.

Woocomerce shop page to show category name & display products within that category

I'm developing a website with Woocommerce plugin installed. I want my shop page to show the category name first & then display the products within that category, then next category name with the products it has.
already searched in google & here, found a solution, but it is not what I want. it added the category name after the price area of every product. I'm not a pro, so having a difficult time to solve this problem.
function wc_category_title_archive_products(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<small class="product_category_title"><?php echo $single_cat->name; ?></small>
<?php }
}
add_action( 'woocommerce_after_shop_loop_item', 'wc_category_title_archive_products', 5 );
<?php
$cat_args = array(
'parent' => '0',
'taxonomy' => 'product_cat'
);
$categories = get_categories( $cat_args );
foreach ($categories as $category) {
echo $category->cat_name;
echo do_shortcode('[product_category category="'.$category->cat_name.'" per_page="12" columns="4" orderby="date" order="DESC"]');
}
?>
SHOW PRODUCT CATEGORY NAME ABOVE TITLE IN SHOP PAGE ==> Check on Woocomerce Version 3.5.3 You need to just copy it and paste it to your theme functions.php
function wpa89819_wc_single_product(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
<?php }
}
add_action( 'woocommerce_after_shop_loop_item_title', 'wpa89819_wc_single_product', 5 );
You're going to have to do a new loop/query for each category. We can automate some of the process by using WordPress's get_categories function. Here's an example::
<?php
$cat_args = array(
'parent' => '0',
'taxonomy' => 'product_cat'
);
$categories = get_categories( $cat_args );
foreach ($categories as $category) { ?>
<ul class="product-category">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'cat' => $category->cat_ID, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<!-- Your output -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
}
?>

Wordpress Custom Post category archive

I have a custom post type set up, with a number of categories and sub categories.
What I am trying to do is create a page that shows all the posts in a specific category, with a menu that lists all the category sub categories so the posts can then be filtered.
I have tried copying the archive template and renaming it taxonomy-(my-custom-taxonomy).php which then if I go to the slug shows certain posts, and using
<?php wp_list_categories(); ?> but I just want a list of all the sub-categories of a specific category, and to filter those posts. I am struggling to show these and use one template for all categories and children.
You can use
$term_id = get_queried_object()->term_id;
and
$tax= get_query_var( 'taxonomy' )
to return the details of the current term and taxonomy being viewed in your taxonomy.php page.
You can then use that info with get_term_children to get the child terms of the current term being displayed. For examples, see the link provided
EDIT
Your code should look like this
<?php
$term_id = get_queried_object()->term_id;
$taxonomy_name = get_query_var( 'taxonomy' );
$termchildren = get_term_children( $term_id, $taxonomy_name );
foreach ( $termchildren as $child ) {
echo '<ul>';
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => $term->slug,
),
),
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<p>' . get_the_title() . '</p>';
}
wp_reset_postdata();
echo '</ul>';
}
?>

Resources