• Resolved lberelson

    (@lberelson)


    Need a conditional written if there are no posts in a category. Ref: https://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)
Viewing 1 replies (of 1 total)
  • The topic ‘Conditional needed when post array is empty’ is closed to new replies.