Wordpress custom post type pagination

2019-06-21 11:36发布

I am trying to get pagination working with the wp pagenavi plugin and a custom post type (portfolio page) in WordPress and I am having no luck.

Here is a stripped down version of my portfolio page:

<?php get_header(); ?>

<?php
  $type = 'portfolio';
  $args=array(
    'post_type' => $type,
    'post_status' => 'publish',
    'paged' => $paged,
    'posts_per_page' => 1,
    'caller_get_posts'=> 1
  );
  $temp = $wp_query;  // assign original query to temp variable for later use   
  $wp_query = null;
  $wp_query = new WP_Query($args); 
?>

<?php if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
...
<?php endwhile; else : ?>
...
<?php endif; ?>

<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
    $wp_query = null; $wp_query = $temp; ?>

<?php get_footer(); ?>

I have my permalinks set to:

/%postname%/

and I have re-saved them.

When I got to page two of my portfolio, I get a 404 page. Any idea why this is happening?

Thanks for the help.

5条回答
做自己的国王
2楼-- · 2019-06-21 11:48

I think you are having a bad case of the old WordPress URL redirect.

Try adding this filter to stop it:

add_filter('redirect_canonical','my_disable_redirect_canonical');

function my_disable_redirect_canonical( $redirect_url ) {
    if ( is_single( 'portfolio' ) )
    $redirect_url = false;
    return $redirect_url;
}
查看更多
Melony?
3楼-- · 2019-06-21 11:49

Custom post type numeric pagination -> http://designphiliconline.blogspot.in/2012/08/wordpress-custom-post-type-pagination.html

This will work with permalink set to default or postname

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-06-21 11:51

This is the way I go to pass the paged variable into the CPT query for navigation, you can have different methods on doing this, but this the only one it has work for me using the WP_QUERY. And no need any plugin for navigation just standard navigation links.

You should place this line over here before you WP_QUERY

 $paged = 1;  
 if ( get_query_var('paged') ) $paged = get_query_var('paged');  
 if ( get_query_var('page') ) $paged = get_query_var('page');

$temp = $wp_query; 
                $wp_query = null; 
                $wp_query = new WP_Query(); 
                $wp_query->query('post_type=portafolio&showposts=7&orderby=post_date&order=DESC&paged='.$paged); 
 while ($wp_query->have_posts()) : $wp_query->the_post(); 

 //post format
get_template_part("content");?>

  <?php endwhile; ?>

  //pagination links here

  <?php 
      $wp_query = null; 
      $wp_query = $temp;  // Reset
   ?>

After you modify your loop file add this to your functions.php this is the variable that will work out the page your on.

 function paginate() {  
   global $wp_query, $wp_rewrite;  
   $wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;  

   $pagination = array(  
    'base' => @add_query_arg('page','%#%'),  
    'format' => '',  
    'total' => $wp_query->max_num_pages,  
    'current' => $current,  
    'show_all' => true,  
    'type' => 'plain'  
    );  

  if ( $wp_rewrite->using_permalinks() ) $pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );  
if ( !empty($wp_query->query_vars['s']) ) $pagination['add_args'] = array( 's' =>   get_query_var( 's' ) );  
echo paginate_links( $pagination );  
}  

 function portafolio_posts_per_page( $query ) {  
    if ( $query->query_vars['post_type'] == 'portafolio' ) $query->query_vars['posts_per_page'] = 10;  
return $query;  
}  
 if ( !is_admin() ) add_filter( 'pre_get_posts', 'portafolio_posts_per_page' );     

You can also read this post is explaining the method of chaining templates this will fix pagination problem that we have to deal when involves custom queries.

http://wp.tutsplus.com/tutorials/custom-post-type-pagination-chaining-method/

查看更多
孤傲高冷的网名
5楼-- · 2019-06-21 11:58

I had a problem with pagination in Wordpress, and couldn't fix it so I rolled my own extention of the WP_Query class -> MF_Query

Simply use MF_Query in place of WP_Query, and then use $your_query->next("Next Page") or $your_query->prev("Previous Page") to add next and previous links (text has defaults, so no arguments required.

It is required that you pass your arguments directly to the class initialisation as an array, as apposed to the various options you have with standard WP_Query.

It's a little hacky, but it works!!

查看更多
Deceive 欺骗
6楼-- · 2019-06-21 12:03
<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
    'post_type' => 'portfolio',
    'paged' => $paged,
    'caller_get_posts' => 1,
    'posts_per_page' => T_Panel('portfolio_page_num')
));
if (have_posts()) :
    while (have_posts()) : the_post();
        // your content loop her
    endwhile;
    wp_reset_query();
endif;

?>
查看更多
登录 后发表回答