Ask your WordPress questions! Pay money and get answers fast! (more info)

Genesis custom loop not paginating correctly on front-page.php WordPress

  • REFUNDED

I'm working on a custom Genesis child theme that uses a static front page set under Settings > Reading. I want to display the primary loop + a second, custom loop on this page using the genesis_custom_loop function.


//* Create a custom loop using genesis_custom_loop()
function xx_do_custom_loop() {

// Use query var "page" for front page and "paged" for all other pages
$paged = (get_query_var('page')) ? get_query_var('page') : 1; // current paginated page
$postsperpage = get_option('posts_per_page'); //Get the posts per page value from Settings

$args = array(
'post_type' => 'post',
'posts_per_page' => $postsperpage,
'paged' => $paged,
'order_by' => 'date',
'order' => DESC
);

genesis_custom_loop( $args );

}

//* Output custom loop
add_action( 'genesis_after_loop', 'xx_do_custom_loop' );



This works, the only problem is it doesn't paginate correctly. It shows the "Next Page" link, which takes you to /page/2/ and shows the correct posts for that page, but on page 2 the "Next Page" link still points to /page/2/ and the "Previous Page" link doesn't appear. If I manually navigate to /page/3/ etc. in the address bar, the correct posts are displayed but "Next Page" always points to /page/2/

I've also tried building a completely custom loop, with both query_posts and a new wp_query and the result is exactly the same in all cases. Pagination DOES work if I create a custom page template with this snippet and assign it to a standard inner page. From what I understand though, paginating a custom loop on a static front page should be entirely possible, e.g. https://wordpress.org/support/topic/custom-post-type-pagination-on-front-page

This is happening on a fresh, up-to-date installation of WP with nothing on it but Genesis & the Genesis Sample child theme.

Answers (1)

2015-04-04

timDesain Nanang answers:

Hi,
you should add global $paged inside the function
try this code:

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'wpq_do_loop' );
function wpq_do_loop() {
global $paged;

$args = (array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'post_status' => 'publish',
'paged' => $paged,
'ignore_sticky_posts' => 1,
));

genesis_custom_loop( $args );
}


maustin89 comments:

Hi! Tried your code, it has the same problem as mine, but it's actually worse because the same posts are displayed on every page, whereas my code displays the correct posts for the various pages and just doesn't properly navigate between them.

This is because the front page uses the "page" variable instead of "paged" (don't ask me why). If I echo get_query_var('page') then it gives the correct pagination value, e.g. on /page/2/ it outputs "2", on /page/3/ it outputs "3", etc. so don't think that's the problem.


timDesain Nanang comments:

ups, i am sorry.

try this following code,
it's works on my environment.

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'wpq_do_loop' );
function wpq_do_loop() {
global $paged;
$page = (get_query_var('page'));

$args = (array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'post_status' => 'publish',
'paged' => (is_front_page() ? $page : $paged),
'ignore_sticky_posts' => 1,
));

genesis_custom_loop( $args );
}


maustin89 comments:

Thank you for the update. I tried your code on my test installation and I'm still getting the same result. I did notice that NUMERIC pagination PARTIALLY works though, see here: http://i.imgur.com/hnDH9w0.png

Clicking on the number takes you to the correct page, but the "Next Page" link is still stuck on /page/2/ no matter which page you're on, and the "Previous Page" link never appears. You can see in the screenshot that it's on page 3 so there should be a "Previous Page" link, but there isn't. Is the same true in your environment? Additionally, I'm using "Next/Prev" pagination for the theme I'm working on (set on the Genesis options page in the dashboard) so there are no numeric links, just "Next Page" and "Previous Page" links.

Right now I'm testing this using the most current versions of Genesis & Genesis Sample with no modifications other than this; your code is the only contents of front-page.php beside "genesis();"


timDesain Nanang comments:

- go to reading setting, then select "Your latest posts" as Front page displays (it will render front-page.php automatically)
- and use following code instead:


remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'wpq_do_loop' );
function wpq_do_loop() {
global $paged, $page;

$args = (array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'post_status' => 'publish',
'paged' => $paged,
'ignore_sticky_posts' => 1,
));

genesis_custom_loop( $args );
}

genesis();