0

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;
    }
0

Post 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 &raquo;' . '</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">&larr;</span> Older posts' ) ) . '</div>'
. '<div class="nav-next">' . get_previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>' ) ) . '</div>'
. '</div>' .
wp_reset_query();

}
add_shortcode( 'title-snippet', 'wpc_shortcode_title' );
| improve this answer | |
  • Thanks! This seems to work really well. I assume it's possible to configure it to show numbers 1 2 instead of "Older posts"/"Newer posts"? But anyway, you're true lifesaver, have been messing with it days already. – Kertix May 26 at 13:59
0

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 &raquo;' . '</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' );
| improve this answer | |
  • Thanks a lot.Solved a lot of issues! – Kertix May 26 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.