WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Plugin: Co-Authors Plus] Using is_first and is_last at the same time?! (2 posts)

  1. adastra
    Member
    Posted 4 years ago #

    Here's what I want to display:

    This article was written by Username1 with help from Username2, Username3 and Username4.

    I tried to configure the snippet provided to do this, but I always get this result:

    This article was written by Username1 with help from , Username2, Username 3 and Username4.

    As you can see, there's a stray comma in there which I can't get rid of. My PHP skills aren't the best...

    Here's my somewhat messy code:

    This article was written by
    <?php
    $i = new CoAuthorsIterator();
    $i->iterate();
    print "<strong>";
    the_author_posts_link();
    print "</strong>";
    if ($i->count() > 1) {
     echo " with help from ";
    }
    while($i->iterate()){
    		print $i->is_last() ? ' and ' : ', ';
    		print $i->is_first() ? '' : '';
    		print "<strong>";
    		the_author_posts_link();
    		print "</strong>";
    }
    ?>
    .

    http://wordpress.org/extend/plugins/co-authors-plus/

  2. adastra
    Member
    Posted 4 years ago #

    I found this solution somewhere else:

    <?php
    echo 'This article was written by';
    the_author_posts_link();
    
    $authors = new CoAuthorsIterator();
    $authors->iterate();
    if ($authors->count() > 1) {
    	echo ' with help from ';
    
    	$i = 0;
    	while($authors->iterate()) {
    		if ($i > 0) echo ', ';
    		if ($authors->is_last()) echo 'and ';
    
    		the_author_posts_link();
    		$i++;
    	}
    }
    
    echo '.';
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic