Get latest post link on WordPress

I have that site : http://ougk.gr and I want on the navigation to have a link which points to the latest post of a specific category (full post with comments etc). How can I achieve that?

Related posts

Leave a Reply

3 comments

  1. There are a few ways to do this. This one uses wp_get_recent_posts(), and prints a basic link:

    <nav>
    
        <?php
            $args = array( 'numberposts' => '1', 'category' => CAT_ID );
            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ){
            echo '<a href="' . get_permalink($recent["ID"]) . '">Latest Post</a>';
            }
        ?>
    
        // .. other menu code ..
    
    </nav>
    

    Where CAT_ID is the id of the targeted category. For your situation the simple answer is to insert the link code just after the opening nav tag, as above.

    To place the link somewhere else in the nav, you’ll have to dive into some of the other functions called in the code you pasted. It might be a good idea to get your hands dirty..

  2. <?php 
        $args = array( 
            'numberposts' => '1', 
        );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ):
    
        $post_id        = $recent['ID'];
        $post_url       = get_permalink($recent['ID']);
        $post_title     = $recent['post_title'];
        $post_content   = $recent['post_content'];
        $post_thumbnail = get_the_post_thumbnail($recent['ID']);
    
        endforeach;
    ?>
    
  3. You need ot get the title and perma link

    <?php
    // retrieve one post with an ID of 5
    query_posts( 'cat=X&posts_per_page=1&order=DESC' );
    
    // the Loop
    while (have_posts()) : the_post();
           echo "<a href='<?php the_permalink(); ?>'>";
           the_title();
           echo "</a>";
    endwhile;
    ?>