0

我正在使用以下代码提取一些关键字并将它们作为标签添加到 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 个字母的标签。

有人能帮我吗。

4

2 回答 2

1

使用strlen检查长度。

int strlen ( string $string )

返回给定字符串的长度。

if(strlen($thetag) > 2) {
    $count++;
    wp_add_post_tags($post_id, $thetag);
}
于 2012-08-06T02:42:49.617 回答
1

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;
}
于 2012-08-06T02:43:21.083 回答