Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Genesis custom loop with pagination
<?php
/* Template Name: Test */
/**
* Genesis custom loop
*/
function be_custom_loop() {
global $post;
// arguments, adjust as needed
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'post_status' => 'publish',
'paged' => get_query_var( 'paged' )
);
/*
Overwrite $wp_query with our new query.
The only reason we're doing this is so the pagination functions work,
since they use $wp_query. If pagination wasn't an issue,
use: https://gist.github.com/3218106
*/
global $wp_query;
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
echo '<ul>';
while ( have_posts() ) : the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
echo '</ul>';
do_action( 'genesis_after_endwhile' );
endif;
wp_reset_query();
}
add_action( 'genesis_loop', 'be_custom_loop' );
remove_action( 'genesis_loop', 'genesis_do_loop' );
genesis();
@swapnilg

This comment has been minimized.

Show comment
Hide comment
@swapnilg

swapnilg Jul 21, 2013

Thanks Bill :)

Thanks Bill :)

@WebEndevSnippets

This comment has been minimized.

Show comment
Hide comment
@WebEndevSnippets

WebEndevSnippets Sep 11, 2013

Thank you Bill!

Thank you Bill!

@craiggrella

This comment has been minimized.

Show comment
Hide comment
@craiggrella

craiggrella May 3, 2014

Bill,

I'm using this snippet in local dev environment with 15 posts on custom post type. Posts per page set to 5.
First page fine. second page fine. Third and final page - 404 error.
Are there any other consideratios I might need for fixing pagination?

Bill,

I'm using this snippet in local dev environment with 15 posts on custom post type. Posts per page set to 5.
First page fine. second page fine. Third and final page - 404 error.
Are there any other consideratios I might need for fixing pagination?

@poblabs

This comment has been minimized.

Show comment
Hide comment
@poblabs

poblabs Aug 10, 2014

It may help someone out there, but on line 13 I made a change on my site for it to use the options set in WordPress Settings > Reading > Blog Pages Show at Most. This way you can update the settings, and have it reflect without changing the code.

'posts_per_page' => get_option('posts_per_page'),

poblabs commented Aug 10, 2014

It may help someone out there, but on line 13 I made a change on my site for it to use the options set in WordPress Settings > Reading > Blog Pages Show at Most. This way you can update the settings, and have it reflect without changing the code.

'posts_per_page' => get_option('posts_per_page'),

@anythinggraphic

This comment has been minimized.

Show comment
Hide comment
@anythinggraphic

anythinggraphic Dec 3, 2014

Thank you! I was having trouble with this.

Thank you! I was having trouble with this.

@webmatter

This comment has been minimized.

Show comment
Hide comment
@webmatter

webmatter Mar 18, 2017

this one finally worked ... thanks!

this one finally worked ... thanks!

@danbru1989

This comment has been minimized.

Show comment
Hide comment
@danbru1989

danbru1989 Jan 27, 2018

Worked when I added genesis_posts_nav(); after the while statement like shown on the blog post: https://www.billerickson.net/code/pagination-in-a-custom-query/

danbru1989 commented Jan 27, 2018

Worked when I added genesis_posts_nav(); after the while statement like shown on the blog post: https://www.billerickson.net/code/pagination-in-a-custom-query/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment