静态FrontPage分页-自定义循环

时间:2017-04-20 作者:Konstantinos Koletsas

我创建了一个自定义内容模板,将其分配到一个页面并对查询进行编码。

一切似乎都正常运转。我唯一的问题是分页。所以,当我进入第二页时,我得到一个“没有找到帖子”

目前为止我所做的尝试:

我设置了另一个分页网格(第三方插件)作为主页。摆弄插件的分页给了我同样的结果

            <?php
            if ( get_query_var(\'paged\') ) {
                 $paged = get_query_var(\'paged\');
                 } elseif ( get_query_var(\'page\') ) {
                 $paged = get_query_var(\'page\');
                 } else {
                 $paged = 1;
                 }
              $args=array(
                 \'post_type\' => \'gadget\',
                 \'post_status\' => \'publish\',
                 \'posts_per_page\' => 36,
                 \'paged\' => $paged,
                 \'nopaging\' => false
                );

              $fp_query = null;
              $fp_query = new WP_Query($args);

              if( $fp_query->have_posts() ) {

                $i = 0;
                while ($fp_query->have_posts()) : $fp_query->the_post();
                global $post;
                $postidlt = get_the_id($post->ID);

              // modified to work with 3 columns
              // output an open <div>
              if($i % 3 == 0) { ?> 

              <div class="vc_row prd-box-row">

              <?php
              }
              ?>
                <div class="vc_col-md-4 vc_col-sm-6 vc_col-xs-12 prd-box row-height">
                    <div class="prd-box-inner">
                    <div class="prd-box-image-container">
                      <a href="<?php the_permalink(); ?>">
                        <div class="prd-box-image" style ="background-image: url(
                            <?php $acf_header = get_field(\'header_image\');
                                  if ($acf_header) {
                                    the_field(\'header_image\');
                                  }
                                  else {
                                      echo types_render_field(\'headerimage\', array("raw"=>"true", "url"=>"true","id"=>"$postidlt"));
                                  }
                            ?>);">
                          <div class="vc_col-xs-12 prd-box-date"><?php $short_date = get_the_date( \'M j\' ); echo $short_date;?><span class="full-date">, <?php $full_date = get_the_date( \'Y\' ); echo $full_date;?></span></div>
                          <div class="badges-container">
                            <!-- Discount Condition -->
                            <?php $discountbg = types_render_field("discount", array("style" => "FIELD_NAME : $ FIELD_VALUE", "id"=>"$postidlt")); if($discountbg) : ?>
                            <div class="vc_col-xs-2 discounted-badge">
                                <i class="fa fa-percent" aria-hidden="true"></i>
                            </div>
                            <?php endif; ?>
                            <!-- Video Condition -->
                            <div class="vc_col-xs-2 video-badge">
                                <i class="fa fa-play" aria-hidden="true"></i>
                            </div>
                            <!-- Crowdfunding Condition -->
                            <?php if ( has_term(\'crowdfunding\', \'gadget-categories\', $post->ID) ):  ?>
                            <div class="vc_col-xs-2 crowdfunding-badge">
                                <i class="fa fa-money" aria-hidden="true"></i>
                            </div>
                            <?php endif; ?>
                          </div>
                        </div>
                      </a>
                    </div>
                    <div class="prd-separator"></div>
                    <div class="vc_row prd-box-info">
                        <div class="vc_col-xs-12 prd-box-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
                    </div>
                    <div class="prd-separator"></div>
                    <div class="vc_row prd-box-info">
                        <div class="vc_col-md-12 vc_col-sm-12 vc_col-xs-12 ct-pr-wrapper">
                            <div class="prd-box-price">
                                <?php   $initalprice = types_render_field("initial-price", array("style" => "FIELD_NAME : $ FIELD_VALUE", "id"=>"$postidlt"));
                                        $discountedprice = types_render_field("discount", array("style" => "FIELD_NAME : $ FIELD_VALUE", "id"=>"$postidlt"));
                                        $tba = types_render_field("tba-n", array("output" => "raw", "id"=>"$postidlt"));
                                        $currency = types_render_field("currency", array());
                                        if ($initalprice && empty($discountedprice) && empty($tba)) {
                                            echo($currency),($initalprice);
                                        }
                                        elseif ($discountedprice && empty($tba)) {
                                            echo($currency),($discountedprice);
                                        }
                                        elseif ($tba) {
                                            echo("TBA");
                                        }
                                ?>
                            </div>
                          <div class="prd-box-category">
                             <?php
                                $terms = get_the_terms(  $post->ID , \'gadget_categories\' );
                                    foreach ( $terms as $index => $term ) {
                                        if ($index == 0) {                                                                                                      
                                            // The $term is an object, so we don\'t need to specify the $taxonomy.
                                            $term_link = get_term_link( $term );

                                            // If there was an error, continue to the next term.
                                            if ( is_wp_error( $term_link ) ) {
                                                continue;
                                            }

                                            // We successfully got a link. Print it out.
                                            echo \'<a class="first-ct" href="\' . esc_url( $term_link ) . \'">\' . $term->name . \'</a>\';
                                        }
                                    }
                             ?>
                             <?php
                                $terms_rst = get_the_terms(  $post->ID , \'gadget_categories\' );
                                echo \'<div class="rest-categories">\';
                                    foreach ( $terms_rst as $index => $term ) {
                                        if ($index > 0) {
                                            // The $term is an object, so we don\'t need to specify the $taxonomy.
                                            $term_link = get_term_link( $term );

                                            // If there was an error, continue to the next term.
                                            if ( is_wp_error( $term_link ) ) {
                                                continue;
                                            }

                                            // We successfully got a link. Print it out.
                                            echo \'<a class="rest" href="\' . esc_url( $term_link ) . \'">\' . $term->name . \'</a>\';
                                        }
                                    }
                                echo \'</div>\';
                             ?>
                          </div>
                        </div>
                    </div>
                    <div class="vc_row prd-box-share-wh-row">
                        <div class="prd-separator"></div>
                        <div class="prd-box-share-container">
                          <div class="vc_col-xs-1"></div>
                            <div class="vc_col-xs-4"><?php echo do_shortcode( \'[addtoany]\' );?></div>
                          <div class="vc_col-xs-1"></div>
                        </div>
                        <div class="prd-box-wishlist-container">
                          <div class="vc_col-xs-1"></div>
                            <div class="vc_col-xs-4">
                                <?php $arg = array ( \'echo\' => true ); do_action(\'gd_mylist_btn\',$arg); ?>
                            </div>
                          <div class="vc_col-xs-1"></div>
                        </div>
                    </div>
                    <div class="prd-separator"></div>
                    </div>
                </div>  

                <?php $i++; 
                 // Closing the grid row div   
                if($i != 0 && $i % 3 == 0) { ?>
                    </div><!--/.row-->
                    <div class="clearfix"></div>
                  <?php } ?>
                  <!-- Random Category Snippet Generation -->
                  <?php 
                        if( $i % 12 == 0 ) {
                            $max = 1; //number of categories to display
                            $taxonomy = \'gadget_categories\';
                            $terms = get_terms($taxonomy, \'orderby=name&order= ASC&hide_empty=0\');

                            // Random order
                            shuffle($terms);

                            // Get first $max items
                            $terms = array_slice($terms, 0, $max);

                            // Sort by name
                            usort($terms, function($a, $b){
                            return strcasecmp($a->name, $b->name);
                            });

                            // Echo random terms sorted alphabetically
                            if ($terms) {
                            foreach($terms as $term) {
                                $termID = $term->term_id;
                                echo \'<div class="random-category-snippet" style="background-image: url(\'.types_render_termmeta("category-image", array( "term_id" => $termID, "output" =>"raw") ).\')"><div class="random-snippet-inner"><a href="\' .get_term_link( $term, $taxonomy ) . \'" title="\' .  sprintf( __( "View all gadgets in %s" ), $term->name ) . \'" \' . \'>\' . $term->name.\'</a><p>\' . $term->description . \'</p><br><br><a class="explore-category" href="\'. esc_url( get_term_link( $term ) ) .\'">Explore This Category</a></div></div> \';
                            }
                            }
                       }
                  ?>

                <?php  
                    endwhile;
                }
                    wp_reset_postdata(); ?>
                    <div class="pagination">
                        <div class="previous"><?php previous_posts_link( \'Older Posts\' ); ?></div>
                        <div class="next"><?php next_posts_link( \'Newer Posts\', $fp_query->max_num_pages ); ?></div>
                    </div>
                <!-- pagination -->
        </div><!--/.container-->  
    </div>          
您可以通过访问this link 点击页面底部的“更新帖子”。

编辑

您可以看到,目前页面底部有一个懒散的加载程序。使用此模板,我几乎达到了预期的效果here. 然而,我在frontpage上仍然一无所获(请记住,frontpage和我之前提供的页面使用的是相同的模板。

2 个回复
最合适的回答,由SO网友:BlueSuiter 整理而成

请使用以下代码更新循环部件:

<?php

$page = (get_query_var(\'page\') ? get_query_var(\'page\') : 1);
$args=array(\'post_type\' => \'gadget\', \'post_status\' => \'publish\', \'posts_per_page\' => 36, \'page\' => $page);

$wp_query = new WP_Query($args);
if( $wp_query->have_posts() ) {

    $i = 0;
    while ($wp_query->have_posts()) : $wp_query->the_post();
    $postidlt = $post->ID;
Reason before using page instead of paged.page (int) - 静态首页的页数。显示通常只显示在静态首页的第X页上的帖子。

SO网友:Swen

我不完全确定这是否有效,但我记得以前也遇到过类似的问题。这值得一试,我从自己的(工作)代码中获得了这一点。

Please note that this is my own vague understanding of the issue.

我认为一般的想法是,您的分页是通过WordPress主查询来显示首页或自定义页。这就是为什么它不适用于主查询中的自定义(附加)查询。

为了避免这种情况,我们首先存储$wp_query 对象内部$temp_query 对象然后我们重置主$wp_query, 并将其替换为我们自己的自定义查询。

在我们的帖子中循环之后,我们恢复了$wp_query 来自我们的$temp_query.

global $wp_query

$custom_query_args = array(
    \'post_type\' => \'gadget\',
    \'post_status\' => \'publish\',
    \'posts_per_page\' => 36
);

// Get the pagination
$custom_query_args[\'paged\'] = get_query_var( \'paged\' ) ? get_query_var( \'paged\' ) : 1;  

// Create the WP_Query object
$custom_query = new WP_Query( $custom_query_args );

// Store the existing $wp_query object
$temp_query = $wp_query;

// Clear the $wp_query object
$wp_query = NULL;

// Replace with our custom query object
$wp_query = $custom_query;

// Run our query
if ( $custom_query->have_posts() ) {

    while ( $custom_query->have_posts() ) {

        $custom_query->the_post();

    }

    // Display pagination links before resetting the $wp_query object to the original
    ?>
    <div class="pagination">
        <div class="previous"><?php previous_posts_link( \'Older Posts\' ); ?></div>
        <div class="next"><?php next_posts_link( \'Newer Posts\', $custom_query->max_num_pages ); ?></div>
    </div>
    <?php

}

// Reset postdata
wp_reset_postdata();

// Restore main query object
$wp_query = NULL;
$wp_query = $temp_query;

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请