is there a better way of combining this?

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’.

Read More

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>

Related posts

Leave a Reply

2 comments

  1. 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 🙂