Getting first & last post in custom post type outs

2019-08-31 09:18发布

问题:

I'm using WP, and have a script where when an image is clicked, the single post content loads using .load(). The arrows to navigate through each single post is located inside the .project div that is being loaded using .load().

Problem is, on the first and last posts, I only want to display certain arrows.

For example, the first item in the post gets loaded in, it shouldn't have the 'previous' arrow because there are no previous posts. Same with the last post and the 'next' arrow.

So basically to work around this, I'm trying to come up with a PHP statement (without being in the loop) to tell if the current post is the last post or first post in the custom post type.

Here's what I have so far.. just not sure how to get ID's of first post and last post outside of the loop. Everything else besides that has been tested and works. Below is just the 'logic' behind the code mostly.

<?php
// Get other posts in post type
$next_post = get_next_post();
$previous_post = get_previous_post();
$current_id = get_the_ID();

// Get ID's of posts
$next_id = $next_post->ID;
$previous_id = $previous_post->ID;

// if($next_id == $previous_id) because on first/last posts, get_next_post
// and get_previous_post return the same value.
if($next_id == $previous_id) {
    if() { 
        // if last post in custom post type
    } else() {
        // if first post in custom post type
    }
}
?>

<?php if(isnt first post) { ?>
    <li class="left" data-projectid="<?php echo $next_id; ?>"></li>
<?php } ?>
<li class="grid"></li>
<?php if(isnt last post) { ?>
    <li class="right" data-projectid="<?php echo $previous_id; ?>"></li>
<?php } ?>

回答1:

I haven't worked with WP so much, but since WP templates are all PHP files and WP exposes its own API to user you can use any PHP syntax in them. If you're not concerned about running two queries each time you navigate your page then this will help you to get the idea.

<?php

global  $wpdb;
$last_one = FALSE;
$first_one = FALSE;

// Get last one
$last_result = $wpdb->get_results("SELECT `id` FROM `posts` ORDER BY `id` DESC LIMIT 0, 1", ARRAY_A);
if($last_result){ if($last_result['id'] == $next_post){ $last_one = TRUE; } }

// Get first one
$first_result = $wpdb->get_results("SELECT `id` FROM `posts` ORDER BY `id` ASC LIMIT 0, 1", ARRAY_A);
if($first_result){ if($first_result['id'] == $previous_post){ $first_one = TRUE; } }

?>

Remember to check the names of fields and tables as I don't know the names.



回答2:

Ended up using this code and it works fine...

Edit: updated code.. if for any reason somebody else ever needs it:

$args = array('post_type'=>'your_post_type', 'posts_per_page' => -1);
$posts = get_posts($args);
$first_id = $posts[0]->ID; // To get ID of first post in custom post type 
// outside of loop


$last_id = end($posts);
echo $last_id->ID; // To get ID of last post in custom post type outside of loop

if($current_id != $first_id) { ?>
    <li class="left" data-projectid="<?php echo $previous_id; ?>"></li>
<?php } ?>
<?php if($current_id != $last_id->ID) { ?>
    <li class="right" data-projectid="<?php echo $next_id; ?>"></li>
<?php } ?>


标签: php wordpress