WordPress.org

Support

Support » How-To and Troubleshooting » invalid argument fror each()

invalid argument fror each()

  • Please see the link below:

    link

    In the widgets area on the right side, I am getting the error:

    Warning: Invalid argument supplied for foreach() in /home/content/04/9655804/html/wp-content/themes/parallelus-salutation/functions.php on line 271

    The code in these lines are as follows:

    function qa_widget_show_sub_cats( $cat_args, $instance ) {
    	global $post;
    	if ( is_qa_page() ) {
    		$cats = get_the_terms( $post->ID, 'question_category' );
    		foreach ( $cats as $category ) {
    			if ( $category->term_id ) {
    				$termchildren = get_term_children( $category->term_id, 'question_category' );
    				if ( $termchildren && !empty( $termchildren ) && !is_object( $termchildren )  ) {
    					$cat_args['child_of'] = $category->term_id;
    					break;
    				}
    			}
    		}
    	}
    	return $cat_args;
    }

    Could you please help me figure out where is the problem?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Michael

    @alchymyth

    Forum Moderator

    happens if there is no $cats found;

    edit the section to include a conditional check:

    $cats = get_the_terms( $post->ID, 'question_category' );
    		if( $cats ) foreach ( $cats as $category ) {

    Thanks a lot, this solved the issue. Your support is very much appreciated.

    Would you have any suggestions for learning PHp for beginners especially to work with WordPress.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘invalid argument fror each()’ is closed to new replies.