0

I would like to know how to know if the next article is the most recent ? And if is not the most recent, dont' show the content (The right arrow and "Article suivant") please,

Here is my current code :

For next :

<?php
$next_post = get_next_post();
if (!empty( $next_post )): ?>
  <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>"><?php echo esc_attr( $next_post->post_title ); ?></a>
<?php endif; ?>

For previous :

<?php
$prev_post = get_previous_post();
if (!empty( $prev_post )): ?>
  <a href="<?php echo $prev_post->guid ?>"><?php echo $prev_post->post_title ?></a>
<?php endif ?>

Thank you every one !!

0

Sounds like WordPress does not support this out of the box. However, you can get the most recent post via wp_get_recent_posts(), and then check against the result from there.

Here I am using array_shift() to turn the array containing one post into the post itself.

$args = array(
    'numberposts' => 1,
    'post_status' => 'publish',
);
$most_recent_post = array_shift(wp_get_recent_posts( $args, ARRAY_A ));

$next_post = get_next_post();
if (!empty( $next_post )) {
    // is the next post the most recent post?
    if ($most_recent_post['ID'] === $next_post->ID) {
        // next post is most recent post
    } else {
        // next post is not most recent post
    }
}
  • and in the : if ($most_recent_post['ID'] === $next_post->ID) { i said the right container display none ? – sbzc Jun 29 '17 at 12:36
  • I do not know your code. But yes, inside the if put the code that is going to be executed, if it is the most recent post – kero Jun 29 '17 at 12:38
  • and what is for get previous post please ? – sbzc Jul 4 '17 at 20:40
  • You can use get_previous_post() for that instead – kero Jul 5 '17 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.