0

I am trying to list all my tags in index.php, but it doesn't seem to be working and I am unable to see why. Looking at documentation, the properties are correct and and get_tags() is retieving an array.

<?php 
$tags = get_tags();
foreach($tags as $tag) { 
echo '<li><a class="button" data-filter=".tag-'.$tag->name.'" href="' . get_tag_link(
$tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></li>
';
} 
?>

I was able to achieve what I wanted with categories using:

<?php 
$categories = get_categories();
foreach($categories as $category) { 
echo '<li><a class="button" data-filter=".category-'.$category->name.'" href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>
';
} 
 ?>
5
  • What do you mean by not working Commented Dec 21, 2014 at 14:33
  • The code should output all my tags with the html I have added, but unfortunately nothing is being echoed. The codex offers a solution to list tags on the get_tags page, and using that outputs nothing, so I'm wondering is there something stopping this from working? Commented Dec 21, 2014 at 14:47
  • Your code works, you most probably have some other error. Turn debug on and check for errors. Commented Dec 21, 2014 at 14:56
  • I created some new tags and they appeared straight away, but not the old tags which seems strange. The old tags are just imported dummy content from wordpress, what would stop them being pulled in? Commented Dec 21, 2014 at 17:20
  • It is difficult to say. These old tags, do they have posts attached to them. If they are not assigned to posts, then they will not show up in your code Commented Dec 21, 2014 at 18:01

1 Answer 1

-2

I tested your code and it's working. So you maybe have somewhere else an error.

Try var_dump($tags); and see if there's content.

Did you created some tags and assigned them to posts?

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.