Ashley Cameron Design

Ashley Cameron Design

List posts by taxonomy tag

SnippetsWordPress

About a 1 minute read

"Individually, we are one drop. Together, we are an ocean."

Ryunosuke Satoro


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php //List posts by taxonomy tag
    $terms = get_terms('language');
        if (count($terms)) {
        echo "<h3>Quick links</h3>";
        }
    $i=0; // counter for printing separator bars
        foreach ($terms as $term) {
            $wpq = array ('taxonomy'=>'language','term'=>$term->slug);
            $query = new WP_Query ($wpq);
            $article_count = $query->post_count;
            echo "<a href="#".$term->slug."">".$term->name."</a>";
            // output separator bar if not last item in list
            if ( $i < count($terms)-1 ) {
            echo " | " ;
            }
        $i++;
        }
    foreach ($terms as $term) {
        $wpq = array ('taxonomy'=>'language','term'=>$term->slug, 'orderby'=>'title', 'order'=>'ASC');
        $query = new WP_Query ($wpq);
        $article_count = $query->post_count; ?>
        <h2 id="<?php $term->slug ?>"><?php echo $term->name; ?></h2>
        <?php if ($article_count) { ?>
        <ul>
        <?php $posts = $query->posts;
        foreach ($posts as $post) {
        echo "<li><a href="".get_permalink()."">".$post->post_title."</a></li>";
        } ?>
        </ul>
        <?php }} ?>