php - wordpress中get_the_category与get_the_terms的区别是什么?

查看:320
本文介绍了php - wordpress中get_the_category与get_the_terms的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

get_the_category和get_the_terms貌似都是用来获取分类信息的,
前者使用方法简单,不带参数,后者使用方法

<?php get_the_terms( $id, $taxonomy ); ?> 

例如:woocommerce中,获取当前产品页面的分类信息,前者居然无效,后者要使用

<?php get_the_terms( $id, 'product_cat'); 

为什么要填写'product_cat',我怎么知道这里为什么必须写product_cat,而不是product_cat1,从何得知?
请大神讲解一下什么时候用前者?什么时候用后者,两者的具体区别是什么?

解决方案

get_the_category 调用的就是 get_the_terms函数,只不过他的第二个参数带的是默认的 category

https://developer.wordpress.org/reference/functions/get_the_terms/
<?php get_the_terms( $id, 'category'); ?>

这篇关于php - wordpress中get_the_category与get_the_terms的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆