Skip to content

Instantly share code, notes, and snippets.

@illucent
Created March 7, 2014 15:22
Show Gist options
  • Select an option

  • Save illucent/9413430 to your computer and use it in GitHub Desktop.

Select an option

Save illucent/9413430 to your computer and use it in GitHub Desktop.
pagination
<?php
$posts_per_page = 3; // how many posts per term
$taxonomy = 'category'; // taxonomy
$post_type = 'post'; // post type
$include_terms = array( 1, 25, 58 ); // the terms A B C ids
$post_count = $i = 0;
$custom_terms = get_terms( $taxonomy, array( 'parent' => 0, 'include' => $include_terms ) );
if ( $custom_terms ) :
foreach ( $custom_terms as $term ) {
$post_type_count = (int) get_term_post_type_count( $term->term_id, $taxonomy, $post_type );
if ( $post_type_count > $post_count )
$post_count = $post_type_count;
$custom_terms[$i]->count_post_type = $post_type_count;
++$i;
}
if ( $post_count > 2 ) {
global $wp_query;
$wp_query->max_num_pages = ceil( $post_count / $posts_per_page );
}
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
foreach ( $custom_terms as $custom_term ) :
$args = array(
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $custom_term->slug,
'include_children' => 0
),
)
);
if ( is_paged() ) {
$args['offset'] = ( $paged-1 ) * $posts_per_page;
}
$loop = new WP_Query( $args );
$prev = get_next_posts_link( 'Older posts' );
$next = get_previous_posts_link( 'Newer posts' );
if ( $loop->have_posts() ) :
// category name
echo '<h2>'.$custom_term->name.'</h2>';
?>
<!-- start of loop -->
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<!-- put your loop code here -->
<p><?php the_title(); ?></p>
<?php endwhile; ?>
<!-- end of loop -->
<?php
if ( $prev || $next ) {
if ( $prev )
echo '<div class="nav-previous alignleft">' . $prev . '</div>';
if ( $prev )
echo '<div class="nav-next alignleft">' . $next . '</div>';
}
?>
<!-- end of loop -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment