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?
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?
Share Improve this question asked Sep 11, 2019 at 16:43 vonnievonnie 431 gold badge1 silver badge4 bronze badges1 Answer
Reset to default 0The 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>';
}