加载更多按钮不适用于类别页面

时间:2017-01-15 04:08:18

标签: php html ajax wordpress categories

我正在为所有类别" food"的帖子制作一个存档页面。所以我创建了一个名为category-food.php的php文件并添加了我的代码,但代码并没有正常工作。在将代码粘贴到另一个php文件page-food.php之前,我无法弄清楚为什么。该代码最终致力于页面食品。有没有人有理由为什么会这样,我怎么能让它在category-food.php上工作?

*主要问题是在category-food.php上我的ajax加载更多按钮根本不显示。但是在page-food.php上,ajax加载更多按钮显示并且工作正常。这是我正在使用的插件 - https://wordpress.org/plugins/easy-load-more/

代码......

<?php
get_header();
get_template_part ('inc/carousel-food');

$the_query = new WP_Query( array(
    'posts_per_page' => 3,
    'paged' => get_query_var('paged', 1),
    'cat' => 10,
 ));

if ( $the_query->have_posts() ) {

    // display #ajax wrapper only if we have posts
    echo '<div id="ajax"><div class="row">';
    $i = 0;
    while($the_query->have_posts()) {
          $the_query->the_post(); ?>

                <?php
                   if($i % 3 == 0) {
                     echo '</div><div class="row">';
                   }
                ?>

                <div class="col-md-4">
                    <article <?php post_class(); ?> >
                        <?php the_post_thumbnail('medium-thumbnail'); ?>
                            <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                            <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                            <?php get_template_part( 'share-buttons' ); ?>
                            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                            <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
                    </article>
                </div>

    <?php $i++; }//end while

    echo '</div></div>'; // close the #ajax wrapper after the post list

    if(get_query_var('paged') < $the_query->max_num_pages) {
        load_more_button();
    }     

} else { // if there are no posts     
    echo '<p>Sorry, no posts matched your criteria.</p>';     
}//end if

get_footer();
?>

更新代码

<?php
get_header();
get_template_part ('inc/carousel-food');

global $wp_query;
$wp_query->set('posts_per_page', 3);
$wp_query->query($wp_query->query_vars); 


$the_query = new WP_Query( array(
    'posts_per_page' => 3,
    'paged' => get_query_var('paged', 1),
    'cat' => 10,
 ));

if ( $the_query->have_posts() ) {

    // display #ajax wrapper only if we have posts
    echo '<div id="ajax"><div class="row">';
    $i = 0;
    while($the_query->have_posts()) {
          $the_query->the_post(); ?>

                <?php
                   if($i % 3 == 0) {
                     echo '</div><div class="row">';
                   }
                ?>

                <div class="col-md-4">
                    <article <?php post_class(); ?> >
                        <?php the_post_thumbnail('medium-thumbnail'); ?>
                            <h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                            <p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
                            <?php get_template_part( 'share-buttons' ); ?>
                            <a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
                            <?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
                    </article>
                </div>

    <?php $i++; }//end while

    echo '</div></div>'; // close the #ajax wrapper after the post list

    if(get_query_var('paged') < $the_query->max_num_pages) {
        load_more_button();
    }     

} else { // if there are no posts     
    echo '<p>Sorry, no posts matched your criteria.</p>';     
}//end if

get_footer();
?>

1 个答案:

答案 0 :(得分:0)

Easy load more使用global $wp_query对象来检测是否应该显示load-more按钮:

//class-easy-load-more.php: 552 
global $wp_query;
...
$wp_query->max_num_pages == 1 ? ' ajax-inactive' : ''

您设置'posts_per_page' => 3但全局$wp_query使用默认值10.因此,如果在食品类别中有少于10个帖子,则max_num_pages等于1并加载 - 更多按钮将被隐藏。

我认为页面page-food.php $wp_query->max_num_pages等于0 - 所以按钮可见。

您可以在Settings-&gt;阅读页面上更改默认值,但它会影响所有目录/存档页面。 相反,您可以在$wp_query页面上更改全局category-food.php

get_header();
get_template_part ('inc/carousel-food');

global $wp_query;
$wp_query->set('posts_per_page', 3);
$wp_query->query($wp_query->query_vars); 

$the_query = new WP_Query( array(
    'posts_per_page' => 3,
    'paged' => get_query_var('paged', 1),
    'cat' => 10,
 ));
...