Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is what I have.

<?php
$week = date('W');
$year = date('Y');
$projects_in_news = new WP_Query(
                array(
                'post_type' =>'news',
                'posts_per_page' => 5,
                'orderby' =>'ID',
                'order' =>'ASC',
                'w' => $week,
                'y' => $year,
                'tax_query' => array(
                        array(
                             'taxonomy' => 'project_taxo',
                             'terms' => $post->post_name,
                             'field' => 'slug'
                             )
                       )
                )
);

// The Loop
<?php if($projects_in_news->have_posts()) : ?>

        <?php while($projects_in_news->have_posts()) : $projects_in_news->the_post(); ?>
                <ul class="side-list">     
                    <li>
                         <a href="<?php the_permalink(); ?>">
                            <strong class="title"><?php the_title(); ?></strong>
                            <em class="date"><span><?php the_author(); ?>,</span> <?php the_time('d F Y'); ?></em>
                          </a>
                    </li>
                </ul>
        <?php endwhile; ?>

        <?php else: ?>
            <p>There is no news related to this theme</p>

        <?php endif; ?>
            <?php wp_reset_postdata(); ?>

What I have here displays posts from the last week that match the query.

Is there a way of determining if this loops query has posts and if not use a different query and loop?

I tried adding the following code in place of <p>There is no news related to this theme</p>

                                  <?php 
                                            $theme_in_news = new WP_Query(
                                                    array(
                                                        'post_type' =>'theme',
                                                        'posts_per_page' => 5,
                                                        'orderby' =>'ID',
                                                        'order' =>'ASC',
                                                    )
                                            );
                                    ?>

                                    <?php if($theme_in_news->have_posts()) : ?>

                                    <?php while($theme_in_news->have_posts()) : $theme_in_news->the_post(); ?>
                                    <ul class="side-list">     
                                        <li>
                                            <a href="<?php the_permalink(); ?>">
                                            <strong class="title"><?php the_title(); ?></strong>
                                            <em class="date"><span><?php the_author(); ?>,</span> <?php the_time('d F Y'); ?></em>
                                            </a>
                                        </li>
                                    </ul>
                                    <?php endwhile; ?>

But it returns an Internal Server Error.

Thanks

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The code you have provided already has a check to see if posts exist for said query. So all you need to do is expand on it.

...
    <?php else: ?>
        <p>There is no news related to this theme</p>

    <?php endif; ?>
...

Simply place you next query posts in the above code replacing <p>There is no news related to this theme</p>

share|improve this answer
    
Hey @Brady. Forgot to mention that I tried that. I placed a new WP Query followed by a loop in place of <p>There is...</p>. The page doesn't load when I do that. It returns an Internal Server Error. Any ideas? Maybe an example will clear things up. –  Duane Jun 22 '11 at 15:56
    
@Duane please post the code you tried. –  Brady Jun 22 '11 at 16:01
    
I posted the code above in original question. –  Duane Jun 22 '11 at 16:18
    
in the second lot of code you have posted you have missed off <?php endif; ?>. So you should have two of them one after the other. –  Brady Jun 22 '11 at 16:22
    
Stupid me. Thanks a million. I can't believe I spent the last two hours trying to figure this out and all I had to do was add another <?php endif; ?> Thanks again @Brady –  Duane Jun 22 '11 at 16:30
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.