0

the code I am using below will show the last 5 post in whatever category I choose, but I was wondering, How can I get it so that the first post shows different from the other 4?

I want the first post to show the thumbnail and the title under it and the rest to show titles. I found a code on a recent post on here but it's closed, so I couldn't comment and ask questions.

Here is the code I'm using

<?php
$queryObject = new  Wp_Query( array(
    'showposts' => 5,
    'post_type' => array('post'),
    'category_name' => videos,
        'orderby' => 1,
    ));

// The Loop
if ( $queryObject->have_posts() ) :
  $i = 0;
  while ( $queryObject->have_posts() ) :
      $queryObject->the_post();
      ?>
      <?php if ( $i == 0 ) : ?>
          <div class="first-post">
      <?php endif; ?>
      <a href="<?php the_permalink(); ?>" title="<?php printf(__( 'Read %s', 'wpbx' ), wp_specialchars(get_the_title(), 1)) ?>">
          <?php the_post_thumbnail('sidethumbs'); ?>
      </a>

      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      <?php if ( $i == 0 ) : ?>
          </div>
      <?php endif; ?>    
      <?php $i++; ?>
  <?php endwhile; ?>

It's working but the problem is, I don't know where to begin the second styling or how to begin it which the person failed to explain, can anyone help me.

2 Answers 2

2

The code you posted will only make difference in adding <div class="first-post"></div> for the first post, so it still displays same information for all posts.

Try this instead:

<?php
$queryObject = new  Wp_Query( array(
    'showposts' => 5,
    'post_type' => array('post'),
    'category_name' => videos,
    'orderby' => 1,
    ));

// The Loop
if ( $queryObject->have_posts() ) :
    $i = 0;
    while ( $queryObject->have_posts() ) :
        $queryObject->the_post();
        if ( $i == 0 ) : ?>
            <div class="first-post">
            <a href="<?php the_permalink(); ?>" title="<?php printf(__( 'Read %s', 'wpbx' ), wp_specialchars(get_the_title(), 1)) ?>">
                <?php the_post_thumbnail('sidethumbs'); ?>
            </a>
        <?php else: ?>
            <div class="secondary-post">
        <?php endif; ?>
        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
            <?php the_title(); ?>
        </a>
        </div>
        <?php $i++;
    endwhile;
endif;
3
  • Omg Thank you so much, that works perfectly lol @Elisha Terada
    – user32447
    Commented May 29, 2013 at 4:56
  • Also how do i make the title for the first post bold? @Elisha Terada
    – user32447
    Commented May 29, 2013 at 5:03
  • Pat J answered that part of question. Try what he suggested or more specifically, div.first-post a { font-weight: bold; } in your CSS. Commented May 29, 2013 at 5:07
0

The code you show wraps your first post in a <div class='first-post'>...</div> element. You can add styles to it in your theme's CSS file(s) (either style.css or a custom one if you're loading custom styles):

div.first-post {
    /* First Post styles here */
    /* for example, if you want the text to be **bold** */
    font-weight: bold;
}
1
  • I don't get it, please is there a better way to do this, cause it seems as if you are telling me that i won't be able to style the second set of post @Pat J
    – user32447
    Commented May 29, 2013 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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