WordPress.org

Ready to get started?Download WordPress

Forums

Pull Most Recent Post Excerpt (4 posts)

  1. muellerju
    Member
    Posted 6 years ago #

    Hello,
    I am trying to pull an excerpt of the most recent post made in my blog to my static main page via php, I can get a certain post by using this:

    <?php
    $new_id = 1;
    $post_id = get_post($new_id);
    $title = $post_id->post_content;
    echo($title);
    ?>

    But I want an excerpt from the newest post made if possible or just how to get it's ID would be great. Thank you for any advice!

  2. Brayne
    Member
    Posted 6 years ago #

    This works for me.

    <?php $postslist = get_posts('category=1&numberposts=1&order=DESC&orderby=post_date');
    foreach ($postslist as $post) :
    setup_postdata($post); ?>
    <div class="post_item">
    <span class='side_date'><?php the_time('F j, Y'); ?></span><br />
    <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    </div>
    <?php endforeach; ?>
  3. lonchbox
    Member
    Posted 5 years ago #

    Hi, I´m trying to show last post on my home page and I try using your code
    THe thing is I will like to show lastes post but excluding some categories. So with out know well programming I just copy&paste your code and made this modification but still showme a post that I put on the exclude.

    <div id="lastest-post">
    	   	<?php $postslist = get_posts('numberposts=1&order=DESC&orderby=post_date&exclude=142,143,144,157');
    		foreach ($postslist as $post) :
    		setup_postdata($post); ?>
    		<div>
    			<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    			<h4><?php the_excerpt(); ?></h4>
    		</div>
    		<?php endforeach; ?>
    	</div>

    Any advice ?

  4. moshu
    Member
    Posted 5 years ago #

    Try reading the Docs: Template_Tags/get_posts

    What you did: excluded the posts having those IDs. If you want to exclude a category, see the documentation.

Topic Closed

This topic has been closed to new replies.

About this Topic