I am trying to make a comic website that has arrows pointing to First/Previous/Next/Latest on single.php. I've linked the first 3, but I'm struggling in figuring out how to link to Latest. It doesn't feel like it should be that hard, but Google only gave me ways to link it in the menu. Here's the code I have thus far for the Comic Navigation Menu. The last image with the alt "Last" is what I need to link:

<?php while ( have_posts() ) : the_post(); ?>
    <div class="col-md-offset-1 col-md-2 col-sm-2 comic-first">
        <a href="https://echomartin.com/unit418/comic/01/"><img src="https://echomartin.com/unit418/wp-content/uploads/2017/10/arrows-first.png" alt="First"></a>
    <div class="col-md-6 col-sm-6 comic-navigation">
        <?php the_post_navigation( array(
            'prev_text' => '<span class="screen-reader-text">' . __( 'Previous Post', 'multipurpose-blog' ) . '</span> <span class="comic-previous"><span class="nav-title-icon-wrapper">' . '</span>%title</span>',
            'next_text' => '<span class="screen-reader-text">' . __( 'Next Post', 'multipurpose-blog' ) . '</span><span class="comic-next">%title<span class="nav-title-icon-wrapper">' . '</span></span>',
        ) ); ?>
        <h3><?php the_title();?></h3>
    <div class="col-md-2 col-sm-2 comic-last">
        <img src="https://echomartin.com/unit418/wp-content/uploads/2017/10/arrows-last.png" alt="Last">
    <div class="clear"></div>

You'll need to use get_posts() to get the latest post, then pass it to get_the_permalink() to get the URL to it:

$latest_posts = get_posts( array( 'numberposts' => 1 ) );

if ( ! empty( $latest_posts ) ) :
    $latest_post = $latest_posts[0];

    if ( $latest_post->ID !== get_the_ID() ) :

        <a href="<?php echo get_the_permalink( $latest_post ); ?>"><img src="https://echomartin.com/unit418/wp-content/uploads/2017/10/arrows-last.png" alt="Last"></a>


Note that I used $latest_post->ID !== get_the_ID() to check if we were already on the latest post, and to only output the link if we weren't.

  • You are the absolute best. It worked perfectly. Thank you, thank you, thank you!! – Echo Martin Nov 1 '17 at 11:01

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.