Forums

Making a Permanent Link to the Most Recent Post (3 posts)

  1. codylthomas
    Member
    Posted 8 months 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?

  2. mindshare
    Member
    Posted 2 months ago #

    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>';
    }
    ?>
  3. parablese
    Member
    Posted 2 weeks 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.

Reply

You must log in to post.

About this Topic