Pagination issues in custom single.php page
-
Firstly; I have searched high and low for a solution to the problem I am having (over the last 2-3 weeks), but nothing I have done has been able to get the pagination working for a post displaying a grid of posts. I would include more links in this post (including website in question) but I am limited to posting 2 urls.
(The theme being used is Divi, by Elegant Themes. ..child theme is being used)
So:
I have created a CPT and am using Advanced Custom Fields (ACF) to enable a simple admin page for the user to enter details (for each new post). This creates the new post of the CPT.Posts that are created with this CPT are automatically listed on this page: 4wdaus.com/travel-blog
When a post from the Travel Blog page is selected for viewing, it displays an introductory page [post] that displays a grid of all blog posts with the category that is relevant for that page (trip) – not the CPT. It is this page where I would like the pagination, and these are the pages where the problem is.
The code for creating the CPT is this:
?php function major_trips_cpt() { $args = array( 'labels' => array( 'name' => 'Major Trips', 'singular_name' => 'Major Trip', 'all_items' => 'All Major Trips', ), 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields' ), 'menu_position' => 4, 'show_ui' => true, 'show_in_menu' => true, 'show_in_admin_bar' => true, 'description' => ( 'New Major Trip Entry'), 'public' => true, 'has_archive' => true, 'menu_icon' => get_stylesheet_directory_uri() . '/images/fwdaus-icon.png', 'rewrite' => array('slug' => '4wdaus-major_trips') ); register_post_type( 'major_trip', $args ); } add_action( 'init', 'major_trips_cpt' ); ?>
..and for displaying the grid of posts in the custom single.php (in the child theme) is:
<?php $args = array( 'post_type' => 'post', 'posts_per_page' => 6, 'order' => 'desc', 'orderby' => 'date', 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => get_field('category_to_display'), // category from the Advanced Custom Field variable ), ), ); if ( get_query_var('paged') ) { $args['paged'] = get_query_var('paged'); } elseif ( get_query_var('page') ) { $args['paged'] = get_query_var('page'); } else { $args['paged'] = 1; } $the_query = new WP_Query( $args ); // Pagination fix $temp_query = $wp_query; $wp_query = NULL; $wp_query = $the_query; ?> <div id="fwd-blog-grid"> <?php if ( $the_query->have_posts() ) : ?> <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> <div class="fwd-grid-item"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?> <h2><a href='<?php the_permalink(); ?>'><?php the_title(); ?></a></h2> <div class="entry-content"> <a href='<?php the_permalink(); ?>'><?php the_excerpt(); ?></a> </div> </div> <?php endwhile; ?> <?php wp_reset_postdata(); echo "<div style='clear: both;'>"; //wp_pagenavi( array( 'query' => $the_query ) ); echo "<div style='float: left;'>"; next_posts_link( '« Older Posts' ); echo "</div>"; echo "<div style='float: right;'>"; previous_posts_link( 'Newer Posts »' ); echo "</div>"; echo "</div>"; // Reset main query object $wp_query = NULL; $wp_query = $temp_query; ?> </div>
This page (amongst many many others I have read) would appear the best resource I can find, but maybe there is something I haven’t understood? How to fix pagination for custom loops?
Maybe some useful info
– the page displaying the post grid to be paginated is a Post as opposed to a page (with reference to WP terminology)
– the grid of posts being displayed (where pagination is not working) are not of the CPT. They are displayed as per the relevant category – an ACF variable
– when mouse pointer hovers over ‘Older Posts’ link, url is shown with ‘/page/2/’ added, but when clicked, the first initial page is shown
– if tested, the variable of ‘max_num_pages’ will return a number greater than 1, so there are numerous pages to display
– I am not able to manually enter the URL with /page/2/ added, in the address bar. Initial page is displayed.
– if I enter a fixed category for the post grid (as opposed to the ACF variable) it makes no difference, except for displaying a grid of posts of the same category on each page
– if I enter a number for ‘paged’ in code. eg. ‘$args[‘paged’] = 3′ the third page will actually be displayed
— with the previously mentioned test, the link for ‘Newer Posts’ is still not displayed. Only ‘Older Posts’ is displayed.I realise this is probably a reasonably long post and maybe there are still important details I have not mentioned, but here’s hoping that someone might have some suggestions.
Thanks
- The topic ‘Pagination issues in custom single.php page’ is closed to new replies.