我目前正在使用Wordpress网站 . 客户想要的是能够为 Session 创建时间表 . 他要求的是可以选择点击每个帖子中的按钮,这将增加一个15分钟的问答时段 .

我花时间的方式是使用一个帖子的 Headers ,该帖子将写为11:20 AM - 11:40 AM

我遇到的问题是,当在substr&strrchr中使用时,get_the_title()函数会变空 . 有趣的是,当我把它作为“上午11:20 - 上午11:40”而不是get_the_title()时,它很好 .

顺便说一句,我仍然是新的PHP,所以我可能不会以正确的方式去做 .

我的代码

echo qatime(get_the_title(), "AM");

和functions.php文件中的代码是

function qatime($string, $cat){
$newCat = $cat;
$stringsplit = substr(strrchr($string, "-"), 1);
$pieces = explode(":", $stringsplit);
$firstnum = preg_replace("/[^0-9]/", '', $pieces[0]);
$lastnum = preg_replace("/[^0-9]/", '', $pieces[1]);
$qaTime = 15;
$minute = $lastnum + $qaTime;

if($minute < 60){
} else {
    $firstnum += 1;
    $minute -= 60;
}
if($firstnum >= 12){
    $firstnum -= 12;
    if($firstnum == 0){
        $firstnum = 12; 
    }
    $newCat = 'PM';
}
if($pieces[0] == 12){
    $cat = 'PM';    
}
$minutes = str_pad($minute, 2, "0", STR_PAD_LEFT);
return $stringsplit." ".$cat." - ".$firstnum.":".$minutes." ".$newCat;
}

Solved

感谢您的快速回复 . 我发现不是使用get_the_title(),而是post-> post-title使它工作 .

$string = $post->post_title;
 echo qatime($string,'AM');