0

I use ACF advanced custom field to add a custom field "authors" to "articles" post type. Authors field is a relationship field which relate Articles post type with Authors post type. I want to display the values of this custom field in the form of : author one, author two So, I found this code from ACF documentations:

<?php 
$posts = get_field('relationship_field_name');
if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <span>Custom field from $post: <?php the_field('author'); ?></span>
        </li>
    <?php endforeach; ?>
    </ul>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

But, this code display the result as a list. So, I tried to modify the code to be:

<?php 
$posts = get_field('author_name');
if( $posts ): 
    echo '' . ' <img src="aut.png" alt="authors" height="20" width="20">' . ' ' ;
?>
    <?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; ?>

Actually, this code give me the result in the form I want (author one, author two,), but, I need to delete the comma ',' from the end of the result to be (author one, author two). I know that i need to use echo rtrim($post,' ,') function, but I couldn't figure out how. Any suggestions please?

closed as off-topic by Michael, TheDeadMedic, Howdy_McGee, Mark Kaplun, Mayeenul Islam Aug 26 '15 at 7:41

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

0

This isn't really a WordPress question, but since you're using the Loop, a lot of people will assume it's WordPress-specific. That being said, the solution is to use PHP's native array-to-string conversion utilities.

Instead of the code you do have, you'll want to use something like:

<?php 
$posts = get_field( 'author_name' );
if ( $posts ) {
    echo '&nbsp;<img src="aut.png" alt="authors" height="20" width="20">&nbsp;';

    $post_list = array();
    foreach ( $posts as $post ) { // variable must be called $post (IMPORTANT)
        setup_postdata( $post );
        $post_list[] = sprintf( '<a href="%s">%s</a>', esc_url( get_the_permalink() ), esc_html( get_the_title() ) );
    }
    wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly

    echo implode( ', ', $post_list );

}

Very briefly, this updates your original code to do a few things:

  • It uses the &nbsp; HTML entity instead of attempting to concatenate a strings with extra spaces (more performant)
  • It builds up an array of post entries and uses PHP's native implode() to join the entries together with commas.
  • It uses sprintf() to make the post entry generation a bit cleaner
  • It uses proper escaping for the permalink and the post title to prevent stored XSS vulnerabilities

Again, these changes are vanilla PHP, we just happen to be using a WordPress loop and WordPress template tags.

  • Thanks a lot, that did the trick. thanks for the detailed helpful answer. – nisr Aug 23 '15 at 13:46
  • Another question please. Can i replace the last comma in (author one, author two, author three) with '&' to be (author one, author two & author three)? – nisr Aug 23 '15 at 13:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.