Wordpress Custom Post-type Pagination 404 w / WP-Pagenavi

时间:2015-08-19 21:52:55

标签: php wordpress pagination wordpress-plugin

我需要为我正在处理的项目提取两种自定义帖子类型。我将它们作为数组传递给post_type。在前面,它们分为两个不同的列表类别,微笑和广告。他们出现了,但分页打破了。我已经设置了%postname%的永久链接,编辑了Page navi的设置,仍然可以阅读404.

网址应该显示为/ smiles / page / * /,他们这样做,但是为404.

这是代码。

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

        $the_query = new WP_Query( array( 
                  'post_type' => array ('smiles','ads'),
                  'order' => 'DESC',
                  'posts_per_page' =>'3',
                  'paged' => $paged ));

        while ( $the_query->have_posts() ) : $the_query->the_post();    
        ?>
            <?php if ($post->post_type == 'smiles') { ?>
            <li class=smile>
                <h3><?php the_author(); ?></h3>
                <a href="<?php the_permalink(); ?>" class=x-fi><img src="<?php echo get_first_image(); ?>" /></a>
                <p><?php the_excerpt(); ?></p>
                <?php echo getPostLikeLink( get_the_ID() ); ?>
                <a href="<?php the_permalink(); ?>">Read More / Comment...</a>
            </li>
            <?php } ?>

            <?php if ($post->post_type == 'ads') { ?>
            <li class=ad>
                <?php the_content(); ?>
            </li>
            <?php } ?>                  
        <?php endwhile; ?>
        </ul>
        <?php wp_pagenavi( array( 'query' => $the_query ) ); wp_reset_query(); ?>

编辑1:

检查了一些帖子。将$ the_query更改为$ wp_query但没有结果。

1 个答案:

答案 0 :(得分:0)

好的,我明白了。

简单的解决方案是删除插件并添加自定义函数来处理分页。之后,我更改了博客的阅读帖子&#39;页面到1和更新的永久链接。然后分页开始工作。

完整解决方案:

的functions.php

function pagination($pages = '', $range = 2) {  
     $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'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</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)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
}

归档smiles.php

<?php   
                $the_query = new WP_Query( array( 
                          'post_type' => array ('smiles','ads'),
                          'order' => 'DESC',
                          'posts_per_page' =>'2',
                          'paged' => $paged
                          ));

                while ( $the_query->have_posts() ) : $the_query->the_post();    
                ?>
                    <?php if ($post->post_type == 'smiles') { ?>
                    <li class=smile>
                        <h3><?php the_author(); ?></h3>
                        <a href="<?php the_permalink(); ?>" class=x-fi><img src="<?php echo get_first_image(); ?>" /></a>
                        <p><?php the_excerpt(); ?></p>
                        <?php echo getPostLikeLink( get_the_ID() ); ?>
                        <a href="<?php the_permalink(); ?>">Read More / Comment...</a>
                    </li>
                    <?php } ?>

                    <?php if ($post->post_type == 'ads') { ?>
                    <li class=ad>
                        <?php the_content(); ?>
                    </li>
                    <?php } ?>                  
                <?php endwhile; ?>
                </ul>
                <?php pagination( $the_query->max_num_pages ); ?>
相关问题