0

I have created a function to use as a shortcode to allow me to display a specific post type on my page. The function works well, but I would like to the results to be paged, and the navigation only to appear if needed. her is my current code


extract(shortcode_atts(array("country" => '' ), $atts));
$country = preg_replace('~�*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $country );  
$argsq = array ('post_type' => 'quote',
                    'country' => $country,
                'orderby' => 'meta_value' 
              );


$testim= get_posts($argsq );
$output = '';
foreach ($testim as $testim_post){

$output .= "";
$output .= "".$testim_post->post_content."
"; $output .= $testim_post->post_title; $output .= ""; } return $output;

1 Answer 1

1

add this pagination function:

function pagination( $query, $baseURL = get_bloginfo( $url ), $echo = true ) {  
    $page = $query->query_vars["paged"];  
    if ( !$page ) $page = 1;  
    $qs = $_SERVER["QUERY_STRING"] ? "?".$_SERVER["QUERY_STRING"] : "";  
    // Only necessary if there's more posts than posts-per-page  
    if ( $query->found_posts > $query->query_vars["posts_per_page"] ) {  
        $re = '<ul class="paging">'; 
        // Previous link? 
        if ( $page > 1 ) { 
            $re .= '<li class="previous"><a href="'.$baseURL.'page/'.($page-1).'/'.$qs.'">« previous</a></li>'; 
        } 
         // Loop through pages 
        for ( $i=1; $i <= $query->max_num_pages; $i++ ) { 
            // Current page or linked page? 
            if ( $i == $page ) { 
                $re .= '<li class="active">'.$i.'</li>'; 
            } else { 
                $re .= '<li><a href="'.$baseURL.'page/'.$i.'/'.$qs.'">'.$i.'</a></li>'; 
            } 
        } 
        // Next link? 
        if ( $page < $query->max_num_pages ) { 
        $re .= '<li><a href="'.$baseURL.'page/'.($page+1).'/'.$qs.'">next »</a></li>'; 
        } 
        $re .= '</ul>';  
        if ($echo){
            echo $re;
        }else{
            return $re;
        }
    }  
}  

then add the paged parameter to your shortcode query and get the current page permalink:

global $post;
$baselink = get_permalink($post->ID);
extract(shortcode_atts(array("country" => '' ), $atts));
$country = preg_replace('~?*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $country ); 
$page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$argsq = array (
    'post_type' => 'quote',
    'country' => $country,
    'orderby' => 'meta_value', // you have to specify a meta_key for this to work
    'page' => $page 
);


$testim= get_posts($argsq );

and just before you return the pagination with your output:

return $output .pagination($testim,$baselink,false);
2
  • thx Bainterent, I am not getting the last bit, ie return $output .pagination($testim,$baselink,false); where this should go?
    – salocin
    Commented Mar 21, 2011 at 15:26
  • @salocin: replace your return $output; with return $output .pagination($testim,$baselink,false); at the end of your shortcode function.
    – Bainternet
    Commented Mar 21, 2011 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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