$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How to make this code valid only when the value exists?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to make this code valid only when the value exists?

matteradmin9PV0评论

very new here to php and wordpress coding (1 day lol). This is my code so far, the problem is that when there is no value assigned to this attribute I receive and error message. I know I need something that says if the value exists then display the rest of the code but not sure how to go about adding that. This is for a woocommerce product attribute btw.

Code so far:

$collection_values = get_the_terms( $product->id, 'pa_collection');

    foreach ($collection_values as $collection_value){
        echo '<h2 class="collection_title"><a href="'.get_term_link($collection_value->slug, $collection_value->taxonomy).'">'.$collection_value->name.'</a></h2>';
    }

Error message:

Warning: Invalid argument supplied for foreach() in /app/public/wp-content/themes/savoy/woocommerce/single-product/title.php on line 21

EDIT: I am adding this above the product title in the title.php page.

very new here to php and wordpress coding (1 day lol). This is my code so far, the problem is that when there is no value assigned to this attribute I receive and error message. I know I need something that says if the value exists then display the rest of the code but not sure how to go about adding that. This is for a woocommerce product attribute btw.

Code so far:

$collection_values = get_the_terms( $product->id, 'pa_collection');

    foreach ($collection_values as $collection_value){
        echo '<h2 class="collection_title"><a href="'.get_term_link($collection_value->slug, $collection_value->taxonomy).'">'.$collection_value->name.'</a></h2>';
    }

Error message:

Warning: Invalid argument supplied for foreach() in /app/public/wp-content/themes/savoy/woocommerce/single-product/title.php on line 21

EDIT: I am adding this above the product title in the title.php page.

Share Improve this question edited Dec 8, 2018 at 18:46 panther888 asked Dec 8, 2018 at 18:15 panther888panther888 11 bronze badge 2
  • See the example in the documentation. – Milo Commented Dec 8, 2018 at 18:19
  • Hey Milo, thanks for the tip. This worked in terms of showing the attribute value only when it exists but now my title is no longer showing (I'm adding this code above the product title). The code that calls the attribute archive link also no longer works. – panther888 Commented Dec 8, 2018 at 18:45
Add a comment  | 

2 Answers 2

Reset to default 0

If we refer to the documentation, we see that get_the_terms returns an array, false, or a WP_Error object. So in our code we'll add a check if $collection_values exists (it's not false), and it's not an error, then we'll know it has to be an array, and it's safe to run a foreach loop on it.

$collection_values = get_the_terms( $product->id, 'pa_collection');

if ( $collection_values && ! is_wp_error( $collection_values ) ){
    foreach ($collection_values as $collection_value){
        echo '<h2 class="collection_title"><a href="'.get_term_link($collection_value->slug, $collection_value->taxonomy).'">'.$collection_value->name.'</a></h2>';
    }
}

This code worked for me:

      $collection_values = get_the_terms( $product->id, 'pa_collection');
    if( !empty($collection_values) ) :
    foreach ($collection_values as $collection_value){
        echo '<h2 class="collection_title"><a href="'.get_term_link($collection_value->slug, $collection_value->taxonomy).'">'.$collection_value->name.'</a></h2>';
    }

endif;
Post a comment

comment list (0)

  1. No comments so far