Pregunta

Is it possible when using the previous_post_link() function to make it get the previous post after next i.e. make it skip a post. So if you're on post 5 in numerical order it skips to post 3?

The reason being I have custom post type single-cpt.php file that pulls in two posts, so when using the previous post link out of the box it means when you go to the next post, one of the ones from the previous post is there again.

Any help would be fabulous.

¿Fue útil?

Solución

$current_id = get_the_ID();
$cpt = get_post_type();
$all_ids = get_posts(array(
    'fields'          => 'ids',
    'posts_per_page'  => -1,
    'post_type' => $cpt,
    'order_by' => 'post_date',
    'order' => 'ASC',
));
$prev_key = array_search($current_id, $all_ids) - 2;
$next_key = array_search($current_id, $all_ids) + 2;
if(array_key_exists($prev_key, $all_ids)){
    $prev_link = get_the_permalink($all_ids[$prev_key]);
    echo $prev_link;
}
if(array_key_exists($next_key, $all_ids)){
    $next_link = get_the_permalink($all_ids[$next_key]);
    echo $next_link;
}

So I queried all the posts IDs from the current post type. Then since it’s a simple array key=>value, just found the current post key in the array and just added or subtracted so if your current post is the 8th one, your next is 10 and you previous is 6. Then if your array doesn’t have enough keys, say your current was the 8th but your array only had 9 that will mess it up, I check to see if the key exists. If it does, use get_the_permalink() with the value of the desired key.

Probably not the most graceful way to do it but…

Otros consejos

I wrote a couple functions that trick WordPress into thinking we're on the previous/next post before calling previous_post_link or next_post_link:

/**
 * Displays the next post link that is adjacent to the next post.
 *
 * @param object       $next_post      Optional. The next post to reference. Default is current post.
 * @param string       $format         Optional. Link anchor format. Default '« %link'.
 * @param string       $link           Optional. Link permalink format. Default '%title'
 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term. Default false.
 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
 */
function next_next_post_link( $next_post = null, $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
    global $post;

    // Keep track of the current post so we can reset back later.
    $current_post = $post;

    // If a "next post" is specified, use that.
    if ( $next_post ) {
        $post = $next_post;
        setup_postdata( $post );
    }

    // Make WordPress think we're on the next post.
    $post = get_next_post();
    setup_postdata( $post );

    // Echo the next post link, skipping the next post.
    next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );

    // Reset everything back.
    $post = $current_post;
    wp_reset_postdata();
}


/**
 * Displays the previous post link that is adjacent to the previous post.
 *
 * @param object       $previous_post  Optional. The previous post to reference. Default is current post.
 * @param string       $format         Optional. Link anchor format. Default '« %link'.
 * @param string       $link           Optional. Link permalink format. Default '%title'.
 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term. Default false.
 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default empty.
 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
 */
function previous_previous_post_link( $previous_post = null, $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
    global $post;

    // Keep track of the current post so we can reset back later.
    $current_post = $post;

    // If a "previous post" is specified, use that.
    if ( $previous_post ) {
        $post = $previous_post;
        setup_postdata( $post );
    }

    // Make WordPress think we're on the previous post.
    $post = get_previous_post();
    setup_postdata( $post );

    // Echo the previous post link, skipping the previous post.
    previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );

    // Reset everything back.
    $post = $current_post;
    wp_reset_postdata();
}

Add those functions to your functions.php file, and use next_next_post_link and previous_previous_post_link the same way you would use next_post_link and previous_post_link

Result: screenshot showing post #3 with links to post #1 and post #5

Licenciado bajo: CC-BY-SA con atribución
scroll top