Show posts of a tag in a Wordpress page

0

I'm trying to organize my posts based on the tags in each article. Example? Cars, houses, a, b, c ... and so on.

I found this site that seems to do it perfectly:

link

In the site example, posts are separated by letter.

How can I do something similar using Wordpress?

    
asked by anonymous 10.05.2015 / 20:28

1 answer

0

This procedure finds the tags that have posts associated with them, and assembles a list based on the first letter:

$list = '';
$tags = get_terms( 'post_tag' );
$groups = array();
if( $tags && is_array( $tags ) ) {
    foreach( $tags as $tag ) {
        $first_letter = strtoupper( $tag->name[0] );
        $groups[ $first_letter ][] = $tag;
    }
    if( !empty( $groups ) ) {
        foreach( $groups as $letter => $tags ) {
            $list .= "\n\t" . '<h2>' . apply_filters( 'the_title', $letter ) . '</h2>';
            $list .= "\n\t" . '<ul>';
            foreach( $tags as $tag ) {
                $url = attribute_escape( get_tag_link( $tag->term_id ) );
                $count = intval( $tag->count );
                $name = apply_filters( 'the_title', $tag->name );
                $list .= "\n\t\t" . '<li><a href="' . $url . '">' . $name . '</a> (' . $count . ')</li>';
                }
            $list .= "\n\t" . '</li>';
        }
    }
}else $list .= "\n\t" . '<p>Nenhuma Tag foi encontrada</p>';

From there, it's up to you how to call the method, or even if you want to use this process in this way. In the middle of the code there are enough things that can be reduced, but the idea is basically this. All you have to do is print ($list) that you have access to everything.

    
12.05.2015 / 20:19