• anonymized-3854925

    (@anonymized-3854925)


    After a couple of months of not touching my new portfolio website design I’ve finally got around to continuing it.

    I keep struggling getting WordPress to do the simplest of tasks though! Here I’m simply trying to get it to display my most recent post on the homepage… it was working originally but I must have altered something without realising and now it’s just displaying all of them!

    Here is the code I have in my index.php (it’s using example code from the Codex)

    PHP Code:
    `<?php $my_query = new WP_Query(‘posts_per_page=1’);
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    <div id=”h1-wrapper”><h1><?php the_title(); ?></h1></div>
    <div class=”detail”>
    <span class=”info”>Filed under <?php the_category(‘, ‘, ‘multiple’); ?> | <?php the_date(); ?></span>
    <span class=”listening-to”>Listening to: <span class=”highlight”>Orbital</span></span>
    </div>
    <div class=”content”>
    <?php the_content(); ?>
    </div>
    <?php endwhile; ?>

    <?php if (have_posts()) : while (have_posts()) : the_post();
    if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    <div id=”h1-wrapper”><h1><?php the_title(); ?></h1></div>
    <div class=”detail”>
    <span class=”info”>Filed under <?php the_category(‘, ‘, ‘multiple’); ?> | <?php the_date(); ?></span>
    <span class=”listening-to”>Listening to: <span class=”highlight”>Orbital</span></span>
    </div>
    <div class=”content”>
    <?php the_content(); ?>
    </div>
    <?php endwhile; endif; ?>`

    Can anybody see the problem?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Try this:

    <?php $recentpost = new WP_Query("showposts=1"); while($recentpost->have_posts()) : $recentpost->the_post(); ?>
    // do stuff in here like display the content and title etc.
    <?php endwhile; ?>
    Thread Starter anonymized-3854925

    (@anonymized-3854925)

    Thanks.

    I managed to get it working in the end using this

    query_posts('posts_per_page=1');
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    	entry();
    endwhile;
    endif;

    Far simpler than my original method (not sure why I used that!)

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Display most recent post only’ is closed to new replies.