WordPress.org

Support

Support » How-To and Troubleshooting » Is there a better way of combining this?

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

    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>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Michael

    @alchymyth

    Forum Moderator

    to avoid a duplicate post, you could add the ‘offset’ parameter to the second args; http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

    Thank you.

    I’ve now got…

    <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, 'offset' => 1 );
    $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>

    Which does work. Is there any way of making it all neater / combined into – or is the way I’ve written it ok?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is there a better way of combining this?’ is closed to new replies.