开发者

ampersand problem with wordpress categories

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 fr开发者_如何学Pythonom 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?


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);


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.


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 ‘.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜