简体   繁体   中英

checking if value is empty

I'm trying to patch up some code that outputs a date range in our wp functions file. Here is the code that was built:

function date_flip() {
    global $custom_metabox; //needed    
    $start_date = $custom_metabox->get_the_value('start_date');
    $end_date   = $custom_metabox->get_the_value('end_date');
    //start day
    $s_day   = substr($start_date,8,2);
    $s_month = substr($start_date,5,2);
    $s_year  = substr($start_date,0,4);
    //end day
    $e_day   = substr($end_date,8,2);
    $e_month = substr($end_date,5,2);
    $e_year  = substr($end_date,0,4);
    //mash up
    $start_date = $s_day . "." . $s_month . "." . $s_year;
    $end_date   = $e_day . "." . $e_month . "." . $e_year;
    //spew out
    echo $start_date . ' – ' . $end_date;
}

The issue I have is that, sometimes we don't put in an end_date in our posts, so this code outputs a blank set of numbers… for example 9.9.2015–..

I was trying to narrow it down by not implementing the $end_date using a check if empty value but it ends up just hanging up my site.

I replaced the last echo with this:

function date_flip() {
    global $custom_metabox; //needed    
    $start_date = $custom_metabox->get_the_value('start_date');
    $end_date   = $custom_metabox->get_the_value('end_date');
    //start day
    $s_day   = substr($start_date,8,2);
    $s_month = substr($start_date,5,2);
    $s_year  = substr($start_date,0,4);
    //end day
    $e_day   = substr($end_date,8,2);
    $e_month = substr($end_date,5,2);
    $e_year  = substr($end_date,0,4);
    //mash up
    $start_date = $s_day . "." . $s_month . "." . $s_year;
    $end_date   = $e_day . "." . $e_month . "." . $e_year;
    //spew out
    echo $start_date;
    if ( !empty( $custom_metabox->get_the_value('end_date') ) ) {
        echo ' – ' . $end_date;
    } else {
        //DO NOTHING
    }

any clue why this might hang up the entire site?

缺少) -

if ( !empty( $custom_metabox->get_the_value( 'end_date' ) ) ) {

You have to replace your code with below. Missing a parenthese

global $custom_metabox;
echo $start_date;
if ( !empty( $custom_metabox->get_the_value('end_date') ) ) {
    echo ' – ' . $end_date;
} else {
    //DO NOTHING
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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