WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] the_excerpt not showing posts (5 posts)

  1. achenn
    Member
    Posted 1 year ago #

    I'm using the following code to show the single most recent post on my landing page:

    pastebin

    My problem is the code doesn't seem to be producing an excerpt. What could be the problem?

    Live site.

  2. Luke Woodward
    Member
    Posted 1 year ago #

    With a quick glance at the code, it looks like you are using the global $wp_query, but you haven't declared it. Before you can use this you would need to declare:
    global $wp_query;
    I'm not sure why you even have this bit in your, code though. Your logic looks like it's trying to check if there is only one post, then show the excerpt if so, then you go and show the excerpt on else anyway.

    Can you give a more detailed explanation of what you are trying to accomplish?

  3. achenn
    Member
    Posted 1 year ago #

    I have a landing page that will, among other things, feature a small excerpt of the most recent blog post.
    Currently, that section has only a link to the landing page.

  4. Luke Woodward
    Member
    Posted 1 year ago #

    You will probably want to do something like this:

    <?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();
  5. achenn
    Member
    Posted 1 year ago #

    That works perfectly, thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic