如何修复自定义WordPress循环与分页在单一帖子视图?

时间:2015-10-13 作者:Marz

我有一个自定义的帖子类型,名为“Releases“使用以下代码完美地设置和分页:

The Loop

    <?php
    $paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
    $args = array( \'post_type\' => \'releases\', \'posts_per_page\' => 3, \'paged\' => $paged, \'page\' => $paged );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); 
    ?>

       <!-- Loop Content -->
          <article>
            <h3><?php the_title(); ?></h3>
            <div><?php the_excerpt(); ?></div>
          </article>
        <!-- end Loop Content -->

    <?php endwhile; ?>

       <!-- Pagination -->
    <?php
        if (function_exists(\'single_view_pagination\')) {
            single_view_pagination($loop->max_num_pages,"", $paged);
        }
    ?>
       <!-- end Pagination -->

The Function

 function single_view_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>";
          echo "<ul>";
       echo "<li>" . $paginate_links . "</li> ";
            echo "</ul>";
     echo "</nav>";
   }
 }

The Problem

现在,当使用上面的代码并查看Archive 对于自定义post类型,所有内容都能正常工作,分页URL链接如下:

http://localhost/releases/page/2   
http://localhost/releases/page/3
但是,在查看Single Post 对于自定义帖子类型,问题是分页URL更改为:

http://localhost/releases/post-name/page/2  
http://localhost/releases/post-name/page/3  
是否有任何方法可以更新此代码或设置新函数以获取pagination URLs on the Single Post view 正确链接(&A);没有像这样的URL中的帖子名称?

http://localhost/releases/page/1  
http://localhost/releases/page/2  
http://localhost/releases/page/3  
非常感谢您的帮助。提前感谢!

Edit:这是一张截屏图,显示了我正试图解决的问题。我需要函数output the page number links for the post type “发布”,instead of outputting the page number links of the current post “职位名称。”Help Screenshot

2 个回复
最合适的回答,由SO网友:Marz 整理而成

在上面Milo代码的帮助下,我可以使用以下方法找到解决方案:

\'base\' => get_pagenum_link(1) . \'%_%\',
希望这可以帮助其他可能遇到此问题的人。谢谢

SO网友:Milo

您需要正确设置基,以便函数知道分页链接相对于哪个URL。

假设/releases/ 是post类型的存档,您可以尝试:

\'base\' => get_post_type_archive_link( \'releases\' ) . \'%_%\',