I'm stuck getting terms into a list and class at the same time. I can return the list no problem, but I need the slug to also be the class.
Here is my code
$terms = get_the_terms($post->ID, 'events17groups' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
endif;
$listclass = implode(" ", $tslugs_arr);
$listgroup = "<li class='$listclass'>" . implode("</li><li
class='$listclass'>", $tslugs_arr) . "</li>";
echo "<ul class='eventgroup'>$listgroup</ul>";
This produces the following HTML
<ul class="eventgroup"><li class="exhibitions home">exhibitions</li><li
class="exhibitions home">home</li></ul>
So what I want is
<ul class="eventgroup"><li class="exhibitions">exhibitions</li><li
class="home">home</li></ul>
Any suggestions?
Why not use a simple readable foreach:
$listgroup = '';
foreach ($tslugs_arr as $item) {
$listgroup .= '<li class="' . $item . '">' . $item . '</li>';
}
echo "<ul class='eventgroup'>$listgroup</ul>";
Have you tried like this?
$terms = get_the_terms($post->ID, 'events17groups' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
endif;
$listclass = implode(" ", $tslugs_arr);
$listgroup = "<li class='".tslugs_arr[0]."'>" . implode("</li><li
class='".tslugs_arr[1]."'>", $tslugs_arr) . "</li>";
echo "<ul class='eventgroup'>$listgroup</ul>";
If you want to do a fancy one-liner then try out:
<?php
$terms = get_the_terms($post->ID, 'events17groups' );
if ($terms && ! is_wp_error($terms)) :
$tslugs_arr = array();
foreach ($terms as $term) {
$tslugs_arr[] = $term->slug;
}
endif;
echo "<ul class='eventgroup'>".implode('', array_map( function( $tslug ){ return "<li class='$tslug'>$tslug</li>"; }, $tslugs_arr ) )."</ul>";
Related
Attempting to output a custom post type taxonomy (property_type) as a shortcode. At the moment where it should output the taxonomy it simply outputs the word Array. Quite new to php, so possibly something simple I'm missing, or completely barking up the wrong tree.
Code is:
function prop_type_shortcode() {
$terms = wp_get_post_terms($post->ID, 'property_type');
if ($terms) {
$out = array();
foreach ($terms as $term) {
$out[] = '<a class="' .$term->slug .'" href="' .get_term_link( $term->slug, 'property_type') .'">' .$term->name .'</a>';
}
echo join( ', ', $out );
}
return $terms;
}
add_shortcode('type', 'prop_type_shortcode');
Thanks in advance for any help.
You are returning the array from wp_get_post_terms(). Instead you should be returning your processed text.
function prop_type_shortcode() {
global $post;
$terms = wp_get_post_terms($post->ID, 'property_type');
if (!$terms) {
return '';
}
$out = array();
foreach ($terms as $term) {
$out[] = '<a class="' .$term->slug .'" href="' .get_term_link( $term->slug, 'property_type') .'">' .$term->name .'</a>';
}
return join( ', ', $out );
}
I am using this function:
// Get terms for post
$terms = get_the_terms($post->ID, 'skills');
if($terms != null) {
foreach($terms as $term) {
echo $term->name . ", ";
unset($term);
}
}
However I see the terms as term1, term2, term3, (with also a comma at the end), how can I show the terms with commas but without that comma when is not necessary?
Instead of echoing all the variables during your loop, you should store them all to an array and then use the implode() function to echo them all with the formatting you desire.
// Get terms for post
$terms = get_the_terms($post->ID, 'skills');
if($terms != null) {
$output = array();
foreach($terms as $term) {
$output[] = $term->name;
unset($term);
}
echo implode(", ", $output)
}
Don't want to use an array or variable? There is another solution. Just check if you are currently on the very last element in the array during your loop.
// Get terms for post
$terms = get_the_terms($post->ID, 'skills');
if($terms != null) {
end($terms);
$endKey = key($terms);
foreach($terms as $key => $term) {
echo $key != $endKey? $term->name.", " : $term->name;
unset($term);
}
}
I added $key to the foreach loop so you can compare against that. You can get the final key of the array by doing end($array) and then using key() to get the actual key.
If you dont want to use array use following:
$terms = get_the_terms($post->ID, 'skills');
$string = "";
if($terms != null) {
foreach($terms as $term) {
$string .= $term->name . ", ";
unset($term);
}
}
echo trim($string, ", ");
You can use rtrim. (from php.net : Strip whitespace (or other characters) from the end of a string)
// Get terms for post
$terms = get_the_terms($post->ID, 'skills');
if($terms != null) {
$stringFinal = "";
foreach($terms as $term) {
$stringFinal = $term->name . ", ";
}
$stringFinal = rtrim($stringFinal, ', ')
}
echo $stringFinal;
Omitting all checks, this can be reduced to one line.
// Get terms for post
$terms = get_the_terms($post->ID, 'skills');
echo implode(", ", array_map(function($T) {return is_a($T, 'WP_Term') ? $T->name : false; }, $terms ))
Works like array_column but with object
I have a piece of code that I use to extract my WordPress categories. It's like this:
<?php foreach ($terms as $term) { echo $term->slug ;} ?>
That code will generate a list of categories like this:
BrazilItalyThailand
But I need the output to be like this:
"Brazil","Italy","Thailand"
How can I do that?
<?php
foreach($terms as $term)
{
$cats[] = $term->slug;
}
$str = '"'.implode('","', array_unique($cats)).'"';
?>
no need of regex!
<?php
foreach ($terms as $term) {
echo '"' . $term->slug . '",';
}
?>
I've got the following PHP code that I'm using to output a list of all custom taxonomy values, then group them into alphabetical order by first letter. This is working fine, except that the URL isn't being outputted. Anyone able to help?
<?php
$list = '';
$tags = get_terms( 'film-categories' );
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= '<div class="cat-group"><h3>' . apply_filters( 'the_title', $letter ) . '</h3>';
$list .= '<ul>';
foreach( $tags as $tag ) {
$url = esc_attr( get_tag_link( $tag->term_id ) );
$name = apply_filters( 'the_title', $tag->name );
$list .= '<li>' . $name . '</li>';
}
$list .= '</ul></div>';
}
}
} else $list .= '<p>Sorry, but no tags were found</p>';
echo $list;
?>
I'm afraid you've confused.
According to your second line - you're fetching terms of custom tax and not tags.
$tags = get_terms( 'film-categories' );
Therefore , any function related to tags won't work correctly.
In order to get the url of the term use the get_term_link() function.
Just replace the current line with:
$url = esc_attr( get_term_link( $tag ) );
Should work.
I am using this code to post a list of all children terms for my taxonomy called 'location'
<?php
$termID = 10;
$taxonomyName = "location";
$termchildren = get_term_children( $termID, $taxonomyName );
echo '<ul>';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
Currently the code is using term id 10 and getting the children for that and displaying in a list.
However I want to add multiple term id's and display a list of all the children of the id's I add.
My id's are 4,6,8,10,14,18,22
Looking for some help to figure out how to do this?
Cheers
I figured out the answer to my own question. using get_terms will get all the terms retured, so there is no need to specify individual id's of my terms.
My code is
<?php
$args = array( 'taxonomy' => 'location' );
$terms = get_terms('location', $args);
$count = count($terms); $i=0;
if ($count > 0) {
$cape_list = '<p class="location-archive">';
foreach ($terms as $term) {
$i++;
$term_list .= '<li><span class="feature">' . $term->name . '</li>';
if ($count != $i) $term_list .= ' ยท '; else $term_list .= '</p>';
}
echo '<ul class="list">';
echo $term_list;
echo '</ul>';
}
?>