Hi Guys, I'm trying to display a taxonomy field called clpr_store_video in my Wordpress sidebar for each store is it possible to call the field value outside the loop on the sidebar, but yet attached to the post id?
I don't really know if its possible or how to do that.
EDITED
What I've tried from another tutorial but no luck.
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$subheading = get_field('clpr_store_video', $taxonomy . '_' . $term_id);
echo $subheading;
See code block to display taxonomy associated with post and list of taxonomies and their links:
If you wish to get taxonomy associated with particular post
$terms = get_the_terms( get_the_ID(), 'clpr_store_video' );
foreach ( $terms as $term ) {
echo $subheading = $term->name;
}
If you wish to get list and link of all taxonomies
<ul>
<?php
$taxonomies = get_terms('clpr_store_video', array("fields" => "all"));
foreach ($taxonomies as $taxonomy) {
echo '<li>'.$taxonomy->name.'</li>';
}
?>
</ul>
Related
I'm struggling with this even after looking at the documentation but all I'm wanting to do is link to a category. I have created a taxonomy field to select categories. Everything is working apart from when I add
I just get the ID of the category but I want the name. This is all within a repeater as I'm creating a menu but I need help getting the name and not the ID.
any help please?
If there is an option of selecting only one taxonomy.
Then write the query:
<?php $term_id = get_sub_field('category_link');
if( $term_id):
$term_name = get_cat_name( $term_id ) ;
$term_url = get_category_link( $term_id ); ?>
<?php echo $term_name; ?>
<?php endif; ?>
OR
If there are multiple category terms, then place this query:
<?php
$terms = get_sub_field('category_link');
if( $terms ):
foreach( $terms as $term_id ):
$term_name = get_cat_name( $term_id ) ;
$term_url = get_category_link( $term_id ); ?>
<?php echo $term_name; ?>
<?php endforeach; endif; ?>
Now place your category term url & name in the anchor tag.
Try this. It can give all your terms under the preferred taxonomy. Just give your taxonomy name in the below code
<?php $terms= get_terms( array(
'taxonomy' => 'taxonomy name',
) );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
First of all, let me know have you selected Term_id in the custom field which you have created & also let me know you can select one or more taxonomy through that custom field.
Trying to get a custom "category" of a custom post type in wordpress. With no luck. Went through a whole bunch of answers, but none worked.
Here is the latest one i've tried
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'quizes' );
if ( $terms != null ){
foreach( $terms as $term ) {
print $term->slug ;
unset($term);
} } ?>
I'm using the code below to get the name and slug of a custom taxonomy and than in the loop I echo the taxonomy name but not every post is linked to this taxonomy and if the post is not linked to the taxonomy get the error
Invalid argument supplied for foreach()
So I was wondering how do I use an if statement so if the post is not in the taxonomy than nothing happens. I tried wrapping the code below in a if(is_tax statement when I did that the custom taxonomy name didn't display on any of the posts.
<?php
$terms = get_the_terms($post->ID, 'class-content' );
foreach ( $terms as $term ) {
$class_name = $term->name; // this grabs the hyphenated name
$class_slug = $term->slug; // this grabs the hyphenated slug
}
?>
$terms = get_the_terms($post->ID, 'class-content' );
if(is_object($terms)){
foreach ( $terms as $term ) {
$class_name = $term->name; // this grabs the hyphenated name
$class_slug = $term->slug; // this grabs the hyphenated slug
}
}
In Wordpress I created a taxonomy called "Cities" for a CPT called "People". I want to display all taxonomy tags selected for a person in the template of the single.php-file.
What is the code for displaying only those elements selected?
I use the following code in the single-people.php:
<?php
$taxonomy = 'cities';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term -> name; ?></li>
<?php
} ?>
</ul>
<?php endif; ?>
I want only the selected taxonomy tags without link.
I understand you want to display all the terms of a taxonomy selected for a single post.
You need to use the function wp_get_post_terms() like this:
$taxonomy = 'cities';
$post_terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "names" ) );
foreach ( $post_terms as $term ) {
echo $term;
}
This will print all the names of the cities the current post is related to. Now you have to adapt this code to your page and, if you don't want links, just do not use <a> tags...
$wrap = 'hello';
$terms = wp_get_post_terms(get_the_ID( ), $wrap, array("fields"=>"names"));
foreach ( $terms as $term ) {
echo $term;
}
in the place of hello you take the taxonomy name and it surely run
I am trying to use custom taxonomy in my custom post. Suppose I have 12 custom post in 3 categories. I only need to display each category for one time. I have used the following code. But it repeated the categories.
<?php
$terms = get_the_terms( $post->ID , 'Categori' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
Please tell me how can I solved the problem.
You can use array_unique() function to remove duplicate values.
But first you need to store your term values in array.
Here is the full code:
<?php
$terms = get_the_terms( $post->ID , 'Categori' );
$c_terms = array(); //Create empty array
foreach ( $terms as $term ) {
$c_terms[] = $term->name;
}
$unique_cat = array_unique($c_terms);
foreach ($unique_cat as $cat) {
echo $cat;
}
?>