Wordpress无限上一个下一个循环在同一类别

时间:2015-07-08 20:14:35

标签: wordpress post categories next infinite

我有这段代码,但有人可以帮助我在自定义帖子'项目'

中使其在同一类别中工作
    <?php 
/**
 *  Infinite next and previous post looping in WordPress
 */
if( get_adjacent_post(false, '', true) ) { 
    previous_post_link('%link', '&larr; Previous Post');
} else { 
    $first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
        echo '<a href="' . get_permalink() . '">&larr; Previous Post</a>';
    wp_reset_query();
}; 

if( get_adjacent_post(false, '', false) ) { 
    next_post_link('%link', 'Next Post &rarr;');
} else { 
    $last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
        echo '<a href="' . get_permalink() . '">Next Post &rarr;</a>';
    wp_reset_query();
}; 

?>

1 个答案:

答案 0 :(得分:1)

// set current category
$categories = get_the_category();
$category = $categories[0];
$cat_ID = $category->cat_ID;    

// get next post link
$next_post = get_adjacent_post( true, '', false );
if( $next_post ) {
    echo '<a href="' . get_permalink( $next_post->ID ) . '">Next post</a>';
} else {
    $first = new WP_Query( array(
        'post_type' => 'project',
        'post_status' => 'publish',
        'posts_per_page' => 1,
        'order' => 'ASC',
        'cat' => $cat_ID
    ));

    $first->the_post();
    echo '<a href="' . get_permalink() . '">Next post</a>';
    wp_reset_query();
};

// show prev post link
$prev_post = get_adjacent_post( true, '', true );
if( $prev_post ) {
    echo '<a href="' . get_permalink( $prev_post->ID ) . '">Prev post</a>';
} else {
    $last = new WP_Query( array(
        'post_type' => 'project',
        'post_status' => 'publish',
        'posts_per_page' => 1,
        'order' => 'DESC',
        'cat' => $cat_ID
    ));

    $last->the_post();
    echo '<a href="' . get_permalink() . '">Prev post</a>';
    wp_reset_query();
};