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?


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:

$posttags = get_the_tags();

if ($posttags) {

    echo 'Read more about ';

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

    foreach( $posttags as $tag ) {
        if( $tag_count == $count )
            $sep = '.';
        elseif( $tag_count - 1 == $count )
            $sep = ', and ';
            $sep = ', ';
        <a href="<?php echo get_tag_link( $tag->term_id ); ?>" title=""><?php echo $tag->name; ?></a><?php echo $sep; ?>
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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