Title: Conditional needed when post array is empty
Last modified: August 21, 2016

---

# Conditional needed when post array is empty

 *  Resolved [lberelson](https://wordpress.org/support/users/lberelson/)
 * (@lberelson)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/conditional-needed-when-postype-is-empty/)
 * Need a conditional written if there are no posts in a category. Ref: [http://howlingwolfmedia.com/site3/classes/class-categories/](http://howlingwolfmedia.com/site3/classes/class-categories/)
   ‘
   Personalized Training’ has no posts so want to echo ‘currently no classes offered
   in this category’ Thx
 * Tried is_empty or if else statements but having trouble implementing and syntax
   problems.
 *     ```
       <div id="accordion" style="width:95%;">
       <?php
           $args=array(
             'child_of' => 4,
             'orderby' => 'name',
             'order' => 'ASC',
             'hide_empty' => '0'
             );
           $categories=get_categories($args);
             foreach($categories as $category) {
               echo '<h3>' . $category->name . '</h3>
       		<div>';
       			global $post;
       			$args = array( 'posts_per_page' => -1, 'category' => $category->term_id, 'orderby' => 'name', 'order' => 'ASC' );
       			//alternatively this also works:  'nopaging' => true
       			$myposts = get_posts( $args );
       			foreach( $myposts as $post ) :  setup_postdata($post);
       			echo  '<p><a href="' . get_permalink($post->ID) . ' " style="color:#7c7c7c; font-size: 1em; text-decoration:none !important; line-height:80%;">' . get_the_title($post->ID) . '</a></p>';
       			endforeach;
       		echo  '</div>
              ';
           }
       ?>
       </div><!-- accordion -->
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [lberelson](https://wordpress.org/support/users/lberelson/)
 * (@lberelson)
 * [12 years, 8 months ago](https://wordpress.org/support/topic/conditional-needed-when-postype-is-empty/#post-4125911)
 * found solution [http://stackoverflow.com/questions/13332128/if-category-empty-return-a-message](http://stackoverflow.com/questions/13332128/if-category-empty-return-a-message)
 *     ```
       if (!$myposts) {
         echo 'Sorry there are currently no classes in this category.';
         }
       else {
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Conditional needed when post array is empty’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [category](https://wordpress.org/support/topic-tag/category/)
 * [foreach](https://wordpress.org/support/topic-tag/foreach/)
 * [setup_postdata](https://wordpress.org/support/topic-tag/setup_postdata/)

 * 1 reply
 * 1 participant
 * Last reply from: [lberelson](https://wordpress.org/support/users/lberelson/)
 * Last activity: [12 years, 8 months ago](https://wordpress.org/support/topic/conditional-needed-when-postype-is-empty/#post-4125911)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
