自定义帖子类型显示分页,但显示404个

问题描述 投票:0回答:1

我无法使用分页来处理自定义帖子类型。

它显示正确,当我单击它时,转到/ page / 2和/ page / 3等,所有这些都给出404页面未找到错误。

index.php

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type'         => array('name1', 'name2', 'name3', 'post'),
    'posts_per_page'    => 4,
    'post_status'       => 'publish',
    'paged'         => $paged
);
$query = new WP_Query( $args );

?>
<?php echo $paged; ?>
<?php if ($query->have_posts()) : ?>
    <?php while ($query->have_posts()) : $query->the_post(); ?>

        // do stuff here
        <?php the_excerpt(); ?> 

    <?php endwhile; ?>
    // get pagination                            
    <?php mypagination( $query ); ?>

<?php endif; ?>

<?php wp_reset_query(); ?>

functions.php

内置the_posts_pagination()不会输出任何内容。

function mypagination($query){ ?>
<ul class="pagination">
    <?php
        $pages = paginate_links( array(
            'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
            'total'        => $query->max_num_pages,
            'current'      => max( 1, get_query_var( 'paged' ) ),
            'format'       => '?paged=%#%',
            'show_all'     => true,
            'type'         => 'array',
            'end_size'     => 2,
            'mid_size'     => 1,
            'prev_next'    => true,
            'prev_text'    => '<span class ="fa fa-caret-left" aria-hidden="true"></span><span class="prev-text">Prev</span>',
            'next_text'    => '<span class="next-text">Next</span> <span class ="fa fa-caret-right" aria-hidden="true"></span>',
            'add_args'     => false,
            'add_fragment' => '',
        ) );

    if (is_array($pages)):
        foreach ($pages as $p): ?>
        <li class="pagination-item js-ajax-link-wrap">
            <?php echo $p; ?>
        </li>
        <?php endforeach;
    endif; ?>
</ul>
<?php
}

如果我使用相同的查询/参数创建自定义API端点,则分页有效。

/**
 * Custom JSON API endpoint.
 * Endpoint URL http://localhost/websitename/wp-json/frontpage/v1
 */
function api_frontpage_posts($data) {
  $output = array();

  // Get post slug.
  $post = get_post($data['parent_id']);
  $slug = $post->post_name;

  // e.g. 1, 2, 3,...
  $paged = $data['page_number'] ? $data['page_number'] : 1;
  $query_args = array(
      'post_type' => array('name1', 'name2', 'name3', 'post'), 
      'post_status' => array('publish'),
      'posts_per_page' => 4,
      'paged' => $paged, 
  );

  // Create a new instance of WP_Query
  $the_query = new WP_Query($query_args);

  if (!$the_query->have_posts()) {
      return null;
  }

  while ($the_query->have_posts()) {
      $the_query->the_post();

      $post_id = get_the_ID();
      $post_title = get_the_title();
      $post_content = get_the_content();
      $post_excerpt = get_the_excerpt();
      $post_date = get_the_date('l, j F Y');

      // Get the slug.
      $post = get_post($post_id);
      $post_slug = $post->post_name;

      // Get image alt.
      $alt = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_alt', true);
      $desc = get_post_meta(get_post_thumbnail_id(), '_wp_attachment_image_caption', true);

      // Get image description.
      $description = null;
      $thumbnail = get_posts(array('p' => get_post_thumbnail_id(), 'post_type' => 'attachment'));
      if ($thumbnail && isset($thumbnail[0])) {
          $description = $thumbnail[0]->post_content;
      }

      // Image data.
      $post_image = array(
          'id' => get_post_thumbnail_id() ,
          'url'  => get_the_post_thumbnail_url(),
          'caption' => get_the_post_thumbnail_caption(),
          'alt' => $alt,
          'description' => $description,
      );

      // Category data.
      $categories = get_the_category($post_id);
      $post_categories = array();
      foreach ($categories as $key => $category) {
          $post_categories[] = array(
              'name' => $category->name,
              'slug' => $category->slug,
              'url'  => get_category_link($category->cat_ID),
          );
      }

      // Push the post data into the array.
      $output[] = array(
          'id' => "post" . $post_id, // cannot use numbers, or hyphens between words for Zurb data toggle attribute
          'slug' => $post_slug,
          'title' => $post_title,
          'url' => get_permalink($post_id),
          'date' => $post_date,
          'content' => $post_content,
          'excerpt' => $post_excerpt,
          'image' => $post_image,
          'categories' => $post_categories
      );
  }

  $result = array(
      "next" => (int)$paged === (int)$the_query->max_num_pages ? null :  site_url() . '/' . $slug . '/page/' . ($paged + 1) . '/',
      "prev" => (int) $paged === 1 ? null : site_url() . '/' . $slug . '/page/' . ($paged - 1)  . '/',

      // Split the array every 4 items.
      "chunks" => array_chunk($output, 4)
  );

  // Reset the post to the original after loop. otherwise the current page
  // becomes the last item from the while loop.
  wp_reset_query();

  // Return json.
  return $result;
}
add_action('rest_api_init', function () {
  register_rest_route('frontpage/v1', '/page/(?P<page_number>\d+)', array(
      'methods' => 'GET',
      'callback' => 'api_frontpage_posts',
  ));
}); 
wordpress pagination custom-post-type
1个回答
0
投票

(hooray Reddit)有人指出我要解决的问题。

也通过https://wordpress.stackexchange.com/questions/22528/custom-post-type-pagination-404-fix将其添加到functions.php中>

add_action( 'parse_query','changept' );
function changept() {
    if( is_category() && !is_admin() )
        set_query_var( 'post_type', array( 'post', 'your_custom_type' ) );
    return;
}
© www.soinside.com 2019 - 2024. All rights reserved.