How can I find out the wordpress taxonomy ID?
I want to create a term exists query...
if (term_exists(array(
'term_id' => 4,
'term_taxonomy_ID' => x
)))
But I can't figure out how to find out the ID for my taxonomy called: 'file-formats'
Any help would be awesome thanks.
Josh
Did you try this ?
<?php
$terms = get_the_terms( $post->ID , 'file-formats' );
if($terms) {
foreach( $terms as $term_name ) {
echo $term_name->term_id."<br />";
}
}
?>
Good luck :)
Try this one : http://codex.wordpress.org/Function_Reference/get_taxonomies
or
http://codex.wordpress.org/Function_Reference/get_term
Try this:
<?php $terms = get_the_terms( $post->ID , 'taxonomy-name' );
if($terms) {
foreach( $terms as $term ) {
echo $term->term_id.'<br />';
}
}
?>
Related
Im trying to display the current post category name for a custom post types' taxonomy, but exclude one of the categories. I see different solutions in StackOverflow, but I can't seem to get any of them to work for me. Below is the code I am using, which works great, but I don't know how to exclude one ID using it.
<?php $terms = get_the_terms( $post->ID , 'press_category' );
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, 'press_category' );
if( is_wp_error( $term_link ) )
continue;
echo $term->name ;
}
?>
Try this
<?php $terms = get_the_terms($post->ID, 'press_category');
foreach ($terms as $term)
{
$term_link = get_term_link($term, 'press_category');
if (is_wp_error($term_link)) continue;
if ($term->term_id != 222)
{
echo $term->name;
}
}
?>
Change 222 in this line to your taxonomy id for exclude
$term->term_id != 222
How to show category hierarchy in woocommerce
What i want is something like this
given with this slug
domain.com/category/balloons/helium-baloons
the hierarchy would be:
Baloons
Helium Baloons
Decor
Weddings
To simplified the idea it should be something like this
you can use wp_list_categories function with arguments
wp_list_categories(
array(
'taxonomy'=>product_cat'
)
);
use the below code to achive this functionality
$myterms = get_terms( 'category', array( 'parent' => 0 ) );
$current_term = get_queried_object();
echo '<ul>';
foreach($myterms as $term){
echo '<li> '.$term->name.'';
if($term->term_id == $current_term->parent){
$child_terms = get_terms( 'category', array('child_of'=>$term->term_id) );
if($child_terms){
echo '<ul>';
foreach($child_terms as $child_term){
echo '<li> '.$child_term->name.'';
}
echo '</ul>';
}
}
echo '</li>';
}
echo '</ul>';
also if you have big child heirarchi you can make this code recursive.
I'm trying to display the taxonomy term of a custom post type (like using <?php the_category( ' ' ); ?> in regular posts). The code below works but need to specify the taxonomy name, is there a way to use only the Post ID?
<?php
$terms = get_the_terms( $post->ID , 'taxonomy_name' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
Thanks in advance!
<?php print the_terms( $post->ID, 'taxonomy_name' , ' ' ); ?>
You cannot get the custom taxonomy terms without taxonomy name but the above code its shorter for you.
I found a way to do it. Using "get_post_taxonomies" and selecting the array witch cat[1]
<?php
$cat = get_post_taxonomies($post);
$terms = get_the_terms( $post->ID , $cat[1] );
// Loop
if ( $terms != null ){
foreach( $terms as $term ) {
$term_link = get_term_link( $term, $cat[1] );
// Print the name and URL
echo '' . $term->name . '';
unset($term); } }
?>
Try this.,
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "names"));
print_r($term_list);
You must use the taxonomy without taxonomy can not get term details.
Thanks you!
as i mentioned in the title, i want to show the category name & its description in the header file of my theme.
i tried to do with several functions which i got from search like i tried below, i have added this in my functions.php, but that not works
function sk_show_product_category_description() {
if (is_singular( 'product' )) {
global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo '<div class="widget-background-wrapper"><div class="widget product-cat-description"><h4 class="widget-title">Note</h4>' . $term->description . '</div></div>';
}
}
lastly i tried by including class WC_Product, but that not works too, i have below mentioned the code which i used for it
global $woocommerce, $post, $WC_Product;
$file =$woocommerce->plugin_path.'/classes/abstracts/abstract-wc-product.php';
$getWooClass = include_once($file);
$test = $getWooClass->get_categories(122);
var_dump($test);
Please guide how can i display the category name of the current product and & its description??
Try this,
global $post;
$args = array( 'taxonomy' => 'product_cat',);
$terms = wp_get_post_terms($post->ID,'product_cat', $args);
$count = count($terms);
if ($count > 0) {
foreach ($terms as $term) {
echo '<div style="direction:rtl;">';
echo $term->description;
echo '</div>';
}
}
for more
Hope its helps..
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>";
}```