Forrest
Forrest

Reputation: 29

Wrapping WordPress Latest Post Link

I'm trying to wrap a div with a link to the latest post. Currently, the code returns the value of the featured image to use as the background image for a continuing div. I just want to wrap it with the url for the latest post. Thanks

<?php
/* Get Recent Post */
$recent_post = wp_get_recent_posts(array(
    'numberposts' => 1,
    'post_status' => 'publish'
));
/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
    /* Get Image */
    $image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
    /* Output Div with Image Set Inline, Use padding Top for Responsive Ratio Size */
    echo '
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div>
    ';                           
}

Upvotes: 2

Views: 40

Answers (1)

Josh Bonnick
Josh Bonnick

Reputation: 2813

Use get_the_permalink like so

/* If Featured Image Set */
if ( has_post_thumbnail($recent_post[0]['ID']) ){
    $image = wp_get_attachment_image_src( get_post_thumbnail_id($recent_post[0]['ID']), 'full');
    echo '<a href="' . get_the_permalink($recent_post[0]['ID']) . '">
<div class="featured-image-div" style="background-image:url('.$image[0].');"></div></a>
    ';                           
}

Function documentation: https://developer.wordpress.org/reference/functions/get_the_permalink/

Upvotes: 0

Related Questions