【问题标题】:pagination not working on wordpress custom post type分页不适用于wordpress自定义帖子类型
【发布时间】:2014-10-24 05:32:39
【问题描述】:

这是我在wordpress中列出自定义帖子类型帖子的代码,但它不起作用分页我在这里犯的错误有什么可以帮助的吗??

<div id="talent-main">
   <?php      
   global $wp_query; 
   $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
   $loop = new WP_Query( array('post_type' => 'talent','posts_per_page' => 2,'paged'=>$paged)); 
   ?>

    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <div id="post-<?php the_ID(); ?>" <?php post_class('multiple') ?>>
              <div class="post-image-talent">
                  <a class="post-frame <?php the_ID(); ?>" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"></a>
                  <?php the_post_thumbnail('video-talent-thumb'); ?>
              </div>
              <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_short_title('', '...', true, '22') ?></a></h2>
          </div> 
    <?php endwhile;?>
    <div class="navigation">   
        <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>   
        <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>   
    </div>  
</div> <!-- main -->

【问题讨论】:

  • 您是否收到 404 错误?通常,分页的 404 错误是由于您的帖子类型共享相同的帖子或页面名称。因此,如果您有一个页面/日历和一个名为“日历”的帖子类型,它会在尝试分页时抛出 404,因为 WordPress 无法确定要显示的内容。
  • 不显示前 2 个帖子没有分页工作@lan
  • 你有多少帖子?我假设超过2个?如果您只有两个,那么它会显示的全部内容...此外,此页面上是否正在执行其他查询?如果是这样,您可能需要在 endwhile 之后添加 wp_reset_query;让事情再次运作codex.wordpress.org/Function_Reference/wp_reset_query

标签: wordpress pagination custom-post-type


【解决方案1】:

你能检查下面的代码吗?

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

$loop = new WP_Query(array(
    'post_type'=>'talent',
    'posts_per_page' => 2,
    'paged' => $paged,
)); ?>

<?php if($loop->have_posts()) : ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <div id="post-<?php the_ID(); ?>" <?php post_class('multiple') ?>>
              <div class="post-image-talent">
                  <a class="post-frame <?php the_ID(); ?>" href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"></a>
                  <?php the_post_thumbnail('video-talent-thumb'); ?>
              </div>
              <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_short_title('', '...', true, '22') ?></a></h2>
          </div> 
    <?php endwhile;?>          
    <?php 
    $total_pages = $loop->max_num_pages;

    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    ?>    
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', 'No post found'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2013-07-13
    • 2014-05-15
    • 2011-03-20
    相关资源
    最近更新 更多