我有这段代码,但有人可以帮助我在自定义帖子'项目'
中使其在同一类别中工作 <?php
/**
* Infinite next and previous post looping in WordPress
*/
if( get_adjacent_post(false, '', true) ) {
previous_post_link('%link', '← Previous Post');
} else {
$first = new WP_Query('posts_per_page=1&order=DESC'); $first->the_post();
echo '<a href="' . get_permalink() . '">← Previous Post</a>';
wp_reset_query();
};
if( get_adjacent_post(false, '', false) ) {
next_post_link('%link', 'Next Post →');
} else {
$last = new WP_Query('posts_per_page=1&order=ASC'); $last->the_post();
echo '<a href="' . get_permalink() . '">Next Post →</a>';
wp_reset_query();
};
?>
答案 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();
};