Created
June 8, 2018 14:15
-
-
Save maxyudin/c1e2ffe314ecc964f115f80a5067a6e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Based on https://wordpress.stackexchange.com/a/228071/11761 | |
* | |
*/ | |
$post_type = 'item'; | |
$taxonomy = 'item_tags'; | |
// count the number of terms for correct pagination | |
$term_count = get_terms( array ( | |
'taxonomy' => $taxonomy, | |
'fields' => 'count', | |
) ); | |
// define the number of terms per page | |
$terms_per_page = 10; | |
// find out the number of pages to use in pagination | |
$max_num_pages = ceil( $term_count / $terms_per_page ); | |
// get the page number from URL query | |
$current_page = get_query_var( 'paged', 1 ); | |
// calculate offset | |
$offset = ( $terms_per_page * $current_page ) - $terms_per_page; | |
// get all taxonomy terms | |
$terms = get_terms( array ( | |
'taxonomy' => $taxonomy, | |
'order' => 'ASC', | |
'orderby' => 'name', | |
'number' => $terms_per_page, | |
'offset' => $offset, | |
) ); | |
echo '<dl>'; | |
// cycle through taxonomy terms | |
foreach ( $terms as $term ) { | |
echo '<dt>' . $term->description . '</dt>'; | |
echo '<dd>'; | |
echo '<ul>'; | |
// cycle through posts having this term | |
$items = get_posts( array ( | |
'post_type' => $post_type, | |
'tax_query' => array( | |
array( | |
'taxonomy' => $taxonomy, | |
'terms' => $term->term_id, | |
), | |
), | |
'numberposts' => -1, // different from WP_Query (see Code Ref) | |
) ); | |
// essential, see comments inside foreach() loop | |
global $post; | |
foreach ( $items as $item ) { | |
// assign $item to global $post | |
$post = $item; | |
// and now set up | |
setup_postdata( $post ); | |
echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>'; | |
// wp_reset_postdata(); // see below | |
} | |
wp_reset_postdata(); // moved outside the foreach() loop | |
// end posts cycle | |
echo '</ul>'; | |
echo '</dd>'; | |
} | |
// end term cycle | |
echo '</dl>'; | |
// Pagination | |
// See the Code Reference for more arguments | |
echo paginate_links( array ( | |
'total' => $max_num_pages, | |
'current' => $current_page, | |
) ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment