post - Wordpress infinite previous next looping in same category -
i have code can me make work in same category in custom post = 'project'
<?php /** * infinite next , 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(); }; ?>
// set current category $categories = get_the_category(); $category = $categories[0]; $cat_id = $category->cat_id; // 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(); };
Comments
Post a Comment