WordPress在分类法中使用逗号

huangapple go评论53阅读模式
英文:

WordPress using comma in taxonomy

问题

在 PHP 8 发布之前,我能够使用以下代码将“--”替换为逗号在自定义标签中。

if(!is_admin()){ // 确保过滤器只在前端调用
	
	$custom_taxonomy_type = 'winners_name';	// 这里放入您的分类法类型
	
	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 致命错误: 
未捕获的错误: 
未定义常量“comma_taxonomy_filter” 
/nas/content/live/stagelask/wp-content/themes/Avada-Child-Theme/functions.php:31
堆栈跟踪:
#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 {main}
在/nas/content/live/stagelask/wp-content/themes/Avada-Child-Theme/functions.php的第31行抛出,来源: https://stagelask.wpengine.com

我首先进行了一些研究,看是否有解决方法。然后我尝试(不成功地)定义了常量comma_taxonomy_filter

任何帮助将不胜感激。

谢谢,
Mark

英文:

Until the release of PHP 8 I was able to replace "--" with a comma in a custom tag. using the following code.

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

After the PHP update it's giving me the following error.
PHP Fatal error:
Uncaught Error:
Undefined constant "comma_taxonomy_filter" in /nas/content/live/stagelask/wp-content/themes/Avada-Child-Theme/functions.php:31\nStack trace:\n#0 /nas/content/live/stagelask/wp-settings.php(591): include()\n#1 /nas/content/live/stagelask/wp-config.php(119): require_once('/nas/content/li...')\n#2 /nas/content/live/stagelask/wp-load.php(50): require_once('/nas/content/li...')\n#3 /nas/content/live/stagelask/wp-blog-header.php(13): require_once('/nas/content/li...')\n#4 /nas/content/live/stagelask/index.php(17): require('/nas/content/li...')\n#5 {main}\n thrown in /nas/content/live/stagelask/wp-content/themes/Avada-Child-Theme/functions.php on line 31, referer: https://stagelask.wpengine.com

I first did some research to see if there was a fix for this. Then I tried (unsuccessfully) to define the constant comma_taxonomy_filter.

Any help would be appreciated.

Thanks,
Mark

答案1

得分: 0

只需将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');
英文:

Only need to fix the second parameter of the add_filter() calls to be strings:

add_filter('get_the_taxonomies',    'comma_taxonomies_filter');
add_filter('get_terms',             'comma_taxonomies_filter');
add_filter('get_the_terms',         'comma_taxonomies_filter');

huangapple
  • 本文由 发表于 2023年7月13日 22:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定