Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to display the tags of a post different than what's standard in WordPress. If I've understood it correct, all I can do with the regular tags is to display different things between them and at the end.

The tags should be displayed like this:

Read more about tag 1, tag 2, and tag 3 Read more about tag 1 and tag 2

How can I get that "and" word in there?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

the_tags() is simple for convenience, if you want more control over formatting, use get_the_tags() and output them however you want.

EDIT - quick example for the template:

<?php
$posttags = get_the_tags();

if ($posttags) {

    echo 'Read more about ';

    $tag_count = count( $posttags );
    $count = 0;

    foreach( $posttags as $tag ) {
        $count++;
        if( $tag_count == $count )
            $sep = '.';
        elseif( $tag_count - 1 == $count )
            $sep = ', and ';
        else
            $sep = ', ';
        ?>
        <a href="<?php echo get_tag_link( $tag->term_id ); ?>" title=""><?php echo $tag->name; ?></a><?php echo $sep; ?>
        <?php
    }
}
?>
share|improve this answer
2  
Optimized your sample code, you should never put counts inside loops, the count has to be re-calculated with every iteration that way, better to do it once prior to the loop. –  t31os Aug 17 '11 at 21:49
    
You're awesome guys. If you have time, could you please tell me what's going on with the code? I'm not that familiar with PHP but would like to learn. Or maybe just tell me about the important parts so I can read about those. –  Mats Aug 17 '11 at 22:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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