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;
}
?>
Related
Using ACF, I have a Taxonomy field with Multi Select and Stylized UI.
I usually have 2 or 3 tax items selected.
I can arrange them using drag and drop. It's wonderful.
On the front end, the array does not reflect the tax order/sorting that I put in place.
Pretty easy code. What am I doing wrong?
<?php
$values = get_the_terms( $post->ID, 'languages' );
if ( $values ) {
echo '<tr class="item"><td>';
foreach ( $values as $value ) {
echo $value->name . '<br/>';
}
echo '</td></tr>';
}
?>
Figured it out.
Because the field saved the correct sorting in the admin field, I knew the order data was saved somewhere. I looked in the database at the post's terms and the associated data. The order was saved there. Instead of using get_the_terms for the loop, I used get_post_meta. That gave me an array with the term IDs in the correct order. Then I got the term name using that ID within a foreach loop. This gave me the taxonomy names in the order from the styled multi-select field.
<?php
$values = get_the_terms( $post->ID, 'languages' );
if ( $values ) {
echo '<tr class="item"><td>';
$values = get_post_meta( $post->ID, 'languages' );
foreach ( $values as $value ) {
foreach ( $value as $item ) {
$term = get_term( $item )->name;
echo $term .'<br/>';
}
}
echo '</td></tr>';
}
?>
I am using two plugins WPCustom Category Image for Be able to insert an image in each category and W4 Post List for list the categories on the screen.
In this moment give me always the last image of categories. I pretend the image of each category corresponding to each ID.
The plugin have one shortcode [term_id] which shows the corresponding category id but i can't use one shortcode inside from another shortcode.
Example - [wp_custom_image_category onlysrc="false" size="full" term_id="[term_id"]]
Any solution?
foreach( get_categories(['hide_empty' => false]) as $category) {
$image = do_shortcode('[wp_custom_image_category onlysrc="false" size="full" term_id="'.$category->term_id.'" ]');
echo $image.'<br/>';
// $id = 4;
$options['template'] =
'[terms]
<div class="col-sm-3 news-cat-links" id="[term_id]">
[term_name]<img src="'.$image.'" /></a>
</div>[/terms]';
}
Go to corresponding plugin files and find the functions of [term_id] shortcode.
below code returns array of custom texanomy named "eventcategory". define $post as global variable .
$terms = get_the_terms( $post->ID, 'eventcategory' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
print_r($draught_links);
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>
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 return the highest term taxonomy id of a post page or a taxonomy page.
I was sucessful at listing all taxonomy id's like this:
<?php
$terms = get_the_terms( $post->ID , 'mytaxonomy' );
if($terms) {
foreach( $terms as $term ) {
echo $term->term_taxonomy_id;
}
}
?>
This is what I am trying to do (return only the highest ID) (doesn't work):
<?php
$terms = get_the_terms( $post->ID , 'mytaxonomy' );
if($terms) {
foreach( $terms as $term ) {
echo max( '$term->term_taxonomy_id');
}
}
?>
Please help :)
NOTE:
My main goal is to make this work: https://wordpress.stackexchange.com/questions/9562/multi-level-taxonomy-navigation
The max function can probably compare only two terms at a time. Keep track of the maximum term as you go through the loop - maybe:
maximum_term = max(maximum_term,term->term_taxonomy_id);
No need to use the loop at all just apply max to the $terms variable