5

This seems like it should be something that's really simple to do, however it's apparently not.

I don't want tags to be links, but I want them to display in an unordered list, with each tag inside an <li>

get_the_tags allows you to echo them without the associated link, but I have no idea how to wrap them in li's.

 <?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->name . ' '; 
  }
}
?>

3 Answers 3

8

This would do it...

 <?php
$posttags = get_the_tags();
if ($posttags) {
  echo '<ul>';
  foreach($posttags as $tag) {
    echo '<li>' .$tag->name. '</li>'; 
  }
  echo '</ul>';
}
?>
4
  • Please explain why that could solve the problem.
    – fuxia
    Commented Jan 10, 2013 at 4:00
  • Thanks! What are the fullstops on either side of the $tag->name for?
    – andy
    Commented Jan 10, 2013 at 4:41
  • 1
    The periods? In PHP that's the string concatenation operator.
    – Doug
    Commented Oct 8, 2014 at 22:11
  • Hi! I found this is useful. But, when there are more than 1 tag, it shows without a comma. Is there any solution to fix up with a comma when there are more than 1 tag.
    – Jornes
    Commented May 11, 2020 at 3:02
0
add_action( 'loop_start', 'list_tags' );

function list_tags() {

$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );

foreach ( (array) $tags as $tag ) {

echo '<li>' . $tag->name . '</li>';
    }
}

Use any WordPress or theme specific hook from your functions file.

0

This is my preferred solution, to wrap the request with strip_tags to retain formatting options. Here is the code:

<?php echo strip_tags ( get_the_tag_list( '<span>', ',&nbsp;', '</span>' ) ); ?>

You can also do this for categories.

<?php echo strip_tags ( get_the_category_list( '<span>', ',&nbsp;', '</span>' ) ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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