• Resolved jasonsweb

    (@jasonsweb)


    Hi,

    On my portfolio page I would like to give my featured post (one post) a different style than the other posts.

    At the moment I’m using the following code:

    <div id="portfolio">
    
     <?php
      // Get all portfolio projects from custom post type
      $args = array(
       'post_type' => 'portfolio',
       'posts_per_page' => -1
      );
    
      $query = new WP_Query($args);
      $countposts = 0;
    
      while ($query->have_posts()) : $query->the_post();
    
      $countposts++;
      if ($countposts == 1) {
     ?>
    
     <!-- Begin Featured Project -->
     <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <div class="entry-featured">
         Here comes the featured project
      </div>
     </div>
     <!-- End Featured Project -->
    
     <?php } else { // Normal projects ?>
    
     <!-- Begin Portfolio Project -->
     <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
      <div class="entry-normal">
        Here comes the normal project
      </div>
     </div>
     <!-- End Portfolio Project -->
    
     <?php } endwhile; wp_reset_postdata(); ?>
    
    </div>

    Now I would like to add a simple heading (like “Other projects”) between the featured project and the list of normal projects.

    Like here:
    <!– End Featured Project –>

    <h3>Other projects</h3>

    <!– Begin Portfolio Project –>

    To achieve this I have to stop the first loop, then add the heading and start the loop again.

    I’ve tried to use rewind_posts, but then the loop is also displaying the featured post as a normal post.

    Is there someone who can help me with this? Thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • edit this section (shown after the changes):

    while ($query->have_posts()) : $query->the_post();
    
      if ($countposts == 1) { ?>
    <h3>Other projects</h3>
      <?php } ?>
    
      $countposts++;
      if ($countposts == 1) {
    Thread Starter jasonsweb

    (@jasonsweb)

    Sorry, I’m not sure if I understand what you mean. Do I have to paste this code below $countposts = 0; ?

    replace this section:

    while ($query->have_posts()) : $query->the_post();
    
      $countposts++;
      if ($countposts == 1) {
     ?>

    with the suggested section.

    Thread Starter jasonsweb

    (@jasonsweb)

    Thanks! Works like a charm!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Featured post’ is closed to new replies.