0

Scenario: Page displays a list of titles for a custom post type (video) with a selected "category" (in the taxonomy its called the subject). For example videos with the subject of "creative"

Problem If there are titles present that match the the criteria then the list is displayed correctly but if there are no titles then the message "No Titles Found" fails to appear on the page. In other words the echo statement is failing.

My Code

<?php $subject = $_GET['subject'];

if ($subject=="") $subject = 'creativity';?><?php /*If there is no value returned then the loop defaults to searching for items beginning with 'Creativity' since its the first term.*/ ?>
<h2>Video Titles For <span class="colorBlack"><?php echo $subject ?></span></h2> <?php     /*This needs to be outside of the loop*/?>

<?php
$loop = new WP_Query( array( 'post_type' => 'video', 'subject' => $subject,     'posts_per_page' => 10 ) ); ?>
<?php if (have_posts() ):?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
                            <div class="glossarybody">
                                <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
                             </div>
            <?php endwhile;
      else: echo 'No Titles Found';
endif;
 ?>

Looking at the codex the form of the 'if else is given as:

<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
    // Your loop code
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
?>

So I seem to have the correct syntax and since the 'if' section functions correctly I assume it has correct syntax.

I'd appreciate any pointers as to where I'm going wrong.

Thanks

2 Answers 2

3

Try $loop->have_posts() on if statement. See if it fixes the issue.

4
  • @dorich Mark it as answered.
    – fuxia
    Aug 19, 2011 at 17:50
  • @toscho: Thanks. However, I need your guidance. Is there a particular method of marking a question as answered? I looked around for a button, similar to the jquery forum, that marks a question as answered but didn't see one. Previously in Stackoverflow I was chastized for adding "Answered" to a subject. What is the appropriate protocol on this board?
    – dorich
    Aug 19, 2011 at 20:57
  • @dorich: there is a "Tick" mark under the voting option on every answer. Just click this and the answer will select as right answer ;)
    – Sisir
    Aug 19, 2011 at 20:59
  • Good grief that was simple. Helped me out :)
    – joshmax
    May 10, 2013 at 14:34
0

Why use WP_Query? I think you can just use get_posts.
I've not tested using args that is not in the list, but it should work.

This is what I would do.

$args=array(
  'subject' => $subject,
  'post_type' => 'video',
  'post_status' => 'publish',
  'posts_per_page' => 10
);

$videos = get_posts( $args );

$has_video = true;

// Loop through the posts
foreach ($videos as $video) :  setup_postdata($video); ?> 

  // We assume  that all posts have a title.
  if(!empty(get_the_title()) {
    <div class="glossarybody">
      <?php the_title(); ?>   
      <?php the_excerpt(); ?>
    </div>
  } else {
    $has_video = false;   
  }
endforeach; 

if(!$has_video)
  _e('Sorry, no posts were found');
1
  • Thanks for your detailed response. I'll try this out but in the meantime wanted to acknowledge your help.
    – dorich
    Aug 19, 2011 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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