分页不适用于自定义主页模板内的自定义查询

时间:2014-12-15 作者:Naresh Devineni

我知道这是一个重复的问题。但对于现有的问题,我的观点还不够。

所以我们开始。。。。

分页不适用于自定义主页模板内的自定义查询。

下面的解决方案(适用于许多其他问题)取自另一个问题。

How to fix pagination for custom loops?


<div class="most-popular type-1">
   <h3 class="section-headline"><?php _e(\'Most Popular\',\'themename\'); ?></h3>
   <div class="widget-articles clearfix">
      <?php 
         $mostpopular_args=array(
             \'post_type\'        => \'post\',
             \'orderby\'          => \'meta_value_num\',
             \'meta_key\'         => \'view_count\',
             \'posts_per_page\'   => \'10\',
         );
         $mostpopular_args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
         ?>
      <?php 
         $mostpopular_pick = new WP_Query($mostpopular_args); 
         // Pagination fix
         $temp_query = $wp_query;
         $wp_query   = NULL;
         $wp_query   = $mostpopular_pick;
         ?>
      <?php if($mostpopular_pick->have_posts() ): ?>
      <?php while($mostpopular_pick->have_posts()): $mostpopular_pick->the_post(); ?>
      <article class="widget-article">
         <a href ="<?php the_permalink(); ?>">
            <div class="widget-article-thumb">
               <?php if(has_post_thumbnail()){ ?>
               <?php $page_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), \'featuredsmall\'); ?>
               <img src="<?php echo $page_thumb[0]; ?>" alt="" />   
               <?php } ?>
            </div>
         </a>
         <div class="widget-content-wrapper">
            <h3 class="widget-article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
         </div>
         <div class="only-count">
            <div class="icon icon-share-alt" aria-hidden="true"></div>
            <?php echo do_shortcode(\'[mashshare shares="true" buttons="false"]\'); ?>
            <div class="share-text">SHARES</div>
         </div>
      </article>
      <?php endwhile; ?>
      <?php endif; ?>
      <?php 
         wp_reset_postdata(); 
         // Custom query loop pagination
         previous_posts_link( \'Older Posts\' );
         next_posts_link( \'Newer Posts\', $mostpopular_pick->max_num_pages );
         // Reset main query object
         $wp_query = NULL;
         $wp_query = $temp_query;
      ?>
   </div>
</div>

然而,这对我不起作用,我不知道为什么。是否有任何外部因素?

这是我的总自定义主页模板。


<?php
/*
Template Name: Home Page
*/
?>
<?php get_header(); ?>
    <div class="main-content col-xs-12" role="main">
        <div class="row">
            <?php get_sidebar(\'abovethefold\'); ?>
        </div>
        <div class="row">
            <?php get_sidebar(\'homecolumnone\'); ?>
            <div class="homecolumntwo-sidebar col-sm-4 col-md-5">
                <div class="most-popular type-1">
                    <h3 class="section-headline"><?php _e(\'Most Popular\',\'themename\'); ?></h3>
                    <div class="widget-articles clearfix">
                            <?php 
                                $mostpopular_args=array(
                                    \'post_type\'         => \'post\',
                                    \'orderby\'           => \'meta_value_num\',
                                    \'meta_key\'          => \'view_count\',
                                    \'posts_per_page\'    => \'10\',
                                );
                                $mostpopular_args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;
                            ?>
                            <?php 
                                $mostpopular_pick = new WP_Query($mostpopular_args); 
                                // Pagination fix
                                $temp_query = $wp_query;
                                $wp_query   = NULL;
                                $wp_query   = $mostpopular_pick;
                            ?>

                            <?php if($mostpopular_pick->have_posts() ): ?>
                                <?php while($mostpopular_pick->have_posts()): $mostpopular_pick->the_post(); ?>
                                    <article class="widget-article">
                                        <a href ="<?php the_permalink(); ?>">
                                            <div class="widget-article-thumb">
                                                <?php if(has_post_thumbnail()){ ?>
                                            <?php $page_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), \'featuredsmall\'); ?>
                                                    <img src="<?php echo $page_thumb[0]; ?>" alt="" />  
                                                <?php } ?>
                                            </div>
                                        </a>
                                            <div class="widget-content-wrapper">
                                                <h3 class="widget-article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                            </div>
                                            <div class="only-count">
                                                <div class="icon icon-share-alt" aria-hidden="true"></div>
                                                <?php echo do_shortcode(\'[mashshare shares="true" buttons="false"]\'); ?>
                                                <div class="share-text">SHARES</div>
                                            </div>
                                    </article>
                                <?php endwhile; ?>
                            <?php endif; ?>
                            <?php 
                                wp_reset_postdata(); 
                                // Custom query loop pagination
                                previous_posts_link( \'Older Posts\' );
                                next_posts_link( \'Newer Posts\', $mostpopular_pick->max_num_pages );
                                // Reset main query object
                                $wp_query = NULL;
                                $wp_query = $temp_query;
                            ?>
                    </div>
                </div>
            </div> <!-- end sidebar -->
            <?php //get_sidebar(\'homecolumntwo\'); ?>
            <?php get_sidebar(\'homecolumnthree\'); ?>
        </div>
    </div> <!-- end main-content -->
<?php get_footer(); ?>

任何帮助都将不胜感激:)谢谢<;3.

1 个回复
SO网友:jay.jivani

用于分页

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

    $mostpopular_args=array(
             \'post_type\' => \'post\',
             \'orderby\' => \'meta_value_num\',
             \'meta_key\' => \'view_count\',
             \'paged\' => $paged,
             \'posts_per_page\' => \'10\',
         );
    $wp_query = new WP_Query($mostpopular_args);

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

    global $wp_query;

    $big = 999999999; // need an unlikely integer

    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
    ) );
?>

结束

相关推荐

将jQuery日期选择器添加到自定义发布类型Metabox

我已经创建了一个自定义的post类型元数据库,它可以很好地显示和运行。问题在于日期选择器似乎不起作用,当单击元框时,您可以输入它,而不是出现日期选择器。另一方面,我的脚本似乎都无法工作,尽管它们都已加载。编写加载所有脚本的代码。//Making jQuery Google API function modify_jquery() { if (!is_admin()) { // comment out the next two lines to load th