函数中GET_POST的分页结果

时间:2011-03-21 作者:salocin

我创建了一个函数,用作短代码,允许我在页面上显示特定的帖子类型。该功能运行良好,但我希望结果被分页,导航只在需要时出现。她是我当前的密码


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 个回复
SO网友:Bainternet

添加此分页功能:

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;
        }
    }  
}  
然后将分页参数添加到快捷码查询中,并获取当前页面永久链接:

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 );
在返回分页和输出之前:

return $output .pagination($testim,$baselink,false);

结束

相关推荐