Post Reads 0 reads

Step 1: Create a file show-posts-in-group-by-term-or-taxonomies.sublime-snippets

Step 2: Copy and Paste below code into the file.

<snippet>
        <content><![CDATA[

// List posts by the terms for a custom taxonomy of any post type
$post_type = '${1:services}';
$tax = '${1:taxonomy_services}';
$tax_terms = get_terms( $tax, 'orderby=name&amp;order=ASC');
if ($tax_terms) {
        foreach ($tax_terms  as $tax_term) {
                $args = array(
                        'post_type'         	=> $post_type,
                        "$tax"        			=> $tax_term->slug,
                        'post_status'           => 'publish',
                        'posts_per_page'        => -1,
                        'order' 			=>	'DESC'
                );

                $my_query = null;
                $my_query = new WP_Query($args);

                if( $my_query->have_posts() ) : ?>

                        <div class="container">
                                <h2 class="title">SERVICES [<?php echo $tax_term->name; ?>]</h2>

                                <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

                                                <h2><?php the_title(); ?></h2>

                                <?php endwhile; // end of loop ?>
                        </div>

                <?php endif; // if have_posts()
                wp_reset_query();

        } // end foreach #tax_terms
} // end if tax_terms

]]></content>
        <description>group by taxonomies</description>
        <tabTrigger>show_post_by_taxonomies</tabTrigger>
        <!-- Optional: Set a scope to limit where the snippet will trigger -->
        <!-- <scope>source.python</scope> -->
</snippet>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top