Get taxonomy terms without taxonomy name - wordpress

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!

Related

Get a Value in the Author Page from a Custom Taxonomy

I have created a Custom Taxonomy called nationality, then I added it to let the user chose his/her nationality through a Custom Account Edit Form using ACF.
I am trying to get the Tax value into the Author.php page but it returns as Array ,
This is my code which I am using:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = wp_get_post_terms($post->ID, 'nationality' , $curauth);
if ($terms){
$out = array(
'name'=>'<span>Nationality: </span>'
);
foreach ($terms as $term)
{
$out[] = '<a " ' . ' href="' . esc_url(get_term_link( $term->slug, 'nationality')) .'">' .$term->name .'</a>' ;}
echo join( ' ', $out );
}
?>
I have also tried the following code:
<?php
$nationality = get_field('nationality', $curauth);
$nationality = get_field_object('nationality', $curauth);
echo $nationality['value'];
?>
still giving me an Array.
The Field type is select and Return Value is set to Term Object either Term ID
the error then is
Object of class WP_Term could not be converted to string
any Ideas how to make this Correct.
Thank you!
Ahmad
are you trying to get the custom taxonomy from the current user? try like this:
get_the_terms( get_the_ID(), 'nationality' )
and if you are sure it will always get only one (take into account some people have more than one nationality) just access the first element:
get_the_terms( get_the_ID(), 'nationality' )[0]
I have the right Code here :
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = get_field('nationality', $curauth);
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo $term->name ; ?>
<?php endforeach; ?>
<?php endif; ?>

Display the category names for the current post, but exclude one ID

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 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 . '';
}

Getting the Product Attribute Term Permalink in WordPress / WooCommerce

I'm using WooCommerce with WordPress to build a store of Antique Photos.
I'm using the WooCommerce product attributes feature to store information about an item photographer.
See here for an example, photographer attribute is pulled out in the box on the left http://bit.ly/1Dp999O
The code that pulls out the attribute looks like this:
if($attribute['is_taxonomy']) {
$values=wc_get_product_terms($product->id, $attribute['name'], array('fields' => 'names'));
echo apply_filters('woocommerce_attribute', wpautop(wptexturize(implode(', ', $values))), $attribute, $values);
}
$values looks like this:
Array ( [0] => Photographer 1 )
The problem is, how do I get to the permalink for the Photographer which is automatically generated by WordPress and WooCommerce:
http://bit.ly/1JtwBna
I can't find any documentation for this within WooCommerce, and this seems to be a taxonomy within a taxonomy which goes a bit further than stabdard WordPress, but I'm assuming this is a fairly standard requirement. Any pointers appreciated.
Once you have the attribute term (in this case the photographer's name) you can use get_term_link() to get the URL. Because the $product id isn't passed to the woocommerce_attribute folder, I couldn't filter that, but instead create an override of the product-attributes.php template. And modified the relevant section to be the following:
if ( $attribute['is_taxonomy'] ) {
$terms = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'all' ) );
$html = '';
$counter = 1;
$total = count( $terms );
foreach( $terms as $term ){
$html .= sprintf( '%s',
esc_url( get_term_link( $term ) ),
esc_attr( $term->name ),
wptexturize( $term->name )
);
if( $counter < $total ){
$html .= ', ';
}
$counter++;
}
echo wpautop( $html );
}
For some reason, the URL isn't a pretty permalink. It's late and I can't tell if this has to do with my configuration or what exactly, but it is a start.
This basically does what I require:
$terms = get_the_terms($product->id, $attribute['name']);
foreach ($terms as $term){
echo ''.$term->name.'';
}
But could do with a bit of refinement for sure.

How to find out a wordpress taxonomy ID? ...not term ID

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 />';
}
}
?>

Resources