I am using the following code to get taxonomy slug:
<?php
$terms = get_the_terms( $post->ID, 'locations' );
if ( !empty( $terms ) ){
$term = array_shift( $terms );
}
?>
I am then using the following code to output the slug:
<?php echo $term->slug; ?>
My question is, how can I use this to output two different taxonomies in the same location? For example:
<?php
$terms = get_the_terms( $post->ID, 'locations', 'status' );
if ( !empty( $terms ) ){
$term = array_shift( $terms );
}
?>
I thought I could perhaps add terms 'location' , 'status' but it doesn't work.
If you want to display two or more taxonomy then i think you should loop the $terms variable.
<?php
$terms = get_the_terms( $post->ID, 'locations' );
if ( !empty( $terms ) ){
foreach ($terms as $term):
echo $term->slug;
endforeach;
}
?>
Hope its helps you.
Thank You
According to the official documentation for get_the_terms, only one taxonomy can be supplied. If you wanted to output the slugs of all the terms within two different taxonomies, you can do as Mohammad suggested, but twice.
i.e.
<?php
// output all slugs for the locations taxonomy
$locations_terms = get_the_terms( $post->ID, 'locations' );
if ( ! empty( $locations_terms ) ) {
foreach ( $locations_terms as $term ) {
echo $term->slug;
}
}
// output all slugs for the status taxonomy
$status_terms = get_the_terms( $post->ID, 'status' );
if ( ! empty( $status_terms ) ) {
foreach ( $status_terms as $term ) {
echo $term->slug;
}
}
?>
However, if you only care to get the slug of an individual term in each of the taxonomies, you may find get_term_by more useful.
i.e.
<?php
$loc_field = 'name';
$loc_field_value = 'special location';
$loc_taxonomy = 'locations';
$locations_term = get_term_by( $loc_field, $loc_field_value, $loc_taxonomy );
echo $locations_term->slug;
$stat_field = 'name';
$stat_field_value = 'special status';
$stat_taxonomy = 'status';
$status_term = get_term_by( $stat_field, $stat_field_value, $stat_taxonomy );
echo $status_term->slug;
?>
Related
How to get woocommerce tag slug names to an array correctly? I'm using the following code but it doesn't output anything.
<?php
$terms = get_the_terms( $post->ID, 'product_tag' );
$sluglist = array();
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$sluglist[] = $term->slug;
}
}
echo count($sluglist);
?>
You could use wp_get_post_terms() WordPress function instead this way to get the term slugs in an array with one line of code
$term_slugs = wp_get_post_terms( get_the_id(), 'product_tag', array( 'fields' => 'slugs' ) );
// The term slugs count
echo count($term_slugs);
// Testing: The raw output (preformatted)
echo '<pre>'; print_r($term_slugs); echo '</pre>';
I need a help with a WordPress code.
On this page https://developer.wordpress.org/reference/functions/get_the_terms/ I got a code that returns my taxonomies and their terms.
But I'd like a certain taxonomy called "Ad Type" to be excluded from this list.
I managed to exclude some terms from "Ad Type" using this code...
// An array of IDs to ignore / exclude
$ excluded_ids = array (1, 2, 3, 4);
foreach ($ terms as $ term) {
// Only proceed if the term_id is NOT in the $ excluded_ids array
if (! in_array ($ term-> term_id, $ excluded_ids)) {
$ out. = '<li> <a href="' .get_term_link($term-> slug, $ taxonomy).' '>'. $ term-> name. '</a> </ li>';
}
}
But what I want is that the "Ad Type" taxonomy does not appear at all.
Because with the code above the Title of "Ad Type" is still appearing on the terms list retrieved.
What i want is to totally exlcude this taxonomy from being retrieved on that part of my wordpress template.
Here is the code:
function wpdocs_custom_taxonomies_terms_links() {
// Get post by post ID.
$post = get_post( $post->ID );
// Get post type by post.
$post_type = $post->post_type;
// Get post type taxonomies.
$taxonomies = get_object_taxonomies( $post_type, 'objects' );
$out = array();
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
}
return implode( '', $out );
}
And here is how i retrieve it on my template page:
<?php echo wpdocs_custom_taxonomies_terms_links(); ?>
Is there a way to do this?
can anybody help me?
Thanks
I don't know if I am comparing it right but you can check if current taxonomy is not Ad Type only then get terms
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
if ($taxonomy_slug !== 'ad_type') :
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
endif;
}
You can exclude the desired term(s) by adding if ($term->term_id == 30) continue; as shown below, where 30 and 31 are the IDs of excluded terms.
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// Get the terms related to post.
$terms = get_the_terms( $post->ID, $taxonomy_slug );
if ( ! empty( $terms ) ) {
$out[] = "<h6>" . $taxonomy->label . ":</h6>\n<ul>";
foreach ( $terms as $term ) {
//Excluded terms, change the IDs as you wish
if ($term->term_id == 30) continue;
if ($term->term_id == 31) continue;
$out[] = sprintf( '<li>%2$s</li> - ',
esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
esc_html( $term->name )
);
}
$out[] = "</ul>\n";
}
}
return implode( '', $out );
}
It is about WooCommerce single product pages. I'm trying to use the product category to display the related products. I'm able to display it with the code below. Using this will include the current post and it displays the product only.
<?php
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term ) {
$product_cat_name = $term->name;
break;
}
$ids = array();
$currentID = get_the_ID();
$args = array('post_type' => 'product', 'product_cat' => $product_cat_name);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
$ids[] = $loop->post->ID;
endwhile;
wp_reset_query();
print_r($ids);
?>
But I'm trying to prevent the current product to be displayed on that related products. I have tried to use the first second of the code below, but instead of excluding, it retrieves all the default posts.
<?php
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term ) {
$product_cat_name = $term->name;
break;
}
$ids = array();
$currentID = get_the_ID();
$args = array('post_type' => 'product', 'product_cat' => $product_cat_name, 'post__not_in' => array($currentID));
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
$ids[] = $loop->post->ID;
endwhile;
wp_reset_query();
print_r($ids);
?>
How can I achieve this?
Thanks
Based on your first code snippet, this should work and will avoid to display your current product in the related products based on the current product category.
This is the code:
<?php
global $post;
$ids = array();
// Get the "main" product category
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ){
if($term->parent != 0) {
$product_cat_name = $term->name;
break; // stop the loop
}
// The Query
$loop = new WP_Query( array(
'post_type' => 'product',
'product_cat' => $product_cat_name,
'post__not_in' => array($post->ID) // Avoid displaying current product
) );
if ( $loop->have_posts() ):
while ( $loop->have_posts() ) : $loop->the_post();
$ids[] = $loop->post->ID; // Set all other product IDs for that product category
endwhile;
endif;
wp_reset_query();
// Raw output
print_r($ids);
?>
This should work…
I try to get a slug name of the tags of my product. Like this
$args = array( 'post_type' => 'product');
$list_tags = [];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$tag = get_the_term_list($post->ID, 'product_tag', '', ',' );
array_push($list_tags, $tag );
endwhile;
return $list_tags;
I obtain a list of my tags but I want the slug of this tags.
Any idea?
$args = array( 'post_type' => 'product');
$list_tags = array()
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$terms = get_the_terms( $post->ID, 'product_tag' );;
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$list_tags = $term->slug;
array_push($list_tags, $terms );
}
}
endwhile;
return $list_tags;
Based on: Woocommerce Get product tags in array
I've created a taxonomy.php page in my WordPress theme folder. I would like to get the current term id for a function.
How can I get this?
get_query_var('taxonomy') only returns the term slug, I want the ID
Nevermind! I found it :)
get_queried_object()->term_id;
Simple and easy!
get_queried_object_id()
Here's the whole code snippet needed:
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
Use following code
This will print your current taxonomy name and description(optional)
<?php
$tax = $wp_query->get_queried_object();
echo ''. $tax->name . '';
echo "<br>";
echo ''. $tax->description .'';
?>
If you are in taxonomy page.
That's how you get all details about the taxonomy.
get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
This is how you get the taxonomy id
$termId = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) )->term_id;
But if you are in post page (taxomony -> child)
$terms = wp_get_object_terms( get_queried_object_id(), 'taxonomy-name');
$term_id = $terms[0]->term_id;
<?php
$terms = get_the_terms( $post->ID, 'taxonomy');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0];
?>
See wp_get_post_terms(), you'd do something like so:
global $post;
$terms = wp_get_post_terms( $post->ID, 'YOUR_TAXONOMY_NAME',array('fields' => 'ids') );
print_r($terms);
It's the term slug you want.Looks like you can get the id like this if that's what you need:
function get_term_link( $term, $taxonomy = '' ) {
global $wp_rewrite;
if ( !is_object($term) ) {
if ( is_int( $term ) ) {
$term = get_term( $term, $taxonomy );
} else {
$term = get_term_by( 'slug', $term, $taxonomy );
}
}
This works on all page types not just taxonomy term archives
$category_id = get_the_category( get_the_ID());
$id = get_category($category_id[0]->term_id);