简体   繁体   English

带有分页功能的Wordpress自定义帖子类型类别视图

[英]Wordpress Custom Post type Category View with Pagination

I have a custom post type 'charters' and within my category.php page I have a loop pulling in 'charter' posts for the current category id. 我有一个自定义帖子类型“ charters”,在我的category.php页面中,我有一个循环,用于获取当前类别ID的“ charter”帖子。 I have a custom pagination setup displaying page numbers when I click page 2 link, it goes to URL /page/2 which displays a 404 error. 我有一个自定义分页设置,当我单击第2页链接时显示页码,它转到URL / page / 2,它显示404错误。 I can't figure out why I am getting the 404. If I adjust my posts per page the pagination updates and displays the correct number of pages but the links do not show up. 我不知道为什么会得到404。如果我每页调整我的帖子,则分页会更新并显示正确的页面数,但链接不会显示。

Category.php Code: Category.php代码:

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

  $product_args = array(
      'post_type' => 'charters',
      'posts_per_page' => 10, //the same as the parse_query filter in our functions.php file
      'paged' => $paged,
      'page' => $paged,
      'cat' => $cat
    );

  $product_query = new WP_Query( $product_args ); ?>

  <?php if ( $product_query->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?>
      <article class="loop">
        <h3><?php the_title(); ?></h3>
        <div class="content">
          <?php the_excerpt(); ?>
        </div>
      </article>
    <?php endwhile; ?>
    <!-- end of the loop -->


    <!-- pagination here -->
    <?php
       if (function_exists( 'custom_pagination' )) :
          custom_pagination( $product_query->max_num_pages,"",$paged );
      endif;
   ?>


  <?php wp_reset_postdata(); ?>

  <?php else:  ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
  <?php endif; ?>

Functions.php Code: Functions.php代码:

function prefix_change_cpt_archive_per_page( $query ) {

    //* for cpt or any post type main archive
    if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'product' ) ) {
        $query->set( 'posts_per_page', '10' );
    }

}
add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' );


function prefix_change_category_cpt_posts_per_page( $query ) {

    if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
        $query->set( 'post_type', array( 'product' ) );
        $query->set( 'posts_per_page', '2' );
    }

}
add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' );



function custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {

  if (empty($pagerange)) {
    $pagerange = 2;
  }


  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }


  $pagination_args = array(
    'base'            => get_pagenum_link(1) . '%_%',
    'format'          => 'page/%#%',
    'total'           => $numpages,
    'current'         => $paged,
    'show_all'        => False,
    'end_size'        => 1,
    'mid_size'        => $pagerange,
    'prev_next'       => True,
    'prev_text'       => __('&laquo;'),
    'next_text'       => __('&raquo;'),
    'type'            => 'plain',
    'add_args'        => false,
    'add_fragment'    => ''
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class='custom-pagination'>";
      echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
      echo $paginate_links;
    echo "</nav>";
  }

My Permalink Settings are set to "Post Name" I have a feeling this has something to do with rewrites but I can not figure out how to get the page urls to work correctly. 我的永久链接设置设置为“帖子名称”,我感觉这与重写有关,但是我不知道如何使页面URL正常工作。 An example of my categories are as follows: 我的类别的示例如下:

/category/long-beach/ /分类/长海滩/
/category/san-diego/ /类别/ SAN-迭/

you should try this: 您应该尝试这样:

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    'post_type' => 'charters',
    'cat'=> $cat,
    'post_per_page'=> 6, 
    'paged' => $paged 
    );

    $my_query = new WP_Query( $args );

    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p><?php
        endwhile;
    }

    wp_reset_query();

?>

<?php echo pnavigation( $query ); ?>

then you need to add following code to function.php : 那么您需要向function.php添加以下代码:

function pnavigation( $wp_query ) {

  $big = 999999999; // need an unlikely integer
  $pages = paginate_links( array(
  'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
  'format' => '?paged=%#%',
  'current' => max( 1, get_query_var('paged') ),
  'total' => $wp_query->max_num_pages,
  'prev_next' => false,
  'type'  => 'array',
  'prev_next'   => TRUE,
  'prev_text'    => '&larr;',
  'next_text'    => '&rarr;',
  ) );
  if( is_array( $pages ) ) {
    $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
    echo '<ul class="pagination pagination-lg">';
    foreach ( $pages as $page ) {
      echo "<li>$page</li>";
    }
    echo '</ul>';
  }
}

you need to just replace url (client-testimonials) with your url 您只需要用url替换url(客户推荐)

function pagination_rewrite() {
  add_rewrite_rule('client-testimonials/page/?([0-9]{1,})/?$', 'index.php?pagename=client-testimonials&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');

If you are using default pagination then please use below code. 如果您使用默认分页,请使用以下代码。

<?php
$current_page = get_queried_object();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'charters',
'paged'         => $paged,
'cat'=> $cat);

$my_query = new WP_Query( $args );

if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p><?php
    endwhile;
}
next_posts_link( 'Older Entries', $my_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
wp_reset_query();
?>

Add below code in your function.php 在您的function.php中添加以下代码

function posts_on_categorypage( $query ) {
    if ( $query->is_category()) {
        $query->set( 'posts_per_page', '10' );
    }
}
add_action( 'pre_get_posts', 'posts_on_categorypage' );

There was a couple of errors in my code. 我的代码中有几个错误。 This is what I get for copy/pasting code and not going through it line by line. 这是我获得的复制/粘贴代码,而不是逐行通过代码。 Thanks everyone who helped out, but I got this to work by changing: 感谢所有提供帮助的人,但我通过更改以下内容使它起作用:

Bad Code: 错误代码:

function prefix_change_category_cpt_posts_per_page( $query ) {

    if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
        $query->set( 'post_type', array( 'product' ) );
        $query->set( 'posts_per_page', '2' );
    }

}

Good Code: 好代码:

function prefix_change_category_cpt_posts_per_page( $query ) {

    if ( $query->is_main_query() && ! is_admin() && is_category( $cat ) ) {
        $query->set( 'post_type', array( 'charters' ) );
        $query->set( 'posts_per_page', '10' );
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM