WordPress.org

Ready to get started?Download WordPress

Forums

wp_query latest post (4 posts)

  1. ByronCooke
    Member
    Posted 1 year ago #

    I have been attempting to display my latest post on the home page of my site but I cant understand why my code wont work? can anyone help?

    Thanks, Byron

    <?php
            $latest_post = new WP_Query("showposts=1");
            if($latest_post->have_post()) :
        ?>
        <div class="boxBar">
            <?php
                while($latest_post->have_post()):
                    $latest_post->the_post();
            ?>
            <h1 class="boxedHeader">
                <?php the_title() ?>
            </h1>
            <?php endwhile ?>
        </div>
        <?php endif ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    what exactly happens with the 'wont work'?
    i.e. what is the result?

    see all parameters for WP_Query() http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

    possibly try:

    $latest_post = new WP_Query("post_type=post&posts_per_page=1&orderby=date&order=DESC");
  3. ByronCooke
    Member
    Posted 1 year ago #

    thanks for the reply, that still didnt get it working unfortunately

    the problem is the title isnt being displayed, neither is the div 'boxBar' and all content after, without the php all html elements display ok

  4. ByronCooke
    Member
    Posted 1 year ago #

    oh my god, I cant believe how simple the problem was, i missed the s off the end of have_posts()

    think its too late, time for bed haha

    thanks anyway for your time

Topic Closed

This topic has been closed to new replies.

About this Topic