自定义单一模板内的Wordpress循环分页

时间:2015-02-17 11:21:08

标签: wordpress pagination custom-post-type

我尝试了很多解决方案和更改,但我无法在单个模板中的post_type循环中进行分页。

这是我测试的最后一个代码:

<?php 
    $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
    $wp_query = new WP_Query(); 
    $wp_query->query('post_type=some_listing&taxonomy=some_listing_category&field=ID&posts_per_page=2'.'&terms='.$someterm .'&paged='.$paged);  
?>

<?php if( $wp_query->have_posts() ): ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

// loop

<?php endwhile; ?>

<?php
    next_posts_link( 'Older Entries', $the_query->max_num_pages );
    previous_posts_link( 'Newer Entries' );
?>

<?php wp_reset_postdata(); ?>
<?php endif; ?>

另外我在functions.php文件中有这个:

add_action('template_redirect', function() {
    if ( is_singular('areas') ) {
        global $wp_query;
        $page = (int) $wp_query->get('page');
        if ( $page > 1 ) {
          // convert 'page' to 'paged'
          $query->set( 'page', 1 );
          $query->set( 'paged', $page );
        }
        // prevent redirect
        remove_action( 'template_redirect', 'redirect_canonical' );
    }
}, 0 );

3 个答案:

答案 0 :(得分:0)

一些建议

//set the "paged" parameter (use 'page' if the query is on a static front page)
  $paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;

尝试添加此类链接

<?php next_post_link('%link', 'Newer entry &gt;', true) ?>
<?php previous_post_link('%link', '&lt; Older entry', true) ?>

答案 1 :(得分:0)

一位朋友解决了这个问题,解决方案是这个函数,只是将它添加到functions.php。

function custom_disable_redirect_canonical( $redirect_url ){
    global $post;
    $ptype = get_post_type( $post );
    if ( $ptype == 'post' ) $redirect_url = false;
    return $redirect_url;
}
add_filter( 'redirect_canonical','custom_disable_redirect_canonical' );

答案 2 :(得分:0)

尝试使用此代码进行自定义循环分页:

<?php
if ( get_query_var('paged') ) {
    $paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
    $paged = get_query_var('page');
} else {
    $paged = 1;
}

$custom_query_args = array(
    'post_type' => 'some_listing', 
    'posts_per_page' => get_option('posts_per_page'),
    'paged' => $paged,
    'post_status' => 'publish',
    'ignore_sticky_posts' => true,
    'order' => 'DESC', // 'ASC'
    'orderby' => 'date'
);
$custom_query = new WP_Query( $custom_query_args );

if ( $custom_query->have_posts() ) :
    while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

        <article <?php post_class(); ?>>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
            <div><?php the_excerpt(); ?></div>
        </article>

    <?php
    endwhile;
    ?>

    <?php if ($custom_query->max_num_pages > 1) : // custom pagination  ?>
        <nav class="prev-next-posts">
            <div class="prev-posts-link">
                <?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
            </div>
            <div class="next-posts-link">
                <?php echo get_previous_posts_link( 'Newer Entries' ); ?>
            </div>
        </nav>
    <?php endif; ?>

<?php
    wp_reset_postdata(); // reset the query 
else:
    echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>

来源: