• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2665

Original Genesis Tutorials & 5000+ Guaranteed Code

Snippets

  • Support
  • Newsletter
  • Videos
  • Log in

Premium Member? - Request custom code

Display List of Tags

This tutorial provides 5 different code examples which enable you to display a list of tags anywhere in your theme.

tags

In the 1st example, the code displays the list using the loop start hook and includes the count.

add_action( 'loop_start', 'list_tags_with_count' );
function list_tags_with_count() {
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) {
echo '<li><a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . ' (' . $tag->count . ') </a></li>';
    }
}

The 2nd example removes the tag count:

add_action( 'loop_start', 'list_tags_no_count' );
function list_tags_no_count() {
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) {
echo '<li><a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . '</a></li>';
    }
}

The 3rd example adds a custom class to each listed tag:

add_action( 'loop_start', 'list_tags_with_class' );
function list_tags_with_class() {
$tags = get_tags();
$html = '

<ul class="post_tags">';
foreach ( $tags as $tag ) {
    $tag_link = get_tag_link( $tag->term_id );
            
    $html .= "<li><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
    $html .= "{$tag->name}</a></li>";
    }
$html .= '</ul>

';
echo $html;
}

The 4th example works on single posts and displays tags for each post in a list.

add_action('loop_start','list_single_post_tags');
function list_single_post_tags() {
if ( is_singular('post')) {
    echo get_the_tag_list('

<ul><li>','</li><li>','</li></ul>

');
    }
}

See get_the_tag_list for more information.

Custom Tag List Widget

This code enables you to create your own tag list widget similar to the native tag cloud widget included in WordPress.

custom-tag-widget

You’ll need to load the file from your functions file and register the widget using the following code.

In this example, we’ll load the file from a child theme using get_stylesheet_directory()

require_once( get_stylesheet_directory() . '/class-wp-widget-tag-list.php' );

function register_tag_lists_widget() {
    register_widget( 'WP_Widget_Tag_List' );
}
add_action( 'widgets_init', 'register_tag_lists_widget' );

And then create a file named class-wp-widget-tag-list.php in your child themes root directory using the following code.

Taxonomy Terms

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Recent Posts

  • Checkbox to Remove Specific Posts & Pages From Your Home Page Loop
  • Menu Logo Menu Using CSS Grid In New Versions of Genesis Sample Theme
  • Featured Posts With Pagination In Widget Area Using Business Pro
  • How To Translate the Property Details Additional Features Text In AgentPress Pro
  • Unlink All Archive Links For Each Entry On Per Post Basis

Advertise · WPEngine · Genesis · Log in

  • How Premium Membership Works
  • Sign Up
  • Support
  • Subscription Details/Invoice
  • Tagged Tutorials
  • Access-Download Problems