[英]checking if value is empty
I'm trying to patch up some code that outputs a date range in our wp functions file. 我正在尝试修补一些在wp函数文件中输出日期范围的代码。 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–..
我的问题是,有时我们不在帖子中添加end_date,因此此代码输出一组空白的数字,例如
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. 我试图通过不使用检查是否为空值来实现$ end_date来缩小范围,但最终却只是挂断了我的网站。
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.