WordPress.org

Ready to get started?Download WordPress

Forums

invalid argument fror each() (3 posts)

  1. nalakath
    Member
    Posted 1 year ago #

    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?

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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 ) {
  3. nalakath
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic