• Hung PD

    (@nhalauxehoi)


    WordPress pagination function in shortcode always displayed on the top. Please have a look on below code

    /*-------------------------------------------------------------------------*/
        /* 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;
        	}
    
        }

    Then I called it in the shortcode function.

    /*-------------------------------------------------------------------------*/
        /* 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" );

    The above code is working fine , but it has one problem, when it display in specific page, it always displayed on the top regardless it’s place. So appreciate your support to help me, thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • sachinyadav82

    (@sachinyadav82)

    Hello Hung The,

    I also faced the same problem while writing my first shortcut. So what i did is suggesting you.

    When you are returning any value from a function you need to use output buffer.

    So please change your function as follows:

    /*-------------------------------------------------------------------------*/
        /* Custom Pagination */
        /*-------------------------------------------------------------------------*/
    
        function suareztheme_pagination($pages = '', $range = 2){
    <strong>ob_start();</strong> //  please add just after the start of function
        	$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;
    
    <strong>
    return ob_get_clean();</strong> //Put it at the end.
    
        	}
    
        }
    sachinyadav82

    (@sachinyadav82)

    Basically it should be like this.

    function suareztheme_pagination($pages = ”, $range = 2){

    ob_start(); // please add just after the start of function

    All actual code in between


    return ob_get_clean();
    //Put it at the end.

    }

    Thread Starter Hung PD

    (@nhalauxehoi)

    Hi @sachinyadav82

    I fixed my problem. In the shortcode callback, you only need to concatenate and return the html:

    $portfoliogrid .= suareztheme_pagination();

    Also thanks to your suggest. I’ll take a look ??

    Regards,
    Hung The.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WordPress pagination fuction in shortcode always displayed on the top’ is closed to new replies.