【问题标题】:adding pagination to a custom post type in wordpress在 wordpress 中为自定义帖子类型添加分页
【发布时间】:2014-11-02 16:55:53
【问题描述】:

我在向我在 Wordpress 中创建的自定义帖子类型添加分页时遇到了一个小问题。分页链接出现在我列出了所有帖子的模板上,在自定义帖子类型中,但当我单击链接查看旧帖子时,它会打开第二页,但显示第一页中的相同帖子那里。此外,在第二页上,“旧帖子”链接不会更新为“../page/3”,而是保持“../page/2”。我按照此处指定的步骤 (https://stackoverflow.com/a/18325002/2115001) 并根据“选项 2”下列出的信息修改了我的代码。这是我的代码目前的样子:

<?php

    $temp = $wp_query;

    $wp_query = null;

    $wp_query = new WP_Query();

    $wp_query->query('showposts=3&post_type=medals'.'&paged='.$paged);

    while ($wp_query->have_posts()) : $wp_query->the_post();

    // loops code here

    endwhile;

    echo '<nav>';

    echo previous_posts_link('&laquo; Newer');

    echo next_posts_link('Older &raquo;');

    echo '</nav>';

    $wp_query = null;

    $wp_query = $temp;  // Reset

?>

我是否需要在functions.php 文件中添加一些代码才能使其正常运行,或者我的原始代码有问题?谢谢。

【问题讨论】:

    标签: php pagination custom-post-type wordpress


    【解决方案1】:

    试试这个:

    <?php
      $type = 'portfolio';
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $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();
      wp_query->query($args);
    ?>
    

    然后将您的永久链接放到默认结构并返回到该结构的状态。

    【讨论】:

    • 当我用您的代码替换我的代码时,网站无法加载。我用您的代码替换以下代码: $temp = $wp_query; $wp_query = null; $wp_query = 新的 WP_Query(); $wp_query->query('showposts=3&post_type=medals'.'&paged='.$paged);
    【解决方案2】:

    试试下面的代码

    $paged = get_query_var('page') ? get_query_var('page') : 1;
    
    $args = array('posts_per_page' => 3,
    'paged'=> $paged,
    'post_type' => 'medals');
    
    $wp_query = new WP_Query($args);
    
    while ($wp_query->have_posts()) : $wp_query->the_post();
    
    // loops code here
    
    endwhile;
    
    global $wp_query;
    
    $big = 999999999;
    
    echo 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,
    'next_text' => __('Next »'),
    ));
    

    【讨论】:

    • 很遗憾,这并没有奏效。分页链接不指向子页面。
    • 用下面的行替换第 4 行,这将生成数字分页。 $wp_query->query('posts_per_page=3&post_type=medals&paged='.$paged);
    • 我已经替换了代码,但是如果你现在查看网站,你会发现所有的分页链接都指向主页。我相信我们已经接近了,但似乎有一些小故障。
    • 好的,移动全局 $wp_query;就在结束之后。
    【解决方案3】:
     <?php while ( $query->have_posts() ) : $query->the_post(); ?>              
     <article class="">
               <div class="">
                <h4><?php the_time( 'm' ); ?></h4>
                <h4><?php the_time( 'd' ); ?></h4>
            </div>
            <div class="">
                <h5><?php the_title(); ?></h5>
                <p><?php the_excerpt(); ?></p>
            </div>
          </div>
        </a>
    </article>               
    
     <?php endwhile; // End the loop. Whew. ?>
    
    <?php
      global $wp_query;
    
      $big = 999999999; // need an unlikely integer
    //echo  esc_url( get_pagenum_link());
       echo paginate_links( array(
          'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $query->max_num_pages,
        'type'=>'list',
        'prev_text'    => __('<'),
        'next_text'    => __('>'),
      ) );
    ?>
    

    希望它对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-04
      • 2014-05-15
      • 2011-03-20
      • 2014-05-21
      • 2018-02-23
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多