WordPress静态主页分页不起作用

时间:2021-04-17 作者:Khandaker Ikrama

我试图在主页上添加分页,但它不起作用。有人能帮我解决这个问题吗?

    <ul class="xl-products grid- masonry">
        <?php

        if(is_front_page()) {
            $paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
        }else {
            $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        }

        $args = array(
            \'post_type\' => \'product\',
            \'posts_per_page\' => 10,
            \'paged\'          => $paged,
        );

        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <li class="xl-product-item item- brick">
                <div class="product-box content">

                    <?php woocommerce_template_loop_product_thumbnail(); ?>
                    <div class="info">
                        <h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                        <div class="category">

                            <?php echo wc_get_product_category_list( $loop->post->ID, \', \', \'\' ); ?>

                        </div>
                        <div class="price-wrapper">
                            <?php woocommerce_template_loop_price(); ?>
                        </div>

                        <div class="cart-btn">
                            <?php woocommerce_template_loop_add_to_cart(); ?>

                        </div>
                        <div class="user">
                            <?php echo get_avatar(get_the_author_meta( \'ID\' ), 50); ?>
                            <span class="name"><?php the_author(); ?></span>
                        </div>
                    </div>

                </div>
            </li>

        <?php endwhile; ?>

        <?php else : echo __( \'No products found\' ); ?>
        <?php endif; ?>

    </ul><!–/.products–>

    <div style="text-align:center;">
        <?php //echo 4;?>

        <div class="nav-previous alignleft"><?php next_posts_link( \'Older posts\' ); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( \'Newer posts\' ); ?></div>

    </div>


    <?php wp_reset_postdata(); ?>

1 个回复
SO网友:Elex

您正在使用全局$wp_query 默认情况下,而不是在自定义查询上($loop). 这可能就是它不能正常工作的原因。

<ul class="xl-products grid- masonry">
    <?php
    global $wp_query;

    if(is_front_page()) {
        $paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
    }else {
        $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    }

    $args = array(
        \'post_type\'      => \'product\',
        \'posts_per_page\' => 10,
        \'paged\'          => $paged,
    );
    
    $loop = new WP_Query( $args );
    
    // Reset main wp_query 
    $wp_query = null; 
    $wp_query = $loop;

    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

        <li class="xl-product-item item- brick">
            <div class="product-box content">

                <?php woocommerce_template_loop_product_thumbnail(); ?>
                <div class="info">
                    <h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <div class="category">

                        <?php echo wc_get_product_category_list( $loop->post->ID, \', \', \'\' ); ?>

                    </div>
                    <div class="price-wrapper">
                        <?php woocommerce_template_loop_price(); ?>
                    </div>

                    <div class="cart-btn">
                        <?php woocommerce_template_loop_add_to_cart(); ?>

                    </div>
                    <div class="user">
                        <?php echo get_avatar(get_the_author_meta( \'ID\' ), 50); ?>
                        <span class="name"><?php the_author(); ?></span>
                    </div>
                </div>

            </div>
        </li>

    <?php endwhile; ?>

    <?php else : echo __( \'No products found\' ); ?>
    <?php endif; ?>

</ul><!–/.products–>

<div style="text-align:center;">
    <?php // You $wp_query is still from $loop there ?>
    <div class="nav-previous alignleft"><?php next_posts_link( \'Older posts\' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( \'Newer posts\' ); ?></div>

</div>

<?php // You can reset from here
$wp_query = null; 
wp_reset_query(); // Reset $wp_query to the real $wp_query 
您不需要使用wp_reset_postdata 如果您使用wp_reset_query (See the doc of wp_reset_query) 它已经包含在函数中。

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?