codylthomas
Member
Posted 2 years ago #
On my site, I have a navigation bar of image links on the static front page. I want one of the images to link to the permalink of the most recent post. I don't want it to go to the most recent post in the feed. I don't need a Most Recent post widget. I want the link to go to the single post permalink of the most recent post, so that it already goes to the page where people can comment on the post.
However, obviously, the permalink of the most recent post is going to change each time I post. I don't want to change this link on my static frontpage every time I post.
What PHP script can I write to create this link?
This reply is 6-months late - but hey better late than never right?
<?php
global $post;
$latest_posts = get_posts('numberposts=1');
foreach($latest_posts as $latest) {
echo '<a href="'.get_permalink($latest->ID).'">'.$latest->post_title.'</a>';
}
?>
parablese
Member
Posted 2 years ago #
Hey mindshare, thanks for the code. (better late than never indeed)
I'm using it now and it does just what I was trying to do.
Now I'm just wondering.. how would one go about modifying it to link to the most recent subpage of a page? I sort_order=desc my subpages after numbering them chronologically, so I feel like this would be feasible. I even tried replacing all the instances of "post" with "page" in your code, and it returned (plenty of) pages, haha.
This PHP-newb would greatly appreciate it.
mskala23
Member
Posted 1 year ago #
how can this be used on another site, separate from the blog?
mskala23
Member
Posted 1 year ago #
...and this code is working amazing so far. I just need to find a way to use it on an external site.