Display ACF Taxonomy in order of Multi Select order - php

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

Related

Getting all the product attributes and their terms in WooCommerce

I have researched this all day but cant seem to get a straight answer how can I get the set product attributes along with the configured terms for each?
This is what I have now
//get the terms
$attribute_taxonomies = wc_get_attribute_taxonomies();
$taxonomy_terms = array();
if ( $attribute_taxonomies ) {
foreach ($attribute_taxonomies as $tax) {
//dont know what to add here
}
}
var_dump($taxonomy_terms);
Here below you will get a list of all product attributes and their respective term names:
echo '<ul>';
// Loop through WooCommerce registered product attributes
foreach( wc_get_attribute_taxonomies() as $values ) {
// Get the array of term names for each product attribute
$term_names = get_terms( array('taxonomy' => 'pa_' . $values->attribute_name, 'fields' => 'names' ) );
echo '<li><strong>' . $values->attribute_label . '</strong>: ' . implode(', ', $term_names);
}
echo '</ul>';
If you prefer to get an array of the WP_terms objects, you will use:
// Get the array of the WP_Terms Object for the each product attribute
$terms = get_terms( array('taxonomy' => 'pa_' . $values->attribute_name );
That will allow use a foreach loop to get what you want from each term…
First check the wc_get_attribute_taxonomies() function with var_dump().

Getting custom taxonomy tags based on get_the_ID() (Outside for loop)

I have a custom taxonomy called type. Type has the following options:
Blog
News
Training
I've created a draggable element (in Visual Composer) which will show these tags. Because of this, the draggable element is not part of archive-resources.php, so I can't run it through a loop.
What I'm trying to do is:
Get blog post ID (which I've done via get_the_ID().
Display type that is assigned that blog post.
However, currently, all three type tags are displaying. Where am I going wrong?
$blogpostID = get_the_ID();
$termType = get_terms('type');
$output = '';
foreach ( $termType as $termT ) {
echo $output . ''.$termT->name.'';
}
Ok, Do it this way :
<?php
$terms = get_the_terms( $post->ID , 'type' );
$output = '';
if ( $terms != null ){
foreach ( $terms as $term ) {
echo $output . ''.$term->name.'';
}
}

Create associated array out of taxonomy terms

I'm trying to get associated array out of taxonomy slug and taxonomy name. I have this
foreach (get_terms('taxonomy') as $tax_term) {
$taxonomy_slug = $tax_term->slug;
$taxonomy_name = $tax_term->name;
}
The issue is, that these are just strings glued together, I don't know how to separate them :\ When I print_r them out I get:
term1term2term3term4...
What I need is a way to separate those, and then create array that will look like this:
Array(
['term_1'] => Term 1
['term_2'] => Term 2
...
)
Is this possible?
As per your inputs in comments I have tried to find out the solution. Hope this will helps you.
$terms = array();
foreach (get_terms('taxonomy') as $tax_term) {
$taxonomy_slug = $tax_term->slug;
$taxonomy_name = $tax_term->name;
$exploded = explode($taxonomy_name,$taxonomy_slug);
foreach ($exploded as $termValue) {
if (!empty($termValue)) {
$terms[$taxonomy_name."_".$termValue] = ucfirst($taxonomy_name)." ".$termValue;
}
}
}
echo "<pre>"; print_r($terms);

How to remove duplicate custom taxonomy?

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

How do I find the largets item in an array with php (specifically, largest taxonomy_id in an array of ids)?

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

Categories