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 . '",';
}
?>
Related
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>";
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
PHP
<?php
$categories = get_the_category();
foreach ($categories as $category){
echo $category->cat_name;}
?>
Currently this will display categories as "cat1cat2cat3"
What I want it to be is "cat1, cat2, cat3"
I tried this echo $category->cat_name . ', '; but this just adds a comma after every category. Even if the post just has one category: Ex. "cat1, " And it also adds commas to the last category in the list: Ex. "cat1, cat2, cat3, "
So how can I get the commmas in but absent if just one category and absent on the last category if its a list?
You can also use implode() for that:
$categories = get_the_category();
$category_names = array();
foreach ($categories as $category)
{
$category_names[] = $category->cat_name;
}
echo implode(', ', $category_names);
Try this: (As of PHP 5.3)
$categories = array_map(function($category) {
return $category->cat_name;
}, get_the_category());
echo implode(', ', $categories);
you can do something like:
<?php
$categories = get_the_category();
$cat = '';
foreach ($categories as $category){
$cat .= $category->cat_name . ', ';
}
$cat = substr($cat,0,-2);
echo $cat;
?>
best regards,
I would like to use php to loop through a products categories and store them into an array, which will be assigned as class names for each product. For some reason my code is not working, and there are no PHP errors. Perhaps it is a wordpress issue:
$classes = array();
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
$classes[] = $term->slug;
}
<li <?php post_class( $classes ); ?>>
Essentially, I am trying to assign categories as class names to their respective product. This isn't throwing an error, but nothing loads. Anyone see any issues here?
Using array_shift to break up the array worked for me, but it does not work if $cats has mutliple classes
<?php $classes = array();
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
$cats[] = $term->slug;
}
$classes[] = implode(" ", $cats);
<li <?php post_class( $classes); ?>>
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>';
}
?>