【问题标题】:Load more button does not work for category page加载更多按钮不适用于类别页面
【发布时间】:2018-04-17 23:56:03
【问题描述】:

我正在为所有“食物”类别的帖子创建一个存档页面。所以我创建了一个名为 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();
?>

【问题讨论】:

    标签: php html ajax wordpress categories


    【解决方案1】:

    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' =&gt; 3,但全局$wp_query 使用默认值10。因此,如果在食品类别中的帖子少于10 个,则max_num_pages 等于1,并且加载更多按钮将被隐藏。

    我认为页面上的 page-food.php $wp_query-&gt;max_num_pages 等于 0 - 所以按钮是可见的。

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

    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,
     ));
    ...
    

    【讨论】:

    • 您介意将它放入我的代码中吗?我在 PHP 方面仍然不是最好的,当我尝试添加它时,我想我把它放错了位置,因为我得到了一个错误。
    • 页面上唯一的东西是我的标题和我的轮播(它被搞砸了)我粘贴了我在上面的问题中输入的代码。但我很确定我只是在错误的位置添加了您建议的代码。
    • 您好像用新代码替换了$the_query = new WP_Query 代码?改回来。您只需添加新的,无需修改旧的
    • 我再次更新了代码。现在会出现加载更多按钮,但是当您按下它时,它会转到此网址 - localhost:8888/wordpress/category/food/?paged=2 - 并滚动回顶部并加载索引页面?
    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2018-07-06
    • 2022-12-01
    • 1970-01-01
    • 2011-04-26
    相关资源
    最近更新 更多