WordPress 类别的 & 符号问题

发布于 2024-08-31 03:50:48 字数 290 浏览 8 评论 0原文

我有一系列类别,其中一些带有 &即事件和娱乐。 我的脚本导入这些类别并使用其名称获取每个类别的 ID。 即:$cat_id = array(get_cat_id($linearray[0]),get_cat_id($linearray[1]),get_cat_id($linearray[2]),get_cat_id($linearray[3])); 然后,我的脚本使用这些类别 ID 将帖子添加到 wp。 我的问题是我使用 & 导入的类别;不是进口的。 这些相同的类别(当发送电子邮件通知时)会在&处破坏电子邮件。 有一个简单的解决方法吗?

I have an array of categories, some with an & i.e. events & entertainment.
My script imports these categories and gets the ID of each using its name.
i.e.: $cat_id = array(get_cat_id($linearray[0]),get_cat_id($linearray[1]),get_cat_id($linearray[2]),get_cat_id($linearray[3]));
My script then adds a post to wp using these category ID's.
My problem is that categories from my import with the & are not imported.
These same categories (when an email notice is sent) break the email at the &.
Is there a simple workaround to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

后知后觉 2024-09-07 03:50:48

当你写帖子时,你可以在那里写 & 而不是 & ,它将被翻译为 & 没有任何问题。

您也可以使用 str_replace 函数将其转换为 & 例如:

$new_text = str_replace('&', '&', $your_string);

When you are writing post, instead of & you can write & there and it will be translated to & without any problems.

Also you could use the str_replace function to convert that to & eg:

$new_text = str_replace('&', '&', $your_string);
东北女汉子 2024-09-07 03:50:48

我想在某些情况下我使用了我自己的 WordPress 自定义 sluggify 函数:

function sluggify($text) {
   $text = strtolower(htmlentities($text));
   $text = str_replace("&", "and", $text);
   $text = str_replace("andamp;", "and", $text);
   $text = str_replace(get_html_translation_table(), "-", $text);
   $text = str_replace(" ", "-", $text);
   $text = preg_replace("/[-]+/i", "-", $text);
   return $text;
}

注意两行重复的行:

$text = str_replace("&", "and", $text);
$text = str_replace("andamp;", "and", $text);

虽然重复,但非常必要!

对于下面无知的评论者 - 这是一个可重用的函数,您可以在其中传递任何字符串值,并且它将被猛击。因此,对于上述情况是可行的。

I think for some circumstances I used my own custom sluggify function for Wordpress:

function sluggify($text) {
   $text = strtolower(htmlentities($text));
   $text = str_replace("&", "and", $text);
   $text = str_replace("andamp;", "and", $text);
   $text = str_replace(get_html_translation_table(), "-", $text);
   $text = str_replace(" ", "-", $text);
   $text = preg_replace("/[-]+/i", "-", $text);
   return $text;
}

Note the two repetitive lines:

$text = str_replace("&", "and", $text);
$text = str_replace("andamp;", "and", $text);

Although repetitive, it's quite necessary!

For the ignorant commenter below - it's a reusable function where you can pass in any string value and it will be slugged. So, workable for the above case.

垂暮老矣 2024-09-07 03:50:48

有一个 WP 内置函数可以实现此目的:

$new_text = wp_specialchars_decode($your_string);

来自 文档

将许多 HTML 实体转换为其特殊字符。
具体涉及:&、<、>、" 和 '。

There is a WP built-in function for this:

$new_text = wp_specialchars_decode($your_string);

From the documentation:

Converts a number of HTML entities into their special characters.
Specifically deals with: &, <, >, ", and ‘.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文