admin管理员组文章数量:1336186
I have made little shortcodes to insert data to my home screen. I loop over total number of posts and output pre-styled html.
My question is, is it possible to also make pagination for that kind of loops? For example if I have 10 posts and I would like to display 5 and then under these are link to another 5. Or if this isn't possible, what direction I should look?
Currently I'm displaying data like that (this is only for title as I groomed it to be shorter for putting it here):
add_shortcode('title-snippet', 'wpc_shortcode_title');
function wpc_shortcode_title() {
$args = array( 'numberposts' => '1000' );
$recent_posts = wp_get_recent_posts( $args );
$html = '<div></div>';
for($x = 0; $x < count($recent_posts); $x++) {
$html .= '
<div class="vce-text-block">
<div class="vce-text-block-wrapper vce">
<p>
<span style="color: #ffffff; font-family: Playfair Display;">
<span style="font-size: 21.3333px;">
'.$recent_posts[$x]["post_title"].'
</span>
</span>
</p>
</div>
</div>
';
}
return <<<HTML
$html
HTML;
}
I have made little shortcodes to insert data to my home screen. I loop over total number of posts and output pre-styled html.
My question is, is it possible to also make pagination for that kind of loops? For example if I have 10 posts and I would like to display 5 and then under these are link to another 5. Or if this isn't possible, what direction I should look?
Currently I'm displaying data like that (this is only for title as I groomed it to be shorter for putting it here):
add_shortcode('title-snippet', 'wpc_shortcode_title');
function wpc_shortcode_title() {
$args = array( 'numberposts' => '1000' );
$recent_posts = wp_get_recent_posts( $args );
$html = '<div></div>';
for($x = 0; $x < count($recent_posts); $x++) {
$html .= '
<div class="vce-text-block">
<div class="vce-text-block-wrapper vce">
<p>
<span style="color: #ffffff; font-family: Playfair Display;">
<span style="font-size: 21.3333px;">
'.$recent_posts[$x]["post_title"].'
</span>
</span>
</p>
</div>
</div>
';
}
return <<<HTML
$html
HTML;
}
Share
Improve this question
asked May 26, 2020 at 12:22
KertixKertix
34 bronze badges
2 Answers
Reset to default 0Post loop with pagination in the shortcode [title-snippet type="post" limit="5"]:
function wpc_shortcode_title( $atts ) {
extract( shortcode_atts( array( 'limit' => 5, 'type' => 'post'), $atts ) );
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
query_posts( array (
'posts_per_page' => $limit,
'post_type' => $type,
'order' => 'ASC',
'orderby' =>'menu_order',
'paged' => $paged ) );
$list = ' ';
while ( have_posts() ) { the_post();
$list .= '<article class="listing-view clearfix">'
. '<div class="listing-content">'
. '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>'
.'<p>' . get_the_excerpt() . '</p>'
. '<a href="' . get_permalink() . '">' . 'View »' . '</a>'
. '</div>'
. '<a class="listing-thumb" href="' . get_permalink() . '">' . get_the_post_thumbnail($page->ID, 'listing-thumb') . '<span></span></a>'
. '</article>';
}
return
'<div class="listings clearfix">'
. $list
. '<div class="nav-previous">' . get_next_posts_link( __( '<span class="meta-nav">←</span> Older posts' ) ) . '</div>'
. '<div class="nav-next">' . get_previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>' ) ) . '</div>'
. '</div>' .
wp_reset_query();
}
add_shortcode( 'title-snippet', 'wpc_shortcode_title' );
Yes it is possible to show number pagination. Try below-updated code:
function wpc_shortcode_title( $atts ) {
extract( shortcode_atts( array( 'limit' => 5, 'type' => 'post'), $atts ) );
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
query_posts( array (
'posts_per_page' => $limit,
'post_type' => $type,
'order' => 'ASC',
'orderby' =>'menu_order',
'paged' => $paged ) );
$list = ' ';
while ( have_posts() ) { the_post();
$list .= '<article class="listing-view clearfix">'
. '<div class="listing-content">'
. '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>'
.'<p>' . get_the_excerpt() . '</p>'
. '<a href="' . get_permalink() . '">' . 'View »' . '</a>'
. '</div>'
. '<a class="listing-thumb" href="' . get_permalink() . '">' . get_the_post_thumbnail($page->ID, 'listing-thumb') . '<span></span></a>'
. '</article>';
}
global $wp_query;
$args_pagination = array(
'base' => add_query_arg( 'paged', '%#%' ),
'total' => $wp_query->max_num_pages,
'current' => $paged
);
$list .= '<div class="post-nav">';
$list .= paginate_links( $args_pagination);
$list .= '</div>';
return
'<div class="listings clearfix">'
. $list
. '</div>' .
wp_reset_query();
}
add_shortcode( 'title-snippet', 'wpc_shortcode_title' );
本文标签: shortcodePagination for custom posts loop
版权声明:本文标题:shortcode - Pagination for custom posts loop 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742400908a2467863.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论