Community Forums › Forums › Archived Forums › General Discussion › Issues with Custom Pagination
Tagged: pagination, WP_query
- This topic has 5 replies, 3 voices, and was last updated 3 years, 8 months ago by Brad Dalton.
-
AuthorPosts
-
August 14, 2020 at 11:43 pm #500579LanceHillierParticipant
Hi All,
I wonder if anyone has a moment to run eyes over this code looking at the pagination.
The issue is, it's an archive page listing posts 'work' by the 'galleries' taxonomy and I'm trying to add pagination to the page.
This code produces the right amount of pagination links but the second page shows just a duplicate of the first page, and then the next page throws a 404.
Thanks you!!
http://onlocalmachine.com<?php /** * * Template Name: Gallery Archives * */ // remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' ); remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'lh_gallery_loop' ); /** * Custom loop that displays a list of galleries with corresponding posts. */ function lh_gallery_loop() { // number of tags to show per-page $per_page = 2; // $offset = ( $page - 1 ) * $per_page; $term_args = array( 'number' => $per_page // 'offset' => $offset ); $galleries = get_terms('galleries', $term_args,); // $gallery_term_count = count(get_terms('galleries')); // $gallery_term_count = count( $galleries ); $gallery_term_count = count(get_terms('galleries')); // function reset_variable(){ // unset($term_args); // $gallery_term_count = count( $galleries ); // } if ($galleries) { foreach ( $galleries as $gallery ) { // Display gallery name echo '<h2 class="gallery-title">' . $gallery->name . '</h2>'; echo '<div class="gallery-list">'; // WP_Query arguments $args = array( 'post_type' => 'work', 'tax_query' => array( array( 'taxonomy' => 'galleries', 'field' => 'term_id', 'terms' => $gallery->term_id, ), ), ); // The Query $query = new WP_Query( $args ); // The Loop if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); $image = get_field( 'image' ); $sold = get_field( 'availability' ); $url = get_permalink(); $image = get_field( 'image' ); if ( $image ) : ?> <a class="work-images" href="<?php echo $url; ?>"><img src="<?php echo esc_url( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" /> <?php endif; // Add red dot to work if sold if ( $sold ) : echo '<span class="dot"></span><p class="gallery-title gallery-title-sold">' . get_the_title() . '</p>' ?> <?php else: ?> <?php echo '<p class="gallery-title gallery-title-available">' . get_the_title() . '</p>' ?> <?php endif; ?> </a> <?php } // End while } // End if echo '</div>'; // Restore original Post Data wp_reset_postdata(); } } //Pagination Function function lancehillier_do_taxonomy_pagination( $gallery_term_count, $per_page ) { $total = ceil($gallery_term_count / $per_page); $current = get_query_var( 'paged' ); $base = esc_url_raw( str_replace( 999999999, '%#%', get_pagenum_link( 999999999, false ) ) ); $format = '?paged=%#%'; if ( $total <= 1 ) { return; } ?> <nav class="pagination"> <?php echo paginate_links( [ 'base' => $base, 'format' => $format, 'add_args' => false, 'current' => max( 1, $current ), 'total' => $total, // 'prev_text' => '←', // 'next_text' => '→', // 'type' => 'list', 'end_size' => 3, 'mid_size' => 3, ] ); ?> </nav> <?php } //add pagination if (function_exists("lancehillier_do_taxonomy_pagination")) { lancehillier_do_taxonomy_pagination($gallery_term_count, $per_page); } } //if ends // Start the engine. genesis();
August 15, 2020 at 2:02 am #500580andytcParticipantI can see one error being thrown on line 25 shown below -
php: error - error - unexpected ')' in Standard input code $galleries = get_terms('galleries', $term_args,);
Remove the comma after the $term_args as below -
$galleries = get_terms('galleries', $term_args);
Don't know if that will be the final solution , but try it anyway
August 15, 2020 at 3:29 am #500581LanceHillierParticipantThanks for the reply Andy, missed that one!! but hasn't changed the outcome, unfortunately.
August 15, 2020 at 7:44 am #500582LanceHillierParticipantThanks Andy, I have this fixed now, it ended up being a permalink conflict because of the way I had the page set up, it was the same as the CPT which after changing it to something else everything was fine.
Thank you!
August 15, 2020 at 11:46 am #500583andytcParticipantExcellent, glad it’s all working
August 18, 2020 at 11:59 am #500655Brad DaltonParticipantAnother thing, you could add a check for ACF
if ( ! class_exists( 'acf' ) ) return;
Or
Use WordPress custom field functions rather than plugin specific functions
get_post_meta( get_the_ID(),'availability', true );
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.