我有一个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
你可以尝试以下代码-
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
评论前必须登录!
注册