Paginate高级自定义字段导致WordPress

时间:2014-12-12 11:02:59

标签: php wordpress pagination advanced-custom-fields paginate

我目前正在使用“高级自定义字段”' WordPress插件,我想知道是否可以对我的搜索结果进行分页。

以下是我在自定义页面模板上使用的代码:

<?php while ( have_posts() ) : the_post(); ?> 

<?php
$args = array( 'posts_per_page' => 5, 'meta_key' => 'course_date', 'order'=> 'ASC', 'orderby' => 'meta_value', 'category' => 2 );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>

<!--- CONTENT --->  

<?php
endforeach; 
wp_reset_postdata();
?> 

1 个答案:

答案 0 :(得分:1)

您必须使用WP_Query类http://codex.wordpress.org/Class_Reference/WP_Query

    <?php while ( have_posts() ) : the_post(); ?> 

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array( 'posts_per_page' => 5, 'meta_key' => 'course_date', 'order'=> 'ASC', 'orderby' => 'meta_value', 'cat' => 2 ,'paged'=>$paged);
$catpost_ = new WP_Query( $args );
if ($catpost_->have_posts() ) : 
    while ($catpost_->have_posts() ) : $catpost_->the_post();

 ?>

<!--- CONTENT --->
<?php
 endwhile; 
 endif;
 wp_reset_query();?>
<?php
            $big=76;
        $args = array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format'       => '?paged=%#%',
'total'        => $catpost_->max_num_pages,
'current'      => $paged,
'prev_next'    => True,
    'prev_text'    => __('Previous'),
    'next_text'    => __('Next'),

'type'         => 'list');
// ECHO THE PAGENATION 
echo paginate_links( $args );
endwhile;
?>

这可能适合你。