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

WP SITES

2762

Original Genesis Tutorials & 6000+ Guaranteed Code

Snippets

  • Premium Access
  • Log in

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.

Subscribe for new Tutorials

Taxonomy Terms

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Recent Posts

  • Remove Star Rating Per Product in WooCommerce
  • Different Menu On WooCommerce Pages
  • Remove Coupon From Cart Page – WooCommerce
  • Set Logo Size In Essence Pro Theme
  • Different Header Image On Blog Page & Home Page – Essence Pro

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
 

Loading Comments...
 

You must be logged in to post a comment.