Support » Themes and Templates » 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 class="nav">
             <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>

    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?


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


    Forum Moderator

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

    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>
    	<?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>
    				<?php endforeach; ?>

    [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.