Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hoping someone can point me in the direction of writing this in a more succinct way.

I'm working on my website, and want to display a total of 5 posts from the category 'general wonderings'.

The first post has different styling from the other 4. Please see code below,

Now, 2 problems... they way I've written it means that there is a duplicate post and secondly - surely there's a better way of combining this into one, rather than the way I've written it?

Any help would be greatly appreciated.

Thank you

    <div class="columns">
      <?php
$args = array( 'category_name' => 'General Wonderings', 'posts_per_page' => 1 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
      <article>
        <h3><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a></h3>
        <h4>
          <?php the_date(); ?>
        </h4>
        <p>
          <?php the_excerpt(); ?>
        </p>
        <p><a href="<?php the_permalink(); ?>">Read more</a></p>
      </article>
      <?php endforeach; ?>
    </div>
    <div class="columns">
      <?php
$args = array( 'category_name' => 'General Wonderings', 'posts_per_page' => 4 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
      <article>
        <h4><a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          <br/>
          <span><?php echo substr($post->post_excerpt, 0,30); ?>...<br/>
          <?php the_date(); ?>
          </span></a></h4>
      </article>
      <?php endforeach; ?>
    </div>
share|improve this question

2 Answers 2

Use offset=1 to exclude first post.

$args = array( 'category_name' => 'General Wonderings', 'posts_per_page' => 4, 'offset' => 1 );
share|improve this answer
    
Thank you! Thats'a great help. Regarding the way I've written the rest of the PHP - is that ok? It feels a bit clumsy to me - I'm not great at PHP –  V Neal Jan 18 '13 at 15:59
    
Yeah, rest of the PHP looks good. –  Prasanna SP Jan 18 '13 at 16:12

I would worry more about performance than succinct code-- shortened it up a little though. Right now you are running two queries. That is not necessary, and that is the biggest issue with this code in my opinion.

$args = array( 'category_name' => 'General Wonderings', 'posts_per_page' => 5 );
$lastposts = get_posts( $args );
$first = true; ?>
<div class="columns"><?php
foreach($lastposts as $post) {
  setup_postdata($post); ?>
    <article><?php
    if ($first) { ?>
      <h3><a href="<?php the_permalink(); ?>">
       <?php the_title(); ?>
      </a></h3>
      <h4>
       <?php the_date(); ?>
      </h4>
      <p>
       <?php the_excerpt(); ?>
      </p>
      <p><a href="<?php the_permalink(); ?>">Read more</a></p>
      </div><div class="columns"><?php // close the first div and open the second
      $first = false;
    } else { ?>
      <h4><a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
        <br/>
        <span><?php echo substr($post->post_excerpt, 0,30); ?>...<br/>
          <?php the_date(); ?>
        </span></a>
      </h4><?php
    } ?>
    </article><?php
} ?>
</div>

Hopefully I preserved the formatting and don't have any syntax errors :)

share|improve this answer
    
Thank you! I think I see what you've done here. It seems to be working, the only issue now that for some reason, there is an empty <div class="column"></div> being rendered. Not sure whether this is related to the updated code or not... –  V Neal Jan 18 '13 at 16:33
    
I think it was an issue with the excerpt - all seems ok now. Thank you! –  V Neal Jan 18 '13 at 16:49
    
Actually - the code above is generating a new <div class="column"> for every new post. Sorry my fault, I should have been clearer. The first article (main one) is to appear in its own column, but then the remaining 4 need to be in the same column. –  V Neal Jan 21 '13 at 12:44
    
So shuffle the <div around. It is pretty trivial change. –  s_ha_dum Jan 21 '13 at 14:58
1  
Edited the answer –  s_ha_dum Jan 21 '13 at 15:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.