无法使用WP_query进行分页

时间:2017-12-29 15:38:03

标签: php wordpress

尝试制作我的第一个wordpress主题,并且我无法在页面上使用自定义帖子类型进行分页。

我会显示分页链接,但是当我点击分页链接时,我会找到" Page not found"。

我的代码......

<ul id="og-grid" class="og-grid">
                <?php
                  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                    $query = new WP_Query(array(
                        'post_type' => 'projects',
                        'post_status' => 'publish',
                        'posts_per_page' => 9,
                        'paged' => $paged
                    ));

                    while ($query->have_posts()) {
                        $query->the_post();
                        $post_id = get_the_ID();
                ?>

                <li>
                    <a href="<?php the_permalink();?>" data-largesrc="<?php echo the_post_thumbnail_url(); ?>" data-title="<?php the_title();  ?>" data-owner="<?php the_field('project-owner') ?>" data-date="<?php the_field('project-date') ?>" data-location="<?php the_field('project-location') ?>" data-contractor="<?php the_field('project-contractor') ?>" data-value="<?php the_field('project-value') ?>" data-description="<?php echo esc_html(get_the_excerpt()); ?>">
                        <div class="project-roll-item" style="background: url('<?php the_post_thumbnail_url(); ?>') no-repeat center center; background-size: cover;">
                            <h4 class="project-info"><?php the_title(); ?></h4>
                        </div>
                    </a>
                </li>

                    <?php
                    } ?>

                </ul>

                <div class="pagination" style="margin-top: 50px;">
                        <?php
                                echo 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'     => false,
                                        'type'         => 'plain',
                                        'end_size'     => 2,
                                        'mid_size'     => 1,
                                        'prev_next'    => true,
                                        'prev_text'    => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
                                        'next_text'    => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
                                        'add_args'     => false,
                                        'add_fragment' => '',
                                ) );
                        ?>
                </div>

                <?php wp_reset_query(); ?>

有任何帮助吗?感谢。

2 个答案:

答案 0 :(得分:0)

对于分页,在不使用任何插件的wordpress中是非常简单的步骤。 我修改了你的代码。 您可以按照以下简单步骤进行分页,

步骤1.将以下功能添加到主题functions.php文件中:

<?php 
function pagination($pages = '', $range = 4)
{ 
     $showitems = ($range * 2)+1; 
     global $paged;
     if(empty($paged)) $paged = 1;
     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }  
     if(1 != $pages)
     {
         echo "<div class=\"pagination\"><span>Page ".$paged." of ".$pages."</span>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
             }
         }
         if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>"; 
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
         echo "</div>\n";
     }
}
?>

<强>第二步。要设置样式,请将以下内容添加到样式表中(通常为style.css)。

.pagination {
clear:both;
padding:20px 0;
position:relative;
font-size:11px;
line-height:13px;
}
.pagination span, .pagination a {
display:block;
float:left;
margin: 2px 2px 2px 0;
padding:6px 9px 5px 9px;
text-decoration:none;
width:auto;
color:#fff;
background: #555;
}
.pagination a:hover{
color:#fff;
background: #3279BB;
}
.pagination .current{
padding:6px 9px 5px 9px;
background: #3279BB;
color:#fff;
}

第3步:最后一步,输入您的模板文件:

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

        $query = new WP_Query(array('post_type' => 'projects',
                                    'post_status' => 'publish',
                                    'posts_per_page' => 9,
                                    'paged' => $paged
                                    'orderby' => 'date'                                     
                                    ));
 ?>
<ul id="og-grid" class="og-grid">
  <?php while ( $query->have_posts() ) : $query->the_post(); ?>

  <li>
                    <a href="<?php the_permalink();?>" data-largesrc="<?php echo the_post_thumbnail_url(); ?>" data-title="<?php the_title();  ?>" data-owner="<?php the_field('project-owner') ?>" data-date="<?php the_field('project-date') ?>" data-location="<?php the_field('project-location') ?>" data-contractor="<?php the_field('project-contractor') ?>" data-value="<?php the_field('project-value') ?>" data-description="<?php echo esc_html(get_the_excerpt()); ?>">
                        <div class="project-roll-item" style="background: url('<?php the_post_thumbnail_url(); ?>') no-repeat center center; background-size: cover;">
                            <h4 class="project-info"><?php the_title(); ?></h4>
                        </div>
                    </a>
                </li>



  <?php endwhile; ?>
 </ul>

<?php if (function_exists("pagination")) {
    pagination($query->max_num_pages);
} ?>

希望这会有所帮助。如果有任何问题,请告诉我。

答案 1 :(得分:0)

请将此用于分页。

<ul id="og-grid" class="og-grid">
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
  'post_type' => 'projects',
  'post_status' => 'publish',
  'posts_per_page' => 9,
  'paged' => $paged
);
$query = new WP_Query($args);

while ($query->have_posts()) {
   $query->the_post();
   $post_id = get_the_ID();
?>

<li>
   <a href="<?php the_permalink();?>" data-largesrc="<?php echo the_post_thumbnail_url(); ?>" data-title="<?php the_title();  ?>" data-owner="<?php the_field('project-owner') ?>" data-date="<?php the_field('project-date') ?>" data-location="<?php the_field('project-location') ?>" data-contractor="<?php the_field('project-contractor') ?>" data-value="<?php the_field('project-value') ?>" data-description="<?php echo esc_html(get_the_excerpt()); ?>">
     <div class="project-roll-item" style="background: url('<?php the_post_thumbnail_url(); ?>') no-repeat center center; background-size: cover;">
       <h4 class="project-info"><?php the_title(); ?></h4>
     </div>
   </a>
</li>

<?php
 } ?>

</ul>
<?php
  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,
    'before_page_number' => '<span class="screen-reader-text">'.$translated.' 
     </span>'
   ));
   wp_reset_query(); ?>