admin管理员组文章数量:1414614
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>';
}
本文标签: categoriesGet permalink to latest post in category
版权声明:本文标题:categories - Get permalink to latest post in category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745167417a2645765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论