Taylor Cox
Taylor Cox

Reputation: 5

Hide Custom Field if Empty on WordPress

I've written two functions to show custom fields on the shop pages below each product, which can be viewed here. The ones specifically are Light Bulb Type and Coverage Area. However, these only apply to certain products. Right now, I am typing in N/A when they don't apply, but I would like to write a conditional statement to hide the entire section if the field is left empty.

Unfortunately, every time I do so, the code breaks the site because it is incorrectly written.

These are the functions I've written to show the custom fields.

add_action('woocommerce_shop_loop_item_title','show_coverage');

function show_coverage() {
    echo '<span class="extra-info-head">Coverage Area:</span> ';
    $coverage =  get_post_meta( $post->ID );
    $custom_fields = get_post_custom();
    $coverage = $custom_fields['wpcf-coverage-area'];
    foreach ( $coverage as $key => $value ) {
    echo $value . '<br/>';
}}

add_action('woocommerce_shop_loop_item_title','show_bulbs');

function show_bulbs() {
    echo '<span class="extra-info-head">Light Bulbs:</span> ';
    $custom_fields = get_post_custom();
    $bulbs = $custom_fields['wpcf-light-bulbs'];
    foreach ( $bulbs as $key => $value ) {
    echo $value;
}}

Apologies if these are poorly written and much appreciation to any suggestions and help!

*UPDATE: This was solved with a combination of the answer below and back-end options on WordPress's custom field posts.

Upvotes: 0

Views: 1347

Answers (1)

hellofromTonya
hellofromTonya

Reputation: 1359

Unfortunately, every time I do so, the code breaks the site because it is incorrectly written.

Your site is erroring out because you have an "undeclared variable."

In the callback function show_coverage(), you are using an undefined variable $post. The function doesn't know about this variable, because you haven't brought it into the function' scope.

While you could include the global $post, it's better to use the API function instead of the global variable.

$coverage = get_post_meta( get_the_ID(), 'wpcf-coverage-area' );

Let's go one step further. What if the custom metadata doesn't exist in the database? Then $coverage is not going to have anything in it. Let's protect for that by checking and then bailing out if nothing is returned.

function show_coverage() {
    $coverage = get_post_meta( get_the_ID(), 'wpcf-coverage-area' );
    if ( ! $coverage ) {
        return;
    }

    echo '<span class="extra-info-head">Coverage Area:</span>';

    foreach ( $coverage as $key => $value ) {
        echo $value . '<br/>';
    }
}

Now, let's do the same treatment to the other callback function:

function show_bulbs() {
    $bulbs = get_post_meta( get_the_ID(), 'wpcf-light-bulbs' );
    if ( ! $bulbs ) {
        return;
    }

    echo '<span class="extra-info-head">Light Bulbs:</span> ';
    foreach ( $bulbs as $key => $value ) {
        echo $value;
    }
}

Now the above functions will fetch the custom field (which is metadata). If it does not exist, you bail out. Otherwise, you start building the HTML markup and rendering it out to the browser.

Upvotes: 1

Related Questions