• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Issues with Custom Pagination

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › Issues with Custom Pagination

This topic is: not resolved

Tagged: pagination, WP_query

  • This topic has 5 replies, 3 voices, and was last updated 2 years, 5 months ago by Brad Dalton.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • August 14, 2020 at 11:43 pm #500579
    LanceHillier
    Participant

    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!!

    <?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' => '&larr;',
    				// 'next_text' => '&rarr;',
    				// '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();
    http://onlocalmachine.com
    August 15, 2020 at 2:02 am #500580
    andytc
    Participant

    I 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 #500581
    LanceHillier
    Participant

    Thanks for the reply Andy, missed that one!! but hasn't changed the outcome, unfortunately.

    August 15, 2020 at 7:44 am #500582
    LanceHillier
    Participant

    Thanks 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 #500583
    andytc
    Participant

    Excellent, glad it’s all working

    August 18, 2020 at 11:59 am #500655
    Brad Dalton
    Participant

    Another 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 );
    

    Get Help – Book Consultation.

  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2023 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble