WordPress 技巧:获取 Term 的分类模式

建站笔记1年前 (2023)发布 小萝卜头
203 0 0

WordPress 竟然没有一个根据 $term_id 获取分类模式 taxonomy 的函数,那就自己写一个:

function get_term_taxonomy($id){
	$term	= get_term($id);

	return ($term && !is_wp_error($term)) ? $term->taxonomy : null;
}

这样在其他代码中就方便调用了,并且还处理了返回是 WP_Error 的情况。

哪一天如果 WordPress 自己也支持了?那就加上 function_exists 的判断:

if(!function_exists('get_term_taxonomy')){
	function get_term_taxonomy($id){
		$term	= get_term($id);

		return ($term && !is_wp_error($term)) ? $term->taxonomy : null;
	}
}

好,非常完美,加入 WPJAM Basic,下一版本上线的时候,就有该函数了。

收录于{我爱水煮鱼} 原文链接原文链接

© 版权声明

相关文章

暂无评论

暂无评论...