T
T
tbalero2015-09-28 23:50:09
WordPress
tbalero, 2015-09-28 23:50:09

How to display category names (from custom taxonomy) separated by commas but without trailing commas?

1. On the WordPress site, on the post page (custom post type), the following code is used to display category names without a link (custom taxonomy category 'genres'):

<?php
    $cur_terms = get_the_terms( $post->ID, 'genres');
    foreach( $cur_terms as $cur_term ){
    echo array_shift( $cur_terms )->name. ', ';
    }
?>

As a result, category names without links (custom taxonomy categories) are displayed correctly, separated by commas, but an extra comma is displayed after the last category name.
Question_1: How do I change this code to display category names without links (custom taxonomy categories) separated by commas, but without a comma at the end ?
2. On the WordPress site on the post page of one custom type - to display a list of related posts of another custom type, a custom 'relationship-field' field of the "Relationship field" type is used ( www.advancedcustomfields.com/resources/relationship ). The following code is used to display:
<?php 
$posts = get_field('relationship-field');

if( $posts ): ?>

   <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
      <?php setup_postdata($post); ?>

         <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

   <?php endforeach; ?>

   <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

As a result, on the post page of one custom type, the titles (with links) of related posts of another custom type are correctly displayed. The titles of related entries are displayed separated by spaces, but without commas.
Question_2: How should I change this code to display the names of related posts separated by commas, but without a comma at the end ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim E, 2015-09-29
@tbalero

for the first one like this:

<?php
$array = array();
$cur_terms = get_the_terms( $post->ID, 'genres');
foreach( $cur_terms as $cur_term ){
$array[] = $cur_term->name;
}
$comma_separated = implode(", ", $array);

echo $comma_separated; // term1, term2, term3
?>

For the second:
<?php 
$posts = get_field('relationship-field');

if( $posts ): 
$new_posts = array(); ?>

   <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
      <?php setup_postdata($post); 
      $new_posts[] =  '<a href="'.get_the_permalink().'">'.get_the_title().'</a>'; // сложили все в массив
      ?>
   <?php endforeach; 
    $done_post = implode(",", $new_posts);
    echo $done_post; ?>

   <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

D
Denis, 2015-09-29
@prototype_denis

$last = end($posts);
foreach ($posts as $post) {
    print ($post === $last) ? '' : ', ';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question