views:

112

answers:

3

The code below shows a Wordpress loop. How can I modify this loop to show only one post on the page?

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="fullbox" id="post-<?php the_ID(); ?>">
<h3><?php the_category(', ') ?></h3>
<div class="fullbox_content">
<h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
A: 
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php static $count = 0;
if ($count == "n") { break; }
else { ?>

<div class="fullbox" id="post-<?php the_ID(); ?>">
<h3><?php the_category(', ') ?></h3>
<div class="fullbox_content">
<h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>

<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>

So in your case, "n" would be 1.

Found here: http://perishablepress.com/press/2007/08/06/super-loop-exclude-specific-categories-and-display-any-number-of-posts/

Vian Esterhuizen
Awesome, thanks. I get the error Parse error: syntax error, unexpected T_ENDWHILE
drew
A: 

Thanks Hatdrawn. You pointed me in the right direction and I was able to modify the code using the reference you gave me. Final edit to the loop was `

<?php static $count = 0;
if ($count == "1") { break; }
else { ?>

  <div class="fullbox" id="post-<?php the_ID(); ?>">
  <h3>
  <?php the_category(', ') ?>
  </h3>
  <div class="fullbox_content">
  <h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">
  <?php the_title(); ?>
  </a></h1>

and ended with the addition of

<?php $count++; } ?>
<?php endwhile; ?>

I now have only one post showing on the home page. SWEET!!!

drew
A: 

Another way to do this by using a plugin would be to get a plugin such as Recent Post by Nick Momrick. It allows you to use a custom template tag to accomplish the same thing. I use a modified version of this plugin on my site. Nick has many useful plugins along the same line at http://nickmomrik.com/code/.

Hope this helps.

BandonRandon