• Resolved chiffy

    (@chiffy)


    Hi,

    I am trying to grab the News Tags and list them in my News Archive

    This is what i got in my function simple_news_loop_custom() in my Themes functions.php:

    // Get Tags
    $args = array(
        'orderby' => 'name',
        'order' => 'ASC'
    );
    $tags = get_tags($args);
    $html = '<span id="news-pill" class="badge badge-pill badge-secondary"><i class="fas fa-tag"></i> ';
    foreach ( $tags as $tag ) {
        $tag_link = esc_url( get_tag_link( $tag->term_id ) );
                 
        $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
        $html .= "{$tag->name}</a>";
    }
    $html .= '</span>';
    if ( !empty($tags)  ) :
    echo $html;
    else :
        echo 'no tags';
    endif;
    

    I built in the Troubleshoot “no tags” and that is what i get for all my NewsPosts (Yes, I did create the Tags in the Admin area.

    ————–

    For Categories I used (and it works fine):

    // Get Categories
    $categories = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC'
    ) );
     
    foreach( $categories as $category ) {
        $category_link = sprintf( 
            '<span id="news-pill" class="badge badge-pill badge-secondary"><i class="fas fa-folder"></i> <a href="%1$s" alt="%2$s">%3$s</a></span>',
            esc_url( get_category_link( $category->term_id ) ),
            esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
            esc_html( $category->name )
        );
        echo $category_link; 
    } 

    Can you please enlighten me on how to to this? ??

    Cheers

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter chiffy

    (@chiffy)

    Solved it. This works for me:

    <?php
    // Get Tags
    $args = array (
        'taxonomy' => 'newstags',
        'orderby' => 'name',
        'order' => 'ASC',
    );
    
    $tags = get_terms($args);
    foreach ( $tags as $tag ) {
        $tag_link = esc_url( get_tag_link( $tag->term_id ) );
        $html = '<span id="news-pill" class="badge badge-pill badge-secondary"><i class="fas fa-tag"></i> ';
        $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
        $html .= "{$tag->name}</a></span>";
        if ( !empty($tags)  ) :
            echo $html;
        endif;
    }

    Thanks anyway for your simple yet awesome plugin ??

    Plugin Author MortenPeterAndersen

    (@mortenpeterandersen)

    Love to help … and without doing nothing ??

    Thread Starter chiffy

    (@chiffy)

    After investigating, I used the wrong function:
    get_terms(); using this spits out ALL tags that are defined.
    To list all Tags used by a SINGLE post one must use get_the_terms();

    So the final code:

    // Get Tags
    
    $args = array (
        'taxonomy' => 'newstags',
        'orderby' => 'name',
        'order' => 'ASC',
    );
    
    $tags = get_the_terms($post, 'newstags');
    if ( !empty($tags)  ) :
    foreach ( $tags as $tag ) {
        $tag_link = esc_url( get_tag_link( $tag->term_id ) );
        $html = '<span id="news-pill" class="badge badge-pill badge-secondary"><i class="fas fa-tag"></i> ';
        $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
        $html .= "{$tag->name}</a></span>";
            echo $html;
    }
    endif;
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get News Tags’ is closed to new replies.