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
Related
This question already has answers here:
Removing last comma from a foreach loop
(9 answers)
Closed 17 days ago.
When separating the categories, the "," appears in the last category. e.g
Category1, Category2,
I am using this function.
// MultiNetwork
function MultiNetwork() {
global $post;
$terms = get_the_terms( $post->ID, 'networks');
$count = 0;
foreach ( $terms as $term ){
if ($count <= 1) {
$firstTerm = $term;
$term_link = get_category_link($firstTerm->term_id);
echo '<span class="network">' . $firstTerm->name . '</span>',',';
$count++;
}
}
}
I would like it to stay
Category1, Category2
Intente usando explode e implode.
For your case, simply only echo the "," if that is not the last item.
For example, remove ,',' and then add the following line:
if ($count < count($terms)) { echo "," ;}
So Change these two lines:
echo '<span class="network">' . $firstTerm->name . '</span>',',';
$count++;
to
echo '<span class="network">' . $firstTerm->name . '</span>';
$count++;
if ($count < count($terms)) { echo "," ;}
You can you use below code to resolve ", " problem.
$terms = get_terms([
'taxonomy' => 'networks',
'hide_empty' => false,
]);
$count = 0;
foreach ( $terms as $term ){
if ($count <= 1) {
$cat[] = $term->name;
$count++;
}
}
$data = implode(', ', $cat);
I have tried this code for the same. please look for the screenshot I'm attaching below.
Screen shot of result
I'm using a Wordpress function to enter the word 'and' before the last term in calling a term list, like this:
function TermList($taxonomy = 'markup') {
global $post;
$term_list = '';
$terms = get_the_terms($post->ID, $taxonomy);
$n = 1;
if ($terms) {
foreach($terms as $term) {
if ($n < count($terms)) {
$term_list .= $term->name . ', ';
} else {
$term_list = rtrim($term_list, ', ') . ' and ' . $term->name;
}
++$n;
}
}
$term_list = rtrim($term_list, ', ');
return $term_list;
}
I want to change this function so it only displays 5 terms and chooses those 5 terms randomly from all available terms. I don't know where to start. Who can point me in the right direction?
Using array_rand
You could use array_rand in php to select a random element from an array, here is the official documentation from php.net
and your code will be something like:
for($i=0 ; $i<5 ; $i++){
$term_list .= $terms [array_rand($terms )] . ', ';
}
$term_list = rtrim($term_list, ', ');
// replace the last comma with 'and'
$portion = strrchr($term_list , ',');
$term_list = str_replace($portion, (" and" . substr($portion, 1, -1)), $term_list );
Using shuffle and array_slice
Another solution to pick up random elements from an array could be using shuffle to randomly shuffle the elements of an array and then using array_slice to pick up certain elements.
Here is an example of code:
shuffle($terms );
print_r(array_slice($terms , 0, 3));
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 don't understand PHP very well but I'm trying to adjust this code below. I want the terms to all have a comma after them except for the last one. Right now the output looks like this:
Audience: Pre-School, High School, Adult,
However I need it to look like this:
Audience: Pre-School, High School, Adult
I realize there are similar questions out there that already answer this question, but since I am clueless when it comes to PHP I don't know how to incorporate those solutions with this code I already have. Can anyone help?
Thanks in advance!
<?php
$terms = get_terms( 'Audience' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<p><strong>Audience:</strong> ';
foreach ( $terms as $term ) {
echo '' . $term->name . ', ';
}
echo '</p>';
}
?>
You can do this with the amazingly convenient function implode:
echo implode(", ", $terms);
You can either count during the foreach loop;
$i = 1;
foreach ( $terms as $term ) {
echo '' . $term->name;
if ($i < count($terms)) {
echo ', ';
}
$i++;
}
Or you can simply load it all into a variable and then crop the last comma and space off.
$output = '';
foreach ( $terms as $term ) {
$output .= $term->name . ', ';
}
echo substr($output,0,-2);
Save the line to a variable during the loop and then remove the last character before printing it. Not the most elegant solution but it works
<?php
$terms = get_terms( 'Audience' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<p><strong>Audience:</strong> ';
$len = count($terms);
$i = 0;
foreach ( $terms as $term ) {
i++;
echo '' . $term->name . ($i < $len ? ', ' : '');
}
echo '</p>';
}
?>
You can use trim() http://ar2.php.net/trim
$string = '';
$terms = get_terms( 'Audience' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
$string .= '<p><strong>Audience:</strong> ';
foreach ( $terms as $term ) {
$string .= '' . $term->name . ', ';
}
$string .= "</p>";
echo trim($string, ',');
}
?>
You can use it to remove strings from the start and end of your whole string.
I am trying to print a nodes term, the code below works and it specifies which vocab ID. But I don't want the term to link to the term list. How do I remove the link. And can this be simplified and without the ul list?
<?php if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
$node = node_load(arg(1));
$vid = 7;
$terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
if ($terms) {print '<ul>'; foreach ($terms as $term)
{print '<li>'.l($term->name,'taxonomy/term'.$term->tid).'</li>'; }
print '</ul>';
}}?>
simply remove l() function
<?php if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
$node = node_load(arg(1));
$vid = 7;
$terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
if ($terms) {print '<ul>'; foreach ($terms as $term)
{print '<li>'.$term->name.'</li>'; }
print '</ul>';
}}?>