Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

List showing all tags on an element loop template

  • fjvanittersum

    I came across this script in order to show a list of available tags in list form, somewhere on a page or post. I would like to add a list with available tags at the bottom of an element loop template, i.e. after the query loop (I do not like the WP-tag cloud block):

    <h2>Tags</h2>
    <ul>
        <?php
        $tags = get_tags();
        if ( $tags ) :
            foreach ( $tags as $tag ) : ?>
                <li><a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
            <?php endforeach; ?>
        <?php endif; ?>
    </ul>

    Unfortunately, the php-script is not interpreted, if added as html-block.

    How can it be solved? Should I put the php-script in Code-snippets? If yes, how to refer to this script? Or is there another way. Offcourse, I can use a list block and add the links to the tags manually, but I prefer generating a list of all available tags automatically.

    Thanks !

    Frans

  • Hi there,

    Use this PHP code to create a shortcode [tag_list] which can be used in your loop template:

    // Define a function to retrieve and display tags
    function custom_tag_list_shortcode() {
        // Retrieve tags using get_tags() function
        $tags = get_tags();
    
        // Initialize output variable
        $output = '';
    
        // Check if there are any tags
        if ( $tags ) {
            // Start building the unordered list
            $output .= '<ul>';
            
            // Loop through each tag
            foreach ( $tags as $tag ) {
                // Add list item with tag link
                $output .= '<li><a href="' . esc_url( get_tag_link( $tag->term_id ) ) . '" title="' . esc_attr( $tag->name ) . '">' . esc_html( $tag->name ) . '</a></li>';
            }
            
            // Close the unordered list
            $output .= '</ul>';
        }
    
        // Return the output
        return $output;
    }
    // Register the shortcode
    add_shortcode( 'tag_list', 'custom_tag_list_shortcode' );
  • fjvanittersum

    Thanks! It works fine!
    Frans

  • You are welcome   🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.