1

I'm having trouble with my WordPress theme.

I'm getting the error below after uploading a featured image into my portfolio item(s):

Warning: Invalid argument supplied for foreach() in /home2/maryhtran/public_html/wp-content/themes/Motive/portfolio-list.php on line 150

This is the code that is causing the error found in portfolio-list.php:

// Get the terms( categories ) for the portfolio item
$terms = get_the_terms( $post_item->ID, 'portfolio_categories' );

foreach( $terms as $term ) {
// some code here
}

Could someone please help me understand why this is happening and how I can fix this error?

closed as off-topic by cjbj, gmazzap Jun 22 '17 at 16:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This problem requires knowledge of how the theme was built. So, you're better off asking it's author than a general purpose site like WPSE. – cjbj Jun 21 '17 at 7:52
1

It happens when the foreach() is fed with an invalid entry, because there is no term returned. To prevent this, wrap your loop inside a conditional:

// Get the terms( categories ) for the portfolio item
$terms = get_the_terms( $post_item->ID, 'portfolio_categories' );
if(is_array($terms) || is_object($terms)){
    foreach( $terms as $term ) {
    // some code here
    }
}

Now, the loop will only run only the post has some terms.

  • Two additional notes: 1. Please create a child theme if you do that. 2. As a quick work-around for this bug (which you should report to the theme author so he can fix it) make sure that each post has at least one category assigned. – kraftner Jun 22 '17 at 10:50
0

The error is in your theme. Contact the theme support people. That is the proper place to ask theme-related questions.

(But, the error looks like the get_the_terms() function is not returning an array, so the foreach won't work.)

That has to be fixed by the theme author.

0

It will be a little hard to predict but here are some options you could try:

  1. Try echoing out the post id to see it it is actually populating.
  2. Check if "portfolio_categories" is the exact name to your taxonomy. You can hover over the link in admin panel and look for "taxonomy=[some_name]" in the URL.

Check these things and see what you can find.

Not the answer you're looking for? Browse other questions tagged or ask your own question.