I have more than 15 posts and I am displaying only 6 per page load and remaining will display after the click on the pagination but the issue is I am not getting my pagination.

I am using below code

function recentPost_on_home(){
<div class="cp-seeWrapper"><div class="row"><div class="col-xl-8 col-lg-8 col-md-8 col-sm-12 col-xs-12"><div class="row">
   <?php $args = array('posts_per_page' => 6);
            $tyler_query = new WP_Query( $args );
            if ($tyler_query->have_posts()) {
                while ( $tyler_query->have_posts() ) {
            $categories = get_the_category(); $names = ''; if ( $categories && is_array( $categories )) { foreach ( $categories as $cat ) { $names .= $cat->name . ' '; } }?>
                <div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-xs-12">
                    <div class="cp-shadow cp-seeSinglePostWrapper">
                    <?php echo get_the_post_thumbnail();?>
                        <div class="bg-white single-post-box">
                            <div class="d-flex cp-CategoryList">
                                <div class="seeDate"><?php echo get_the_date('F j, Y');?></div>
                                <?php echo $names;?>
                            <div class="cp-heading"><h3><a href="<?php echo esc_url( get_the_permalink() );?>" title="<?php echo esc_attr( the_title_attribute('echo=0'));?>" class="seePost_title"><?php echo wp_trim_words(get_the_title(), 10, '...');?></a></h3></div>
                                <p><?php echo wp_trim_words(get_the_excerpt(), 20, '...');?></p>
            <?php  }?>
    <div class="nav-previous alignleft">hello<?php next_posts_link(); ?></div>
   <div class="nav-next alignright"><?php echo previous_posts_link( 'Newer posts' ); ?></div>
    <?php       }?>
    <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12 col-xs-12">
        <!--some logic here-->

    <?php return ob_get_clean(); 
add_shortcode( 'home_recent_post', 'recentPost_on_home' );

The second issue is, I am getting all the categories of the single product, I have to display only 2 categories.

For example, product is IOS and categories are iPhone and iMac,iPod. So I have to display iPhone and iMac only.

  • It is not clear what you are asking for and is not clear why you use the buffer. Does "After 'click'" means client-side scripting? Or you have to create some additional pages for categories? Did you do this? – Max Yudin Nov 2 '19 at 15:29
  • @MaxYudin, Sorry for the late reply, I am using buffer because I was getting some issue with HTML. My output was displaying before the parent div. Check this wordpress.stackexchange.com/questions/351688/… – Naren Verma Nov 2 '19 at 18:29

You're using a custom query (new WP_Query), so you need to pass $tyler_query->max_num_pages (the max pages) to the next_posts_link() function — more details here:

next_posts_link( null, $tyler_query->max_num_pages )

And btw, no need to echo with previous_posts_link() because the function indeed echoes the output. I.e. Just do so: <?php previous_posts_link(); ?>.

Also, you should add paged to your query args ($args):

$args = array(
    'posts_per_page' => 6,
    'paged'          => get_query_var( 'paged' ),

$tyler_query = new WP_Query( $args );

But if you're using the shortcode on a single post or Page, previous_posts_link() wouldn't work since it relies upon a global variable named $paged which is set only for archive-based requests such as search results. To make $paged works on single requests or with previous_posts_link(), you can try:

global $paged;
$paged = $paged ? $paged : get_query_var( 'page' );

$args = array(
    'posts_per_page' => 6,
    'paged'          => $paged,

For example, product is IOS and categories are iPhone and iMac,iPod. So I have to display iPhone and iMac only.

So after discussing via chat (to get more details about the above), you can use the following if you want to display the name of the first two of the categories: (which could actually be just one)

$names = array();
$categories = get_the_category();
//shuffle( $categories ); // uncomment if you want random entries
foreach ( $categories as $i => $term ) {
    if ( $i < 2 ) { // show at most two
        $names[] = '<a href="' . esc_url( get_category_link( $term->term_id ) ) . '">' . $term->name . '</a>';
$names = implode( ', ', $names );

And that would replace the $categories = get_the_category(); $names = ''; if ( $categories && is_array( $categories )) { foreach ( $categories as $cat ) { $names .= $cat->name . ' '; } }.

See here for more details about get_category_link().

Additional Notes

You should call wp_reset_postdata() after your custom loop ends: while ( ... ) { ... } wp_reset_postdata();. Or after the have_posts() block ends: if ( $tyler_query->have_posts() ) { ... } wp_reset_postdata();.

Doing that would restore the global $post variable to the current post in the main query (contained in the global $wp_query variable as opposed to your $tyler_query variable). And this restoration is necessary in order for template tags like the_title() to work in terms of referencing to the current post in the main query.

| improve this answer | |

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.