0

I have a landing page that, among other things, features an excerpt from the most recent post with a "Continue Reading" link that should take the reader to the most recent post.

In my search, I've found a lot of guides on how to link to the most recent post within a specified category, but not much on the most recent post regardless of category(because it will vary).

ETA:

<li>
                <h2><a href="http://growingedgecoaching.com/blog">Growing Edge Blog</a></h2>
                    <?php
                    // Get the most recent post
                    $the_query = new WP_Query( 'posts_per_page=1' );

                    // Pull the excerpt
                    while ( $the_query->have_posts() ) : $the_query->the_post();
                        the_excerpt();
                    endwhile;

                    // Reset Post Data
                    wp_reset_postdata(); 
                    ?>
                <div class="forward-link">

                    <?php
                    $latest = new WP_Query( array( 'posts_per_page' => 1 ) );
                    while( $latest->have_posts() ) :
                        $latest->the_post();
                        echo '<a href="' . get_permalink() . '>' . get_the_title() . '</a>';
                    endwhile;
                    ?>

                </div><!-- end forward-link -->
            </li>

Any ideas? Thanks.

2
  • you want 2nd block to show recent post, 3rd block for tweeter right! what you want in first block ?
    – Amit Kosti
    Commented Aug 1, 2012 at 17:56
  • the first, far left block is the most recent post excerpt that should have the link to the full most recent post. the middle block is the "about" section, and the third, far right block contains the most recent tweets. Commented Aug 1, 2012 at 17:58

2 Answers 2

2

This code uses snippet from @Milo to suit your requirement.

Replace the code you've provided in question with this -

<li>
    <h2><a href="http://growingedgecoaching.com/blog">Growing Edge Blog</a></h2>
    <?php
    $latest = new WP_Query( array( 'posts_per_page' => 1 ) );
    while( $latest->have_posts() ) : $latest->the_post();
    ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    <div class="forward-link">
        <a href="<?php the_permalink(); ?>">Read More..</a>
    </div>


    <?php endwhile; ?>

</div><!-- end forward-link -->
</li>
  • Consider checking out Wordpress codex page for the_loop that has great info
1
  • As always Amit, thank you for your help. I try to reference the Codex as often as possible, I guess I just didn't make the connection between the_loop and this particular context :/ Commented Aug 1, 2012 at 18:30
1

If you query for a single post, you'll get the latest by default:

$latest = new WP_Query( array( 'posts_per_page' => 1 ) );
while( $latest->have_posts() ) :
    $latest->the_post();
    echo '<a href="' . get_permalink() . '>' . get_the_title() . '</a>';
endwhile;
2
  • Oh, good to know. How would I format what you've given me into a link? Commented Aug 1, 2012 at 16:35
  • Thank you, both for your help. I've edited my original question to reflect what I have now- it seems close to what I'm needing, but something is still not quite right. If you take a look at the live site, you'll noticed that the middle section is now on top of the first blog section. Commented Aug 1, 2012 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.