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.

I am a beginner in wordpress template development and I am trying to create a custom index file. I want to display latest posts in segments:

the 2 first posts should be displayed as a list the third post will be displayed with its thumbnail in full width the next 4 posts will be displayed in 1/2 width boxes

The difficulty I am facing is that I have to copy and paste the code for every segment while changing a few things as the width of the span (I am using bootstrap) and a counter that resets itself If it reaches the limit of posts I have set.

Is there a professional way to do this? In php I would write the functions in another file and then call them from index.php, so the code would be well-written and clean.

share|improve this question
add comment

1 Answer 1

You can include other files in the way you're describing by using get_template_part(), see: http://codex.wordpress.org/Function_Reference/get_template_part. This is intended for sections of code that you intend to reuse elsewhere on the site.

However, if you're only using these layouts ("list", "full thumbnail", etc.) on the home page, then it's quite fine to put them all in one file (usually front-page.php). But instead of using a counter, you can use the offset option in the loop to style groups of posts differently. For example:

<?php query_posts('showposts=2'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <h3><?php the_title(); ?></h3>
     <div class="entry">
          <?php the_content('Read more »'); ?>
     </div>
     Posted on <?php the_time('F jS, Y') ?> in <?php the_category(', '); ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

<?php query_posts('showposts=2&offset=1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <h2><?php the_title(); ?></h2>
     Posted on <?php the_time('F jS, Y') ?> in <?php the_category(', '); ?>
<?php endwhile; endif; ?>

Notice the &offset=2. The first loop will grab the first and second posts, the second loop will grab the third and fourth posts.

Another method would be to use one loop, but use a counter to modify the class of the post. For example:

<?php query_posts(); ?>
<?php $counter = 0; ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
     <?php $counter++ ?>
     <?php if ($counter <= 2) $class = "list"
     else if ($counter == 3) $class = "thumbnail";
     else $class = "half"; ?>
     <div id="post-<?php the_ID(); ?>" <?php post_class($class); ?>>
          <h3><?php the_title(); ?></h3>
          <div class="entry">
               <?php the_content('Read more »'); ?>
          </div>
          Posted on <?php the_time('F jS, Y') ?> in <?php the_category(', '); ?>
     </div>
<?php endwhile; endif; ?>
share|improve this answer
    
Wouldn't offset=1 only skip the first post, so you'd be grabbing second and third, not third and fourth posts? –  dalbaeb Mar 5 '13 at 19:52
    
Yeah, I was just providing that as an example. You could use offset=2 to grab the third and fourth posts, then add another loop with showposts=4&offset=4 to grab the fifth, sixth, etc. –  bcorkins Mar 5 '13 at 20:05
    
the problem is that the code is too long and repeats itself in each section so the file becomes hardly readable. But I really like your hint about the offset... it's too complicated to work with counters in a long file with mixed php/html code –  RobDel Mar 5 '13 at 20:17
    
will the code with the offset=2 scan the whole list of posts every time and ignore the first 2 ? –  RobDel Mar 5 '13 at 20:38
    
If offset is the only thing you're working with and if you don't want numerous calls to the database I think in your case it would be best to use rewind_posts() –  dalbaeb Mar 5 '13 at 20:47
show 2 more comments

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.