Skip to content

Instantly share code, notes, and snippets.

@ikushlianski
Last active September 23, 2017 20:16
Show Gist options
  • Save ikushlianski/0661ddec7c6cb2a9cf962b09c03e0c2c to your computer and use it in GitHub Desktop.
Save ikushlianski/0661ddec7c6cb2a9cf962b09c03e0c2c to your computer and use it in GitHub Desktop.
Wordpress: pagination with custom posts. The key is to use the DEFAULT loop, with the necessary data for the loop coming from the name of the file itself, in this case: taxonomy-skill_tag.php. The WP_Query stuff will work only if default post number display options is set to 1
// fix bug when custom posts archive pagination does not work on page 2 and subsequent.
// This is the hack which works when WP_Query (not default loop) is used
$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
function my_option_posts_per_page( $value ) {
global $option_posts_per_page;
if ( is_tax( 'skill_tag') ) {
return 1;
} else {
return $option_posts_per_page;
}
}
<?php get_header(); ?>
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<div id="container">
<div id="content" role="main">
<header class="page-heading section">
<h1 class="page-title"><?php echo $term->name; ?></h1>
</header><!-- .entry-header -->
<div class="entries-wrapper">
<?php while ( have_posts() ) : the_post(); ?>
<div class="post type-post entry">
<img class="entry__image" src="<?php the_field('skill_image'); ?>"/>
<div class="entry__details-wrapper">
<h3 class="entry__title">
<?php
if ($term->term_id != 250 && $term->term_id != 252) : ?>
<a href="<?php echo get_permalink(); ?>"
title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?>
</a>
<?php else : the_title(); ?>
<?php endif; ?>
</h3>
<div class="entry__summary">
<?php if ( get_field('skill_completion_status') ) :?>
<p class="label label-future-skill"><?php the_field('skill_completion_status') ?></p>
<?php endif; ?>
<?php the_field('short_description'); ?>
</div><!-- .entry-summary -->
</div>
</div>
<?php endwhile; ?>
</div>
<?php
wp_reset_query();
?>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Previous', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>
<?php get_header(); ?>
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'skills',
// 'nopaging' => true,
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => 2,
'posts_per_archive_page' => 2,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'skill_tag',
'field' => 'name',
'terms' => $term->name
)
)
);
if ($term->term_id == 250 || $term->term_id == 252) {
$args['meta_key'] = 'skill_completion_status';
$args['orderby'] = 'meta_value';
$args['order'] = 'DESC';
}
$currentlyViewedSkillGroup = new WP_Query( $args );
?>
<div id="container">
<div id="content" role="main">
<header class="page-heading section">
<h1 class="page-title"><?php echo $term->name; ?></h1>
</header><!-- .entry-header -->
<?php if ($currentlyViewedSkillGroup->have_posts()) : ?>
<div class="entries-wrapper">
<?php while ( $currentlyViewedSkillGroup->have_posts() ) : $currentlyViewedSkillGroup->the_post(); ?>
<div class="post type-post entry">
<img class="entry__image" src="<?php the_field('skill_image'); ?>"/>
<div class="entry__details-wrapper">
<h3 class="entry__title">
<?php
if ($term->term_id != 250 && $term->term_id != 252) : ?>
<a href="<?php echo get_permalink(); ?>"
title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?>
</a>
<?php else : the_title(); ?>
<?php endif; ?>
</h3>
<div class="entry__summary">
<?php if ( get_field('skill_completion_status') ) :?>
<p class="label label-future-skill"><?php the_field('skill_completion_status') ?></p>
<?php endif; ?>
<?php the_field('short_description'); ?>
</div><!-- .entry-summary -->
</div>
</div>
<?php endwhile; ?>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $currentlyViewedSkillGroup->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
</div>
<?php endif;
wp_reset_postdata(); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment