0

I have this code wich I use in Wordpress for listing the tags and the number of times they've been used. Here is the code

<?php
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) { ?>
<li class="list-group-item">
<span class="badge"><?php echo $tag->count; ?></span>
<?php echo $tag->name; ?>
</li>
<?php }
?>

The result is the following:

http://s14.postimg.org/z5qghdes1/result.png

So I want to delete the tag 'Animals' from the list. I've looking in other questions but I could not resolve my problem. Thanks for your answer!

3 Answers 3

3

Use the exclude argument for get_tags(). In my example I set the term ID to 5; be sure to replace it with the correct value.

exclude can be an array as well allowing you to exclude multiple tags.

Change:

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

To:

$tags = get_tags( array( 
    'orderby' => 'count', 
    'order'   => 'DESC',
    'exclude' => 5,
) );
Sign up to request clarification or add additional context in comments.

1 Comment

Given WordPress, this is the best solution.
0

You can update your code as bellow:

<?php
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) { 
  if ($tag->name == "Animals" ) { continue; }
?>
<li class="list-group-item">
<span class="badge"><?php echo $tag->count; ?></span>
<?php echo $tag->name; ?>
</li>
<?php }
?>

2 Comments

That works fine! In the other hand, how could I display only the tags I want? For example, I have a lot of tags but I only want to display 'Tech' and 'News'
@CristianTirado If you only want to display a certain set of tags you should use the include argument. See Nathan's answer, just use include instead of exclude.
0

Use following

<?php
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) { 
  if ($tag->name != "Animals" ) {
?>
<li class="list-group-item">
<span class="badge"><?php echo $tag->count; ?></span>
<?php echo $tag->name; ?>
</li>
<?php 
  }
}
?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.