• 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

Pagination issue with a custom category template

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 › Pagination issue with a custom category template

This topic is: resolved

Tagged: broken pagination, custom loop

  • This topic has 10 replies, 4 voices, and was last updated 5 years, 11 months ago by vannt93.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • September 13, 2016 at 8:45 am #193085
    studionoobly
    Member

    So I have the following code that is the basic backbone of a customised category.php page but I can't get the pagination links to show:

    					remove_action ('genesis_loop', 'genesis_do_loop');
    					add_action( 'genesis_loop', 'custom_do_press_loop' );
    
    					function custom_do_press_loop() {
    
    							$args = array(
    							'post_type' => 'post',
    							'posts_per_page' => 3,
    							'orderby' => 'date',
    							'order' => 'DSC',
    					);
    
    						$loop = new WP_Query( $args );
    						if( $loop->have_posts() ):
    
    							while( $loop->have_posts() ): $loop->the_post(); global $post;
    
    							endwhile;
    
    						endif;
    					}
    
    					genesis();
    

    I have also tried the following but that gave me a next link that took me to a 404 page and I think it might be deprecated so not using it any more.

    					remove_action ('genesis_loop', 'genesis_do_loop');
    					add_action( 'genesis_loop', 'custom_do_press_loop' );
    
    					function custom_do_press_loop() {
    							global $paged;
    							global $query_args;
    							$args = array(
    								'post_type' => 'post',
    								'post_status' => 'publish',
    								'posts_per_page' => '3',
    								'cat'	 => 'category_name',
    								'orderby'	=> 'post_date',
    								'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1
    					);
    
    						global $wp_query;
    						$wp_query = new WP_Query( $args );
    						if ( have_posts() ) :
    
    							while ( have_posts() ) : the_post();
    
    						endwhile;
    							do_action( 'genesis_after_endwhile' );
    						endif;
    
    					wp_reset_query();
    
    					}
    
    					genesis();
    

    In the 'Reading Settings' I have 'Posts page' set to nothing and a static front page, there isn't a default category blog page as such on the website. And my 'Permalink Settings' are "/%category%/%postname%/" "Category base" = .

    This along with setting URL's to be "www.category-name/sub-category/sub-category-post" is driving me nuts. Any help on this would be most appreciated.

    Thanks,

    http://localhost
    September 13, 2016 at 3:25 pm #193115
    Brad Dalton
    Participant

    Hi

    What are you trying to do because the code you posted doesn't output anything and isn't coded correctly.


    Recent Client work

    September 14, 2016 at 12:59 am #193146
    studionoobly
    Member

    Hi Brad,

    Oh, well that's not a good start. I'm just trying to create a custom category template page for all categories. Within the loop I have my custom html that I need to be different from the standard Genesis fair.

    Here is the full code that I'm using just now.

    <?php
    /**
     * This is Category file.
     *
     * @package  Genesis-Child-Theme
     * @since    1.0.0
     */
    
    /**
     * Genesis custom loop
     */
    /** Replace the standard loop with our custom loop */
    remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop
    add_action( 'genesis_loop', 'custom_do_press_loop' ); // Add custom loop
    
    function custom_do_press_loop() {
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		$args = array(
    		'post_type' => 'post',
    		'cat' => 'category_name',
        'posts_per_page' => 3,
    		'orderby' => 'date',
    		'order' => 'DSC',
    		'paged' => $paged
    );
    
    echo '<section>';
    	/*
    	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
    	*/
    	$loop = new WP_Query( $args );
    	if( $loop->have_posts() ):
    
        while( $loop->have_posts() ): $loop->the_post(); global $post;
    
    echo '<article>';
    
    echo '<div class="entry-content grey">';
    
      echo '<header class="entry-header block-link-entry-header">';
        echo '<h2 class="entry-title block-link-entry-title">' . get_the_title() . '</h2>';
      echo '</header>';
    
      echo '<p>' . get_the_date() . '</p>';
    
      echo '<p>'  . get_the_excerpt() . '</p>';
    
    echo '</div>';
    
      echo '<a href="' . get_permalink() . '" class="block-link image-block-link">';
    
        echo get_the_post_thumbnail();
    
      echo '</a>';
    
    echo '</article>';
    
    		endwhile;
    		do_action( 'genesis_after_endwhile' );
    
    echo '</section>';
    
    	wp_reset_query();
      endif;
    
    }
    
    genesis();
    
    September 14, 2016 at 9:36 pm #193177
    Genesis Developer
    Member

    Here is the updated code http://pastebin.com/rh6j6JRF.

    You used 'cat' => 'category_name',. I am not sure that it will work correctly. Parameter "cat" is accepting the category ID not cat name or cat slug


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    September 14, 2016 at 10:04 pm #193178
    Brad Dalton
    Participant

    You could also use genesis_custom_loop() if you wanted to maintain the default markup.


    Recent Client work

    September 15, 2016 at 8:13 am #193190
    studionoobly
    Member

    Thanks Brad,

    Still getting a 404?

    So I installed the 'Debug This' plugin and checked the Query > Rewrites and got the following

    Matched Rule: (.+?)/([^/]+)(?:/([0-9]+))?/?$
    Matched Query: category_name=music&name=page&page=2
    Query String: page=2&name=page&category_name=music&debug-this=rewrites

    I found this article http://meigwilym.com/fixing-the-wordpress-pagination-404-error/

    and tried the following code in my functions but no joy? (edit and flushed the permalinks)

    function mg_news_pagination_rewrite() {
      add_rewrite_rule(get_option('category_base').'/([^/]+)(?:/([0-9]+))?/?$', 'index.php?pagename='.get_option('category_base').'&paged=$matches[1]', 'top');
    }
    add_action('init', 'mg_news_pagination_rewrite');
    

    Really not sure what to do now?

    September 15, 2016 at 9:59 am #193201
    Genesis Developer
    Member

    ..


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    September 15, 2016 at 10:10 am #193203
    studionoobly
    Member

    Yup, tried your code too, thanks but it still retrieves to the 404 message

    September 15, 2016 at 10:18 am #193204
    Genesis Developer
    Member

    Yes. It is not working. Sorry for it.


    Download Genesis Featured Posts Combo Widget | Simple Grid Layouts Plugin for Posts, CPTs and terms
    You can request new tips/help.

    September 16, 2016 at 8:42 am #193239
    studionoobly
    Member

    OK so I finally figured out what was causing the issue. I am using Custom Meta Boxes 2 and on some templates I'm using repeatable fields and if you don't use:

    <?php if ( ! empty( $term ) ) : ?> don't forget the <?php endif; ?> at the end.

    Then on empty fields that are not filled in by the user you will end up with errors that can apparently reek havoc with things like pagination.

    September 18, 2016 at 7:23 am #193308
    vannt93
    Member

    Useful! Thanks

  • Author
    Posts
Viewing 11 posts - 1 through 11 (of 11 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

© 2022 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