WordPress.org

Ready to get started?Download WordPress

Forums

Complex code for most recent posts (3 posts)

  1. michaelphill
    Member
    Posted 1 year ago #

    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!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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
    particular the section about multiple loops.

  3. michaelphill
    Member
    Posted 1 year ago #

    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]

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.