0

I need to get the permalink to the latest post in just one category. That link then needs to be placed into a button. The button is rendered within a function in a custom front-page.php template.

I have this so far but it is not working with various permutations and no errors are showing up:

$latest_post = get_post( array( 'cat' => 3, 'posts_per_page' => 1) );
if( $latest_post ) {
    echo '<a href= "' . get_permalink( $latest_post->ID ) . '">Learn More Now</a>';
}

Can anyone help show me where I've gone wrong?

0

The function you are using here, get_post() does not accept query arguments. So you have to use get_posts(). Try this:

$latest_post = get_posts( array( 'cat' => 3, 'posts_per_page' => 1) );
if( !empty( $latest_post ) ) {
    echo '<a href= "' . get_permalink( $latest_post[0] ) . '">Learn More Now</a>';
}
  • arrrggh, for want of an "s"! Thank you @Sohan, it's exactly what I needed and works perfectly. – vonnie Sep 11 at 19: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.