• Resolved TonyaOD

    (@tonyaod)


    Hi there

    I have created a homepage template that is my static front / home page

    I want to add my most recent post onto it under my static comment.

    I have read numerous posts & lots of the php wordpress pages and my head is spinning …. Just when I think I understand what I am reading I realise I havn’t a clue ?? LOL

    Ok so far I have 2 bits of code : – this one displays my most recent post – yay but just a link to it – how do I add the bit that actually shows the content – when I try I get errors

    <?php
        $args = array( 'numberposts' => '1' );
        $recent_posts = wp_get_recent_posts( $args );
    
        foreach( $recent_posts as $recent ){
            echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> '; $recent["post_content"];}
    ?>

    This one works it displays content – BUT it shows lots of recent posts not just THE most recent 1 !!!

    <?php
        $query = new WP_Query( array ( 'orderby' => 'date', 'order' => 'DESC' ) );
    
        while ( $query->have_posts() ) :
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>'; ?>
    	<div class="entry-content">
    		<?php the_content(); ?>
    		<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'scrappy' ), 'after' => '</div>' ) ); ?>
    	</div><!-- .entry-content -->
    	<span class="media-posted-on">
    		<?php edit_post_link( __( 'Edit', 'scrappy' ), '<span class="media-edit-link">', '</span>' ); ?>
    	</span>
    
    <?php endwhile;
    ?>

    Which is my best solution & how do I get the content of my most recent posts – just the 1 ?? PLEASE – thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • <?php
    $query = new WP_Query( array ( 'orderby' => 'date', 'order' => 'DESC', 'showposts' => '1' ) );
    if ( $query->have_posts() ) {
        $query->the_post();
        echo '<li>' . get_the_title() . '</li>';
        echo '<div class="entry-content">';
        the_content();
        wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'scrappy' ), 'after' => '</div>' ) );
        echo '</div><!-- .entry-content -->';
        echo '<span class="media-posted-on">';
        edit_post_link( __( 'Edit', 'scrappy' ), '<span class="media-edit-link">', '</span>' );
        echo '</span>';
    }
    ?>

    Since you’re only wanting one post, I changed while to if, the the end result is the same.

    Or if you’re “echo” adverse, you could do it like this: ??

    <?php
    $query = new WP_Query( array ( 'orderby' => 'date', 'order' => 'DESC', 'showposts' => '1' ) );
    if ( $query->have_posts() ) :
    ?>
        <?php $query->the_post(); ?>
        <li><?php the_title(); ?></li>
        <div class="entry-content">
        <?php the_content(); ?>
    	<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'scrappy' ), 'after' => '</div>' ) ); ?>
        </div><!-- .entry-content -->
        <span class="media-posted-on">
        <?php edit_post_link( __( 'Edit', 'scrappy' ), '<span class="media-edit-link">', '</span>' ); ?>
        </span>
    <?php
    endif;
    ?>
    Thread Starter TonyaOD

    (@tonyaod)

    Oh thank you very much that works fantastically ?? thank you again

    Thread Starter TonyaOD

    (@tonyaod)

    Tested & works great

    Awesome! You’re quite welcome.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘help with displaying most recent post on static page’ is closed to new replies.