WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditional needed when post array is empty (2 posts)

  1. lberelson
    Member
    Posted 10 months ago #

    Need a conditional written if there are no posts in a category. Ref: 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 -->
  2. lberelson
    Member
    Posted 10 months ago #

    found solution http://stackoverflow.com/questions/13332128/if-category-empty-return-a-message

    if (!$myposts) {
      echo 'Sorry there are currently no classes in this category.';
      }
    else {

Reply

You must log in to post.

About this Topic