分页无效并重定向到主页

时间:2018-02-17 15:38:45

标签: php wordpress wordpress-theming

我有一个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篇帖子。

我如何在我的短代码中添加分页编号,这是有效的。 为什么分页不起作用? 对不起,我的英语不好 谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码 -

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