我使用以下代码提取一些关键字,并将它们作为标签添加到wordpress中。
if (!is_array($keywords)) {
$count = 0;
$keywords = explode(',', $keywords);
}
foreach($keywords as $thetag) {
$count++;
wp_add_post_tags($post_id, $thetag);
if ($count > 3) break;
}
该代码将只获取4个关键字,但除此之外,我只想在它们超过2个字符时提取,所以我不会得到只有2个字母的标签。
有人能帮我吗?
strlen($string)
将为您提供字符串的长度:
if (!is_array($keywords)) {
$count = 0;
$keywords = explode(',', $keywords);
}
foreach($keywords as $thetag) {
$thetag = trim($thetag); // just so if the tags were "abc, de, fgh" then de won't be selected as a valid tag
if(strlen($thetag) > 2){
$count++;
wp_add_post_tags($post_id, $thetag);
}
if ($count > 3) break;
}
使用strlen检查长度。
int strlen ( string $string )
返回给定字符串的长度。
if(strlen($thetag) > 2) {
$count++;
wp_add_post_tags($post_id, $thetag);
}