• Relatively new to PHP and having some difficulty with something. Any help would be much appreciated.

    I’m trying to pull in posts to a page and organize them based on custom fields. The list is a list of events that occur over a number of days, so I have them pulled in by order of Event Day (Day 1, Day 2 etc.). Each post also has an associated custom field of event_date. It’s working fine when I pull it in as follows:

    <div class="section">
            <?php query_posts('category_name=event&meta_key=event_day&orderby=meta_value&order=ASC'); while (have_posts()) : the_post(); ?>
    	        <?php  if((get_post_meta($post->ID, "event_date", true))) { ?>
    	        <span class="event_date"> <?php echo get_post_meta($post->ID, "event_date", true); ?></span>
    	        <?php } ?>
    	        <div>
    		         <h2><a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_title();?></a></h2>
    		          <div ><?php the_content(); ?></div>
    		</div>
    	<?php endwhile; ?>
    </div>

    However, this means the date is showing up for every post. Now, the tricky part is, I only want the event_date to show up once for each Event Day — at the top of each “Event Day” section.

    My thought is to somehow associate/display the Event Date with only the first post in each Event Day. But I’m really not sure about the syntax for how to pull in a custom meta field ONLY for the first post that has another custom meta field, i.e. pull in “Event Date” only for the first post that has “Event Day == Day 1”, etc.

    I could pull the event_date in for all posts and then set the div class to display:none in the CSS for all posts except the first, but the tricky part again is to set this so that it is display:block on the first post of ‘Day 1’ and the first post of ‘Day 2’ etc.

    Of course the simplest thing to do would be to set the event_date only for the first post in each event day, but there are reasons this is impractical (every post needs to have an associated event_date elsewhere on the site, even if I don’t want it to show up here).

    Any ideas how to do this?

Viewing 1 replies (of 1 total)
  • Give the following a try. It gets the current date into $this_date and compares to the previous date. If not equal, it sets the previous date and echoes the date.

    <div class="section">
            <?php query_posts('category_name=event&meta_key=event_day&orderby=meta_value&order=ASC'); while (have_posts()) : the_post(); ?>
    	        <?php  $this_date = get_post_meta($post->ID, "event_date", true);
    	        if ($this_date != $prev_date) {
    	           $prev_date = $this_date; ?>
       	        <span class="event_date"> <?php echo $this_date; ?></span>
    	        <?php } ?>
    	        <div>
    		         <h2><a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_title();?></a></h2>
    		          <div ><?php the_content(); ?></div>
    		</div>
    	<?php endwhile; ?>
    </div>
Viewing 1 replies (of 1 total)
  • The topic ‘showing/hiding meta_values based on other custom fields’ is closed to new replies.