WordPress.org

Ready to get started?Download WordPress

Forums

Is there a better way of combining this? (3 posts)

  1. vikkineal
    Member
    Posted 1 year ago #

    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>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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

  3. vikkineal
    Member
    Posted 1 year ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags