0

I am working on a template for a custom post type (books) in which I am calling a list of authors that come from another custom post type (people). They are linked through the plugin Posts 2 Posts.

My Aim is to create a sequence of authors separated by comma, like this: Author1, Author2, Author3, Author4.

With the code below, the result is a sequence of authors like this : Author1Author2Author3Author4. The same code also works to get a list of elements by adding a li division below the_post();, with this results :
-Author1
-Author2
-Author3
-Author4

The piece of code :

if ($autores->have_posts()): 

                echo '<li>';
                    echo '<b>Autores: </b>';
                        while ( $autores->have_posts() ) : $autores->the_post();
                                echo '<a href="';
                                echo the_permalink();
                                echo '">';
                                echo the_title();
                                echo '</a>';
                        endwhile; 
                    echo '</li>'; 
                    endif; 

Maybe this question can be answered in multiple forms. One is by converting this object into an array of elements (I don't know how...yet?). At least, I found a very useful method which works with taxonomy elements (here: https://wordpress.stackexchange.com/a/238362), but this is not working in my case, maybe because I have one object, so when I make a count($autores) I always have 1 as result. I also wonder if it is possible to use css to convert a list of elements into inline elements and separate them with commas.

I will really appreciate if someone here can give me an idea to resolve this question. Keep searching by my side...

1
  • the_permalink echo's internally, it doesn't return a title, so echo the_permalink(); is the same as echo ""; the_permalink();. the same is true of any WP function starting with the_
    – Tom J Nowell
    Feb 22, 2018 at 17:07

1 Answer 1

0

If you want to keep them all within one outer <li>, you can use something like this. Instead of echoing them immediately, save them to an array, then loop through the array.

<?php
if ($autores->have_posts()):
    echo '<li>';
        // create an empty array
        $autoresArray = array();
        while($autores->have_posts()): $autores->the_post();
            // save the link and name
            $autoreLink = get_permalink();
            $autoreName = get_the_title();
            // now add them to a multidimensional array
            $autoresArray[] = array(
                'link' => $autoreLink,
                'name' => $autoreName,
            );
        endwhile;
        // outside of 'while' so we only display the author list once
        for($i=0; $i<count($autoresArray); $i++) {
            // display the linked name
            echo '<a href="' . $autoresArray[$i]['link'] . '">' . $autoresArray[$i]['name'] . '</a>';
            // if it's not the last name in the array, add comma and space
            if($i != (count($autoresArray)-1)) {
                echo ', ';
            }
        }
    echo '</li>';
endif;
?>
4
  • the_title and the_permalink don't return, they echo, so this won't work
    – Tom J Nowell
    Feb 22, 2018 at 17:07
  • This piece of code is really interesting. The test for is working really well to retrieve all the names and links, but something is wrong with the counter test, the last name is still having a comma.
    – Gadget
    Feb 23, 2018 at 10:25
  • Ah, my mistake, the loop goes to 1 less than the count (since arrays start at 0, not 1) so it always adds the comma. Updated above so the last one should no longer have a comma.
    – WebElaine
    Feb 23, 2018 at 15:05
  • It works! WebElaine, I'am astonished. I was trying to modify the value of $i and the comparator between $i and the count. I would never imagine this solution. I was reading a lot about php to understand better how wordpress works, and I realize that it is a really powerful language. . I appreciate your help a lot. One hundred thanks !!!
    – Gadget
    Feb 23, 2018 at 15:47

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