【问题标题】:pagination is not working and redirect to homepage分页不起作用并重定向到主页
【发布时间】:2018-02-18 17:21:58
【问题描述】:

我有一个 wordpress 博客,页面选择首页(主页), 当我选择页面作为主页(从外观 -> 阅读)时,分页不起作用并重定向到主页。 这意味着当输入这个地址时:example.com/page/2 它被重定向到example.com

我正在使用在 function.php 中调用的短代码显示我最近的帖子 这是我的简码:

function last_posts() {
$my_query = new WP_Query( 'cat=-1&posts_per_page=5' );

while ( $my_query->have_posts() ) : $my_query->the_post();

echo '<div style="margin: 20px 5px;" class="row post-inner-content">';
echo '<div class="col-md-8 col-sm-8 col-xs-6" style="padding-right: 15px;">';
echo '<a class="recent-posts-title" href="'.get_the_permalink().'">'.get_the_title().'</a>';
echo '<p class="recent-posts-date">'.get_the_date().'</p>';
echo '<p class="hidden-xs recent-posts-excerpt">';
echo kc_excerpt();
echo '</p>';
echo '<p class="recent-posts-cat">';

$cat = get_the_category($recent["ID"]); //get the category array
if ($cat[0]->name != "Featured") { //check if the category is "featured"
$catname = $cat[2]->name; //set $catname variable to "featured"
} else {
$catname = $cat[0]->name; //or set $catname variable to "other category name"
}
echo $catname; //output $catname variable


echo '</p>';
echo '</div>';
echo '<div class="col-md-4 col-sm-4 col-xs-6"><a href="'.get_the_permalink().'">'.get_the_post_thumbnail( $post_id, array( 350, 220) ).'</a></div>';
echo '</div>';

endwhile;
}
add_shortcode( 'lastposts', 'last_posts' );

我在主页(首页)中使用 [lastposts] 短代码来显示 5 个最近的帖子。

我如何在我的简码中添加分页号并且它的工作。 为什么分页不起作用? 对不起,我的英语不好 谢谢

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    你可以试试这个代码 -

    function last_posts() {
    
    $args = array(
        'post_type' => 'post',
        'posts_per_page'  => -1
    );
    
    $my_query = new WP_Query( $args );
    
    while ( $my_query->have_posts() ) : $my_query->the_post();
    
        echo '<div style="margin: 20px 5px;" class="row post-inner-content">';
        echo '<div class="col-md-8 col-sm-8 col-xs-6" style="padding-right: 
        15px;">';
        echo '<a class="recent-posts-title" 
        href="'.get_the_permalink().'">'.get_the_title().'</a>';
        echo '<p class="recent-posts-date">'.get_the_date().'</p>';
        echo '<p class="hidden-xs recent-posts-excerpt">';
        echo kc_excerpt();
        echo '</p>';
        echo '<p class="recent-posts-cat">';
    
        $cat = get_the_category($recent["ID"]); //get the category array
    
        if ($cat[0]->name != "Featured") { //check if the category is "featured"
            $catname = $cat[2]->name; //set $catname variable to "featured"
        } else {
            $catname = $cat[0]->name; //or set $catname variable to "other 
        category 
        name"
        }
        echo $catname; //output $catname variable
    
        echo '</p>';
        echo '</div>';
        echo '<div class="col-md-4 col-sm-4 col-xs-6"><a 
        href="'.get_the_permalink().'">'.get_the_post_thumbnail( $post_id, 
        array( 
        350, 220) ).'</a></div>';
        echo '</div>';
    
    endwhile;
    
        // Pagination code           
        global $my_query;
    
        echo paginate_links( array(
         'base'         => str_replace( 999999999, '%#%', esc_url( 
          get_pagenum_link( 999999999 ) ) ),
         'total'        => $my_query->max_num_pages,
         'current'      => max( 1, get_query_var( 'paged' ) ),
         'format'       => '?paged=%#%',
         'show_all'     => false,
         'type'         => 'plain',
         'end_size'     => 2,
         'mid_size'     => 1,
         'prev_next'    => true,
         'prev_text'    => '<span class="lnr lnr-arrow-left"></span>',
         'next_text'    => '<span class="lnr lnr-arrow-right"></span>',
         'add_args'     => false,
         'add_fragment' => '',
       ) );          
    
    }
    add_shortcode( 'lastposts', 'last_posts' );
    

    更多信息请查看此链接https://codex.wordpress.org/Function_Reference/paginate_links

    【讨论】:

    • 谢谢,但它仍然无法正常工作并重定向到主页,我希望当用户打开 example.com/page/2 打开显示 10 个持续帖子的新标签时。
    猜你喜欢
    • 1970-01-01
    • 2018-08-28
    • 2019-07-06
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2020-07-06
    相关资源
    最近更新 更多