简码中的 WordPress 分页功能始终显示在顶部
WordPress pagination fuction in shortcode always displayed on the top
WordPress 简码分页功能始终显示在顶部。请看下面的代码
/*-------------------------------------------------------------------------*/
/* Custom Pagination */
/*-------------------------------------------------------------------------*/
function suareztheme_pagination($pages = '', $range = 2){
$showitems = ( $range * 2 ) + 1;
global $paged;
if(empty($paged))
$paged = 1;
if($pages == ''){
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages){
$pages = 1;
}
}
if( 1 != $pages ){
$pagination_html .= '<div class="pagination">';
if( $paged > 2 && $paged > $range + 1 && $showitems < $pages ){
$pagination_html .= '<a href="' . get_pagenum_link( 1 ) . '">«</a>';
}
if( $paged > 1 && $showitems < $pages ){
$pagination_html .= '<a href="' . get_pagenum_link( $paged - 1 ) . '">‹</a>';
}
for ( $i = 1; $i <= $pages; $i++ ){
if ( 1 != $pages && ( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
if ( $paged == $i ){
$pagination_html .= '<span class="current">' . $i . '</span>';
} else{
$pagination_html .= '<a href="' . get_pagenum_link( $i ). '" class="inactive">' . $i . '</a>';
}
}
}
if ( $paged < $pages && $showitems < $pages ){
$pagination_html .= '<a href="' . get_pagenum_link( $paged + 1 ) . '">›</a>';
}
if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages ){
$pagination_html .= '<a href="' . get_pagenum_link( $pages ) . '">»</a>';
}
$pagination_html .= '</div>';
return $pagination_html;
}
}
然后我在shortcode函数中调用了它
/*-------------------------------------------------------------------------*/
/* Grid Shortcode */
/*-------------------------------------------------------------------------*/
function RecentBlog($atts, $content = null) {
extract(shortcode_atts(array(
"comments" => 'true',
"date" => 'true',
"columns" => '4',
"limit" => '-1',
"title" => 'true',
"description" => 'true',
"cat_slug" => '',
"post_type" => '',
"excerpt_length" => '15',
"readmore_text" => '',
"pagination" => 'false'
), $atts));
global $post;
$postformat = get_post_format();
....
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
......
if (have_posts()) : while (have_posts()) : the_post();
$postformat = get_post_format();
if( $postformat == "" ) $postformat="standard";
$protected = "";
$portfoliogrid .= '<div class="' . $column_no . ' post grid-post post-' . $postformat . '">';
.....////
$portfoliogrid .= '<div class="summary-info">';
.......
$portfoliogrid .='</div>';
// If either of title and description needs to be displayed.
if ( $title == "true" || $description == "true" ) {
$portfoliogrid .='<div class="work-details">';
......
$portfoliogrid .='</div>';
}
$portfoliogrid .='</div>';
endwhile; endif;
if ( $pagination == "true" ){
if ( isset( $additional_loop ) ){
echo suareztheme_pagination( $additional_loop->max_num_pages );
} else {
echo suareztheme_pagination();
}
if ( function_exists("suareztheme_pagination") ) {
} else {
next_posts_link('«« Older Posts');
previous_posts_link('Newer Posts »»');
}
}
wp_reset_query();
return $portfoliogrid;
}
add_shortcode( "recentblog", "RecentBlog" );
上面的代码没有问题,但是有一个问题,当它在特定页面显示时,不管它在什么地方,它总是显示在最上面。非常感谢您对我的支持,在此先感谢。
在短代码回调中,您需要连接 return 和 html,例如:
$portfoliogrid .= suareztheme_pagination();
请注意,您必须使用 get_next_posts_link()
和 get_previous_posts_link()
而不是 next_posts_link()
和 previous_posts_link()
。
WordPress 简码分页功能始终显示在顶部。请看下面的代码
/*-------------------------------------------------------------------------*/
/* Custom Pagination */
/*-------------------------------------------------------------------------*/
function suareztheme_pagination($pages = '', $range = 2){
$showitems = ( $range * 2 ) + 1;
global $paged;
if(empty($paged))
$paged = 1;
if($pages == ''){
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages){
$pages = 1;
}
}
if( 1 != $pages ){
$pagination_html .= '<div class="pagination">';
if( $paged > 2 && $paged > $range + 1 && $showitems < $pages ){
$pagination_html .= '<a href="' . get_pagenum_link( 1 ) . '">«</a>';
}
if( $paged > 1 && $showitems < $pages ){
$pagination_html .= '<a href="' . get_pagenum_link( $paged - 1 ) . '">‹</a>';
}
for ( $i = 1; $i <= $pages; $i++ ){
if ( 1 != $pages && ( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
if ( $paged == $i ){
$pagination_html .= '<span class="current">' . $i . '</span>';
} else{
$pagination_html .= '<a href="' . get_pagenum_link( $i ). '" class="inactive">' . $i . '</a>';
}
}
}
if ( $paged < $pages && $showitems < $pages ){
$pagination_html .= '<a href="' . get_pagenum_link( $paged + 1 ) . '">›</a>';
}
if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages ){
$pagination_html .= '<a href="' . get_pagenum_link( $pages ) . '">»</a>';
}
$pagination_html .= '</div>';
return $pagination_html;
}
}
然后我在shortcode函数中调用了它
/*-------------------------------------------------------------------------*/
/* Grid Shortcode */
/*-------------------------------------------------------------------------*/
function RecentBlog($atts, $content = null) {
extract(shortcode_atts(array(
"comments" => 'true',
"date" => 'true',
"columns" => '4',
"limit" => '-1',
"title" => 'true',
"description" => 'true',
"cat_slug" => '',
"post_type" => '',
"excerpt_length" => '15',
"readmore_text" => '',
"pagination" => 'false'
), $atts));
global $post;
$postformat = get_post_format();
....
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
......
if (have_posts()) : while (have_posts()) : the_post();
$postformat = get_post_format();
if( $postformat == "" ) $postformat="standard";
$protected = "";
$portfoliogrid .= '<div class="' . $column_no . ' post grid-post post-' . $postformat . '">';
.....////
$portfoliogrid .= '<div class="summary-info">';
.......
$portfoliogrid .='</div>';
// If either of title and description needs to be displayed.
if ( $title == "true" || $description == "true" ) {
$portfoliogrid .='<div class="work-details">';
......
$portfoliogrid .='</div>';
}
$portfoliogrid .='</div>';
endwhile; endif;
if ( $pagination == "true" ){
if ( isset( $additional_loop ) ){
echo suareztheme_pagination( $additional_loop->max_num_pages );
} else {
echo suareztheme_pagination();
}
if ( function_exists("suareztheme_pagination") ) {
} else {
next_posts_link('«« Older Posts');
previous_posts_link('Newer Posts »»');
}
}
wp_reset_query();
return $portfoliogrid;
}
add_shortcode( "recentblog", "RecentBlog" );
上面的代码没有问题,但是有一个问题,当它在特定页面显示时,不管它在什么地方,它总是显示在最上面。非常感谢您对我的支持,在此先感谢。
在短代码回调中,您需要连接 return 和 html,例如:
$portfoliogrid .= suareztheme_pagination();
请注意,您必须使用 get_next_posts_link()
和 get_previous_posts_link()
而不是 next_posts_link()
和 previous_posts_link()
。