Shows posts from a Tag on a Wordpress page

Asked

Viewed 136 times

0

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

I found this site that seems to do that perfectly:

http://punchsub.net/#anime list/name/all/1

In the example of the site, the posts are separated by letter.

How can I do something similar using Wordpress ?

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 is at your discretion how to call the method, or even if you want to use this process in that way. In the middle of the code there are a lot of things that can be reduced, but the idea is basically this. Just give a print ($list) that you have access to everything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.