• Here’s the situation. This code is on a website with about 400 posts. On several pages of the site, I’m using a shortcode called [show_posts], and I have it setup so that it will pull in category and/or tags to filter the posts that are displayed.
    One example is [show_posts tag=”news”] another is [show_posts category=”fitness” posts_per_page=”10″]

    I just realized that the pagination isn’t working anymore.

    Can anyone please help me!!?? (I think I’ve included everything needed from my functions.php file)

    /**********************************************************************
    SETUP PAGINATION
    ***********************************************************************/
    function paginationmen($pages = '', $range = 4)
    {
         $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)
         {
    		 /*<span>Page ".$paged." of ".$pages."</span>*/
             echo "<div class='pagination-list'>";
             if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
             if($paged > 1 && $showitems < $pages) echo "<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 ))
                 {
                     echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
                 }
             }
    
             if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">›</a>";
             if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
             echo "</div>";
         }
    }
    
    /**********************************************************************
    GET POST IMAGE
    ***********************************************************************/
    
    function post_main_image() {
       global $post, $posts;
       $first_img = '';
       ob_start();
       ob_end_clean();
       $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
       $first_img = $matches [1] [0];
    
       // no image found display default image instead
    	if(empty($first_img)){
    		return "";
    	}else{
    		if(isImage($first_img)==false){
    			return "";
    		}else{
    			return $first_img;
    		}
       }
    }
     function isImage($url)
      {
        $pos = strrpos( $url, ".");
    	if ($pos === false)
    	  return false;
    	$ext = strtolower(trim(substr( $url, $pos)));
    	$imgExts = array(".gif", ".jpg", ".jpeg", ".png", ".tiff", ".tif"); // this is far from complete but that's always going to be the case...
    	if ( in_array($ext, $imgExts) )
    	  return true;
        return false;
      }
    
    /**********************************************************************
    BLOG shortcode [show_posts]
    ***********************************************************************/
    
    function blogposts_func($atts ){
    
     extract(shortcode_atts( array(
    		'category' => '',
    		'tag' => '',
    		'view' => '10',
    	), $atts ) );
    
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    	$loop =new WP_Query(array( 'post_type' => 'post',
    		'order'           => 'DESC',
    		'tag' => $tag,
    		'category_name'  =>$category,
    		'posts_per_page' =>$view,
    		'echo' =>0,
    		'offset' => 0,
    		'paged' => $paged,
    		 ));
    	ob_start();	 	
    
    		while ($loop->have_posts() ) : $loop->the_post();
            ?>
                <div class="show-post list-post">
               <?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) { ?>
                <div class="list-post-image">
                    <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                      <?php the_post_thumbnail(array(108,108));  ?>
                    </a>
                    </div>
                    <div class="list-post-text short-text">
                    <a href="<?php the_permalink() ?>"  title="<?php the_title_attribute(); ?>"><h1><?php the_title(); ?></h1></a>
                    <p><?php echo get_the_date('l, F j, Y'); ?></p>
                    </div>
                	<br class="clear" />
                <?php } else {
                if(post_main_image()==""){
                ?>
                    <div class="list-post-text">
                    <a href="<?php the_permalink() ?>"><h1><?php the_title(); ?></h1></a>
                    <p><?php echo get_the_date('l, F j, Y'); ?></p>
                    </div>
                <br class="clear" />
                <?php
                }else{
                ?>
                    <div class="list-post-image">
                    <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><img src="<?php echo post_main_image();?>" alt="Image from: <?php the_title_attribute(); ?>" /></a>
                    </div>
                    <div class="list-post-text short-text">
                    <a href="<?php the_permalink() ?>"  title="<?php the_title_attribute(); ?>"><h1><?php the_title(); ?></h1></a>
                    <p><?php echo get_the_date('l, F j, Y'); ?></p>
                    </div>
                <br class="clear" />
                <?php
                }
                }
                ?>
                </div>
            <?php
           endwhile;
            ?>
            <!-- pagination -->
    <?php
    if (function_exists("paginationmen")) {
    	paginationmen($loop->max_num_pages);
    }
    
    ?>
    
      <!--End pagination-->
        <!--End post-->
    <?php
    	$Output_string = ob_get_contents ();
    	ob_end_clean();
    	//$return = $content . $contentf;
    
    	return $Output_string;
    
    }
    add_shortcode( 'show_posts', 'blogposts_func' );
  • The topic ‘Pagination not working in custom shortcode’ is closed to new replies.