List custom taxonomies children links - wordpress

I was wondering if you could help me display a taxonomies child links, for example, I have a custom post type of "courses" with a custom taxonomy of "study_type" and categories like "security", "professional" etc. Currently I am correctly displaying the category title and description. I need to know how to get a permalink for the courses that fall into the categories and display them underneath the category description. Thanks for any help or advice you have to offer. Here is me code:
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$taxonomy = $type;
$terms = get_terms( $taxonomy, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
}
}
}
?>

use get_term_children
<?php
//list terms in taxonomy
$types[0] = 'study_type';
foreach ($types as $type) {
$terms = get_terms( $type, '' );
if ($terms) {
foreach($terms as $term) {
echo '<h2>'.$term->name.'</h2>';
echo '<p>' . $term->description . '</p>';
//This is where the links should be
$termchildren = get_term_children( $term->term_id, $type );
if($termchildren){
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
}
}
} ?>

Related

Wordpress - Display category on single page

I create a custom post type with ACF.
I have two different taxonomy for this custom type :
Project type
House type
In House type, i have 4 different category :
all types
House
Appartement
Building
Each post need 'all type' and one other (for example : all type, house)
i would like to display the main category : House and not display : all types
How can i hide 'all types' ?
My code :
<div class="detailfiche">
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'housetype');
if ($terms) {
$output = array();
foreach ($terms as $term) {
$output[] = '<span>' .$term->name .'</span>';
}
echo join( ' ', $output );
};
?>
</div>
Try this
<?php
global $post;
$terms = wp_get_post_terms($post->ID, 'housetype');
if ($terms)
{
$output = array();
foreach ($terms as $term)
{
if ($term->term_id != 222)
{
$output[] = '<span>' . $term->name . '</span>';
}
}
echo join(' ', $output);
};
?>
Change 222 in this line to your taxonomy id for exclude
$term->term_id != 222

How to display taxonomy list with link to taxonomy page on the single custom post type page

I need to list the taxonomies that apply to this post. If the taxonomy only one then this code works well:
global $post;
$terms = wp_get_post_terms($post->ID, 'gintype');
if($terms){
$output = array();
foreach($terms as $term){
$outputnk[] = get_term_link( $term->slug, 'gintype');
$outputme[] = $term->name;
}
}
But if a post has several taxonomy items this code does not work.
This code is working for me:
global $post;
$terms = wp_get_post_terms($post->ID, 'gintype');
foreach($terms as $term){
$term_link = get_term_link( $term );
echo '<div>' . $term->name . '';
}

Sub Category of Current Category in Wordpress

I am currently using the code below in my category.php file to showe a list of the subcategories of the current category.
<?php
$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;
$termchildren = get_term_children( $term_id, $taxonomy_name );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $term, $taxonomy_name ) . '">' .
$term->name . '</a></li>';
}
echo '</ul>';
?>
This is working fine for this application however I would like to amend it now to only show the next level down. Only the direct subcategories for the current category.
Thanks
Richard
Welcome to stackoverflow Richard,
You can get direct child categories with below code
$cat = get_query_var('cat');
$child_categories= get_categories('hide_empty=0&parent='.$cat);
Thanks

Display Post Terms and add link

I've created this shortcode to display my terms (custom taxonomy) on specific post (custom post types) :
// First we create a function
function list_terms_forme_juridique_taxonomy() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'forme_juridique',array('fields'
=> 'names') );
ob_start();
if( count( $terms) > 0) {
echo '<ul>';
echo '<li>' . implode( '</li><li>', $terms) . '</li>';
echo '</ul>';
}
return ob_get_clean();
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);
I am trying to add a link (url) on my terms in order to redirect to the term page but I didn't succeed yet.
Any help ?
Use get_term_link()function for this:
// First we create a function
function list_terms_forme_juridique_taxonomy() {
global $post;
$terms = wp_get_post_terms($post->ID, 'forme_juridique');
ob_start();
if( count( $terms) > 0) {
echo '<ul>';
foreach($terms as $term){
$term_link = get_term_link($term, 'forme_juridique');
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
}
return ob_get_clean();
}
// Add a shortcode that executes our function
add_shortcode( 'forme_juridique', 'list_terms_forme_juridique_taxonomy'
);

Woocommerce category description

I have next code:
<?php
global $post;
$args = array( 'taxonomy' => 'product_cat');
$terms = get_the_terms($category->slug,'product_cat', $args);
$count = count($terms);
if ($count > 0) {
foreach ($terms as $term) {
echo '<div style="direction:rtl;">';
echo $term->description;
echo '</div>';
}
}
?>
The code will display category description. The problem - on sub-category it will display the sub-category description + the parent description.
How i can display the description separate: in parent - the parent description, and on sub - only the sub description?
Try this and let me know if it helped you
add_action( 'woocommerce_after_subcategory_title', 'custom_add_product_description',
12);
function custom_add_product_description ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
$description= $prod_term->description;
echo '<div>'.$description.'</div>';
}
The answer:
<?php
global $post;
$terms = get_the_terms( 'product_cat',$post->ID);
echo '<div style="direction:rtl;">';
echo category_description( get_category_by_slug($terms)->term_id);
echo '</div>';
?>
You can use this code to display the product category description -
<?php global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description; ?>
Anajanas answer works very well, It can also be modified to do what I needed
which was to display a ACF custom field of a woocoommerce product child category on the shop catgegory page.
This is my code to add in functions.php
12);
function xyz ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
echo "<div class='designercatname'>".get_field('designer_name', $prod_term )."</div>";
}```

Resources