简体   繁体   中英

How to return a single result if all posts in loop match condition

this is some basic IF THEN statement stuff but I'm a coding newbie so I'm hoping someone can throw me a bone :-).

I have several posts that have a custom date field. I want to:

1) loop through all those posts and evaluate whether the date fields from everyone of the posts are in the past. If so, I want to display the special message .

2) If just one of the posts is in the past I want to keep evaluating until the end of all posts in the query are reached.

3) If even one post with a date that is in the future is found I have some more content to display from that post instead of the special message .

Currently, the special message is only showing up when the item in the loop has no post content at all. I was also able to get it to return the message after each post was evaluated but then I got a special message for each post evaluated, and I just want to get one message even at any point any one of the posts returns true.

My current code is this:

                        <?php

                        $parent = get_cat_name($category[0]->category_parent);
                        $cur_cat = $cur_cat_slug; 
                        $cur_date = current_time('timestamp',0);

                        echo $cur_cat_name;

                        ?>
                    </div>

                    <div class=dates>
                        <?php

                        $categoryvariable=$category; // assign the variable as current category
                        $query= 'cat=' . $cur_cat_id. '&posts_per_page=100&meta_key=date_value&orderby=meta_value&order=ASC&meta_compare=>=&meta_value=$cur_date'; // concatenate the query
                        query_posts($query); // run the query
                        if ( have_posts() ) : while ( have_posts() ) : the_post();
                        $date_value = get_post_meta($post->ID, 'date_value', true);
                        if ($date_value>=$cur_date)
                            {
                        ?> 
                            <a class=dates-link href="<?php the_permalink(); ?>">
                            <li><?php echo date("D, n/j/Y, g:ia", get_post_meta($post->ID, 'date_value', true)); ?> - <?php $key="course_endtime"; echo get_post_meta($post->ID, $key, true); ?>
                            </a>
                        <div class=info>
                            <table cellpadding=0 cellspacing=3 border=0>
                                <tr>
                                    <td valign=top><img src="<?php bloginfo('template_directory'); ?>/images/i.jpg"></td>
                                    <td>Click any date for more info about a course and to register online.</td>
                                </tr>
                            </table>
                        </div>
                        <?php
                            } 

                        endwhile; else:

                        ?>

                        <div class=course-content>Sorry, no courses are currently scheduled. See <a href="http://dynamictactical.org/courses/" style="color:#000000; text-decoration:underline;">Course Calendar</a> for all upcoming DTT courses.</div>
                        <? endif;
                        wp_reset_query();?>

Here's my little bone: Use a boolean flag to determine whether or not you need to display the special message, and display the message only after the loop. Do something like this:

 Set $specialmessage = true;

before the loop. Then, inside the loop, when you find a post more recent than the date you're testing against, set it to false and stick the_content in a holder. You can use break to exit the loop if you want.Then,

if($specialmessage)
     echo 'special message'
else
     echo the_content_placeholder.

Not a coding expert, but my best advice.

Throwing you a bone:
Keep the php and the output-html apart. That way, you can edit the results further before you output anything at all, and it will be a lot easier to change something later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM