• Hi I’m looking to list my custom posts like so

    2013
    custom post
    custom post
    custom post
    custom post

    2012
    custom post
    custom post
    custom post
    custom post

    2011
    custom post
    custom post
    custom post
    custom post

    I would like it to be dynamic, like if the user creates a new post in 2014, a new header, 2014, would be created and the post displayed under it.

    I have very little programming knowledge but let me just paste what I have for my page (listed by taxonomy)

    <div id="main" class="group">
      <h2><?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?></h2>
          <div id="gallerywrap" class="group">
    
            <?php global $query_string; query_posts($query_string . '&posts_per_page=-1');?>
            <?php if (have_posts()) :  while (have_posts()) : the_post(); ?>
            	<?php
    				$custom = get_post_custom($post->ID);
            		$image_html= get_the_post_thumbnail($post->ID, 'medium');
    			?>
    
              <div class="group">
    		<div class="info">
                <h3><?php echo the_title(); ?></h3>
                <p><?php the_content(); ?></p>
    
              </div>
              <?php print $image_html; ?>
    
            </div>
                 <?php endwhile; else: ?>
    			<p><?php _e('No posts were found. Sorry!'); ?></p>
    		<?php endif; ?>  
    
        </div>
        </div>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter monkeybroth

    (@monkeybroth)

    OK i referenced code from here

    Now the years show up nicely. But I have another problem.
    I have 3 posts. dated in their years 2014, 2013, 2012, for testing.
    the years show up fine on front-end but ALL 3 show this

    2014
    post(2013)
    post(2012)

    2013
    post(2013)
    post(2012)

    2012
    post(2013)
    post(2012)

    2014 posts won’t show likely because today’s date isn’t past that.
    for 2013 and 2012, they show all posts.

    How do edit my code such that they show the corresponding posts in their year??

    PLEASE HELP!

    <div id="gallerylist">
    
    	  <?php
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC");
    foreach($years as $year) : ?>
    
    <ul id="display"><?php echo '<span>',$year,'</span>' ?>
            <?php global $query_string; query_posts($query_string . '&posts_per_page=-1');?>
            <?php if (have_posts()) :  while (have_posts()) : the_post(); ?>
            	<?php
    				$custom = get_post_custom($post->ID);
            		$image_html= get_the_post_thumbnail($post->ID, 'thumbnail');
    			?>
            <li>
                <?php print $image_html; ?>
                <h3><?php echo the_title(); ?></h3>
                <p><?php echo get_post_meta($post->ID, 'caption', true); ?></p>
            </li>
                 <?php endwhile; else: ?>
    			<p><?php _e('No posts were found. Sorry!'); ?></p>
    		<?php endif; ?>
            </ul>
    		<?php endforeach; ?>
    
            </div>

    For posterity, here is what I’ve done to get this working:

    <?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts ORDER BY post_date DESC"); ?>
    <?php foreach($years as $year) : ?>
        <h2><?php echo $year; ?></h2>
    
        <ul>
          <?php query_posts('year='.$year); ?>
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
          <?php endwhile; endif; ?>
        </ul>
    <?php endforeach; ?>

    And you can do a similar thing for categories:

    <?php $categories = get_categories(); ?>
    <?php foreach($categories as $cat) : ?>
      <h2><?php echo $cat->cat_name ?></h2>
    
      <?php query_posts('category_name='.$cat->slug); ?>
    
    <ul>
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <li><a>"><?php the_title(); ?></a> </li>
        <?php endwhile; endif; ?>
      </ul>
    <?php endforeach; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Listing custom posts by year with the year as its heading’ is closed to new replies.