Title: Complex code for most recent posts
Last modified: August 20, 2016

---

# Complex code for most recent posts

 *  [michaelphill](https://wordpress.org/support/users/michaelphill/)
 * (@michaelphill)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/complex-code-for-most-recent-posts/)
 * Trying to do something I’ve never done before.
 * In the header of my theme, I want to show some content, including the title and
   some custom fields, of the most recent post and then a
    `<ul>` of the six most
   recent posts with an offset so the most recent isn’t included in that list.
 * So, structurally, it should be something like this:
 *     ```
       <div class="header">
          <h1>Most Recent Post Title</h1>
          <p>Custom field from most recent post</p>
       </div>
          <div class="nav">
             <ul>
                <li><a href="#">2nd most recent post title</a></li>
                <li><a href="#">3rd most recent post title</a></li>
                <li><a href="#">4th most recent post title</a></li>
                <li><a href="#">5th most recent post title</a></li>
                <li><a href="#">6th most recent post title</a></li>
                <li><a href="#">7th most recent post title</a></li>
             </ul>
       ```
   
 * The problem I’m having is I’m not sure how to combine these two requirements 
   into the correct WordPress code.
 * Can anyone lend a hand?
 * Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/complex-code-for-most-recent-posts/#post-3430446)
 * two possibilities:
    – a single loop with some conditional statements to select
   the right output html; – two loops; second loop excludes the first post.
 * are you planning to have paginated pages, like ‘next posts/prev posts’?
    and 
   how are they to be formatted?
 * general, review:
 * [http://codex.wordpress.org/The_Loop](http://codex.wordpress.org/The_Loop)
    particular
   the section about multiple loops.
 *  Thread Starter [michaelphill](https://wordpress.org/support/users/michaelphill/)
 * (@michaelphill)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/complex-code-for-most-recent-posts/#post-3430489)
 * Below is the code I ended up with. It’s somewhat working, but the big issue is
   that for some reason it’s repeating `<strong><div id="header"></strong>` and `
   <strong><div class="holder"></strong>` in addition to the
    `<li>` elements (that
   I want it to repeat six times). Any ideas why it’s repeating the header and holder
   elements?
 *     ```
       <?php query_posts('numberposts=1'); ?>
       	<?php while (have_posts()) : the_post(); ?>
       	<div id="header" style="background: #<?php echo get_post_meta($post->ID, "hex", true); ?>;">
       		<div class="holder" style="background: #<?php echo get_post_meta($post->ID, "hex", true); ?>;">
       			<div class="box">
       				<h1 class="logo"><a href="/">ColorEveryDay</a></h1>
       					<div class="text-top"><span class="date"><?php the_date('m.d'); ?></span><span>‘<?php the_title(); ?>’</span></div>
       			</div>
       		</div>
       	<?php endwhile; ?>
       			<div class="nav-holder">
       			<ul id="nav">
       				<?php $posts = get_posts('numberposts=6&offset=1'); foreach ($posts as $post): setup_postdata($post); ?>
       				<li class="nav-box">
       					<div style="background: #<?php echo get_post_meta($post->ID, "hex", true); ?>;"><a href="<?php the_permalink(); ?>"><?php the_date('m.d'); ?><span>‘<?php the_title(); ?>’</span></a></div>
       				</li>
       				<?php endforeach; ?>
       			</ul>
       		</div>
       	</div>
       ```
   
 * _[Moderator note: Wrap **all** code using backticks]_

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Complex code for most recent posts’ is closed to new replies.

 * 2 replies
 * 2 participants
 * Last reply from: [michaelphill](https://wordpress.org/support/users/michaelphill/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/complex-code-for-most-recent-posts/#post-3430489)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
