简体   繁体   English

如果在WordPress上为空,则隐藏自定义字段

[英]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. 现在,我在不适用的情况下输入N / A,但如果该字段为空,我想写一个条件语句来隐藏整个部分。

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. *更新:这是通过结合以下答案和WordPress自定义字段帖子上的后端选项解决的。

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 . 在回调函数show_coverage() ,您使用的是未定义的变量$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. 虽然可以包括global $post ,但最好使用API​​函数而不是全局变量。

$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. 然后$coverage将不会包含任何内容。 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. 否则,您将开始构建HTML标记并将其呈现给浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM