posts - Custom loop pagination links not working

admin2025-01-07  7

I'm bringing up an issue that could not be solved after googling for solutions over a few days. The goal I have to meet is to output a pagination for a custom post type named office-magazines with working links. I already managed to output the pagination but clicking any of the links within it takes me to the top page.

Here's the code I embedded into the page template:

   <?php
        global $wp_query, $paged;
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

        $args = array(
          'post_type' => 'office-magazines',
          'posts_per_page' => 9,
          'paged'          => $paged,
          'has_archieve'  => true
        );

        $catquery = new WP_Query($args);
   ?>

   <p class="pagination">
    <?php echo custom_pagination_bar( $loop ); ?>
  </p>

Passing 'post_type' => 'office-magazines' is meant to filter out all posts apart from the ones belonging to the custom post type, 'office-magazines'.

Using the next code in functions.php, I intended to define the function of the pagination:

function custom_pagination_bar($custom_loop) {
  $big = 999999999;
 echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $custom_loop->max_num_pages
) );
}

The same code works for the main post pagination which is output in a separate template page but fails for the custom post pagination.

Could someone please help me find a solution to getting the custom post pagination links to work?

Hoping for some advice,

Ead

I'm bringing up an issue that could not be solved after googling for solutions over a few days. The goal I have to meet is to output a pagination for a custom post type named office-magazines with working links. I already managed to output the pagination but clicking any of the links within it takes me to the top page.

Here's the code I embedded into the page template:

   <?php
        global $wp_query, $paged;
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

        $args = array(
          'post_type' => 'office-magazines',
          'posts_per_page' => 9,
          'paged'          => $paged,
          'has_archieve'  => true
        );

        $catquery = new WP_Query($args);
   ?>

   <p class="pagination">
    <?php echo custom_pagination_bar( $loop ); ?>
  </p>

Passing 'post_type' => 'office-magazines' is meant to filter out all posts apart from the ones belonging to the custom post type, 'office-magazines'.

Using the next code in functions.php, I intended to define the function of the pagination:

function custom_pagination_bar($custom_loop) {
  $big = 999999999;
 echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $custom_loop->max_num_pages
) );
}

The same code works for the main post pagination which is output in a separate template page but fails for the custom post pagination.

Could someone please help me find a solution to getting the custom post pagination links to work?

Hoping for some advice,

Ead

Share Improve this question edited Apr 12, 2018 at 7:17 Jacob Peattie 44k10 gold badges49 silver badges62 bronze badges asked Apr 12, 2018 at 6:45 FizzlerFizzler 757 bronze badges 3
  • Any reason you're using a custom query and not the main loop? – Jacob Peattie Commented Apr 12, 2018 at 7:18
  • 1 the variable you are passing is wrong here.. it should be $catquery <?php echo custom_pagination_bar( $catquery ); ?> – Aishan Commented Apr 12, 2018 at 7:26
  • Indeed, I did not have to resort using the custom query: I managed to filter the main query for the custom post type in the end. – Fizzler Commented Apr 17, 2018 at 8:13
Add a comment  | 

2 Answers 2

Reset to default 0

Use the following:

$args = array(
  'post_type' => '[custom post type name]',
  'paged' => $paged,
  'has_archieve' => true    
);
$catquery = new WP_Query($args);    
echo pagination_bar($catquery);

The only solution that worked for me happens to add an action filter to functions.php in the following format:

add_action('pre_get_posts', function($q){
    if (is_category()){
        if(is_category('NEWS')):
            $q->set( 'posts_per_page', 2 );
        endif;
    }
});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254573a217.html

最新回复(0)