为什么我的WP_QUERY分页只加载主页?

时间:2021-12-07 作者:Lisa

我想做的是:

在自定义php模板文件中使用自定义WP\\U查询设置WP\\U查询分页(它原来不是我的,我只是在修复它)。文件在帖子中循环(类别“Publishing”)并将它们添加到页面中(一次添加5个)。

我的代码应该可以工作,它似乎做得很好,并且遵循WP文档。我想知道是什么bug阻止了它的工作。

我所做的尝试是用paginate\\u链接(数组)添加分页。我确实得到了分页栏,但每次我点击一个链接,它都会跳回主页。但url中添加了一个/页/2/等。

  • 尝试了WP-PageNavi 添加的插件:

  • $custom_query->query(array(\'category_name\' => \'Publishing\', \'page\' => get_query_var(\'paged\')));
    
    并使用短代码:

    wp_pagenavi(array( \'query\' => $custom_query ) );
    
    结果与1)中的结果相同。它总是跳回主页,但会将slug添加到URL。

    我要做的是在页面底部添加一个有效的分页。我想我的模板中循环的工作方式可能存在一些问题?

    My current code with the WP-PageNavi plugin:

    <?php
    
    $args = array(
        
        "category_name"    => "Publishing",
        "orderby"          => "post_date",
        "order"            => "DESC",
        "post_type"        => "post",
        "post_status"      => "publish",
        );
    
    $custom_query = new WP_Query($args); 
    $custom_query->query(array(\'category_name\' => \'Publishing\', \'page\' => get_query_var(\'paged\')));
    
    
    if ( $custom_query->have_posts() ) :
        while ( $custom_query->have_posts() ) : $custom_query->the_post(); 
    
             
            { $id=$post->ID; $image=get_the_post_thumbnail($id, \'full\'); 
                if( class_exists(\'Dynamic_Featured_Image\') ) {
                    global $dynamic_featured_image;
                
                    $featured_images = $dynamic_featured_image->get_featured_images($id );
                    
                }
    
                    echo "<div class=\'direkt\'>".get_the_date( \'d F Y\', $post->ID )."<a href=".get_permalink($post->ID)."?kat=publishing> [Direct Link] </a></div>";
                    ?>
                        <div class="custompost_wrapper">
                            <div class="custompost">
                                <div class="splide">
                                    <div class="splide__track">
                                        <ul class="splide__list">
                                            <li class="splide__slide">
                                                <?php echo $image; ?>
                                            </li>
                                            <?php foreach($featured_images as $featured_image): ?>
                                                <li class="splide__slide">
                                                    <img src="<?php echo $featured_image[\'full\']; ?>"
                                                    data-splide-lazy="<?php echo $featured_image[\'full\']; ?>"
                                                    loading="lazy"
                                                    >
                                                </li>
                                            <?php endforeach; ?>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
    
                <?php
    
                echo "<div class=\'custompost_contentwrapper\'><strong>";
                    the_title();
                echo "</strong><br>";
                echo "<div class=\'thecontent\'>";
                    the_content();
                echo "</div>";
                echo "<div class=\'spacer\'></div></div>";
            } 
                
    
                
        endwhile;  
    
        wp_pagenavi(array( \'query\' => $custom_query ) );
        
        wp_reset_postdata();
    endif;
    
    ?>
    

    The method I tried before the plugin:

    <?php
    
    $published_posts = wp_count_posts()->publish;
    $posts_per_page = get_option(\'posts_per_page\');
    $page_number_max = ceil($published_posts / $posts_per_page);
    
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
    $args = array(
        
        "category_name"    => "Publishing",
        "orderby"          => "post_date",
        "order"            => "DESC",
        "post_type"        => "post",
        "post_status"      => "publish",
        );
    
    $custom_query = new WP_Query($args); 
    
    
    if ( $custom_query->have_posts() ) :
        while ( $custom_query->have_posts() ) : $custom_query->the_post(); 
    
             
            { $id=$post->ID; $image=get_the_post_thumbnail($id, \'full\'); 
                if( class_exists(\'Dynamic_Featured_Image\') ) {
                    global $dynamic_featured_image;
                
                    $featured_images = $dynamic_featured_image->get_featured_images($id );
                    
                }
    
                    echo "<div class=\'direkt\'>".get_the_date( \'d F Y\', $post->ID )."<a href=".get_permalink($post->ID)."?kat=publishing> [Direct Link] </a></div>";
                    ?>
                        <div class="custompost_wrapper">
                            <div class="custompost">
                                <div class="splide">
                                    <div class="splide__track">
                                        <ul class="splide__list">
                                            <li class="splide__slide">
                                                <?php echo $image; ?>
                                            </li>
                                            <?php foreach($featured_images as $featured_image): ?>
                                                <li class="splide__slide">
                                                    <img src="<?php echo $featured_image[\'full\']; ?>"
                                                    data-splide-lazy="<?php echo $featured_image[\'full\']; ?>"
                                                    loading="lazy"
                                                    >
                                                </li>
                                            <?php endforeach; ?>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
    
                <?php
    
                echo "<div class=\'custompost_contentwrapper\'><strong>";
                    the_title();
                echo "</strong><br>";
                echo "<div class=\'thecontent\'>";
                    the_content();
                echo "</div>";
                echo "<div class=\'spacer\'></div></div>";
            }   
    
                
        endwhile;  
        
        $total_pages = $page_number_max;
    
        if ($total_pages > 1){
            $current_page = max(1, get_query_var(\'paged\'));
                    
            echo paginate_links(array(
                \'base\' => get_pagenum_link(1) . \'%_%\',
                \'format\' => \'page/%#%\',
                \'current\' => $current_page,
                \'total\' => $total_pages,
                \'prev_text\'    => __(\'« prev\'),
                \'next_text\'    => __(\'next »\'),
            ));
        };
        
        wp_reset_postdata();
    endif;
    
    ?>
    

    Any help would be appreciated. I\'m trying to get the pagination to work for hours now.

    1 个回复
    SO网友:Aboelabbas

    您可以在WordPress内部创建的页面上对自定义循环分页,代码的问题是您正在使用;第“”页;参数,而不是;第paged页;参数前者用于为单个帖子部分分页,而后者用于为帖子存档或多个帖子分页。

    因此,页面模板上的分页查询应该如下所示:

    $args = array(
    
        "category_name"    => "Publishing",
        "orderby"          => "post_date",
        "order"            => "DESC",
        "post_type"        => "post",
        "post_status"      => "publish",
        \'paged\' => get_query_var(\'paged\'), // This is the correct parameter for pagination
    );
    
    $custom_query = new WP_Query($args);
    
    if ( $custom_query->have_posts() ) :
        while ( $custom_query->have_posts() ) : $custom_query->the_post();
    
            echo \'<h1>\' . get_the_title() . \'</h1>\';
            // The remaining of your code here
        endwhile;
    
        wp_pagenavi(array( \'query\' => $custom_query ) );
    
        wp_reset_postdata();
    endif;
    
    此外,我在您的代码中看到您正在使用$“发布”;未定义的变量。如果您试图访问自定义循环中的当前帖子,最好使用模板标记--帮助器方法--例如get\\u the\\u ID()、get\\u the\\u title()、,。。。默认情况下,这些方法可以访问全局$post对象,而无需将其作为参数传递,因为您已经使用$custom_query->the_post();

    相关推荐

    Add pagination to WP_Query

    我试图在下面的代码中添加分页,但不知道如何与之集成。请帮忙在帖子上找到页码。<?php $child_query = new WP_Query(array(\'post_type\' => \'blogpost\', \'orderby\' => \'date\', \'order\' => \'DESC\')); while ( $child_query->ha