在 PHP 8 发布之前,我可以在自定义标签中用逗号替换“--”。使用以下代码。
if(!is_admin()){ // make sure the filters are only called in the frontend
$custom_taxonomy_type = 'winners_name'; // here goes your taxonomy type
function comma_taxonomy_filter($tag_arr){
global $custom_taxonomy_type;
$tag_arr_new = $tag_arr;
if($tag_arr->taxonomy == $custom_taxonomy_type && strpos($tag_arr->name, '--')){
$tag_arr_new->name = str_replace('--',', ',$tag_arr->name);
}
return $tag_arr_new;
}
add_filter('get_'.$custom_taxonomy_type, comma_taxonomy_filter);
function comma_taxonomies_filter($tags_arr){
$tags_arr_new = array();
foreach($tags_arr as $tag_arr){
$tags_arr_new[] = comma_taxonomy_filter($tag_arr);
}
return $tags_arr_new;
}
add_filter('get_the_taxonomies', comma_taxonomies_filter);
add_filter('get_terms', comma_taxonomies_filter);
add_filter('get_the_terms', comma_taxonomies_filter);
}
PHP 更新后,出现以下错误。 PHP 致命错误: 未捕获的错误: /nas/content/live/stagelask/wp-content/themes/Avada-Child-Theme/functions.php:31 中未定义常量“comma_taxonomy_filter” 堆栈跟踪: #0 /nas/content/live/stagelask/wp-settings.php(591): include() #1 /nas/content/live/stagelask/wp-config.php(119): require_once('/nas/content/li...') #2 /nas/content/live/stagelask/wp-load.php(50): require_once('/nas/content/li...') #3 /nas/content/live/stagelask/wp-blog-header.php(13): require_once('/nas/content/li...') #4 /nas/content/live/stagelask/index.php(17): require('/nas/content/li...') #5 {主要} 扔在第 31 行 /nas/content/live/stagelask/wp-content/themes/Avada-Child-Theme/functions.php 中,参考:https://stagelask.wpengine.com
我首先做了一些研究,看看是否有解决办法。然后我尝试(未成功)定义常量 comma_taxonomy_filter。
如有任何帮助,我们将不胜感激。
谢谢, 马克
只需将
add_filter()
调用的第二个参数固定为字符串即可:
add_filter('get_the_taxonomies', 'comma_taxonomies_filter');
add_filter('get_terms', 'comma_taxonomies_filter');
add_filter('get_the_terms', 'comma_taxonomies_filter');