admin 管理员组文章数量: 1184232
I’m using the classic editor and have disabled Gutenberg blocks and global styles.
I’ve created a custom post type named Resources, for which I’ve also created an archive archive-resources.php.
I’ve created a Taxonomy Custom Field labeled Resource Type (name: res_type), which is linked to Resources and has a Radio Button appearance.
I’ve created a Taxonomy for my custom post type and custom field labeled Materials (the three options for materials are “Book,” “Course,” and “eBook”).
In my archive file, I’ve started the loop if ( have_posts() ) : while ( have_posts() ) : the_post();, and in the loop, I am trying to display which Material was selected from the Taxonomy Custom Field in the Resource that was created.
Initially, I was trying to use the following code:
$res_type = get_field('res_type');
if ($res_type) {
echo '<h4 class="open-sans-lite resource-subtitle smaller-text">'.esc_html($res_type).'</h4>';
}
This code doesn’t work because if my Taxonomy Custom Field “Return Value” is set to ID, it only returns a number, and if it’s set to Object, it returns an error ("fatal error/exception").
Then, I tried this code:
$res_type = get_field('res_type');
if ($res_type) {
echo '<h4 class="open-sans-lite resource-subtitle smaller-text">'.esc_html($res_type->name).'</h4>';
}
However, this resulted in nothing at all in my HTML, not even an empty H4 element.
I’ve also been trying get_field_object() to no avail.
Please help me understand how to retrieve the “Book,” “eBook,” or “Course” taxonomy that’s selected for their respective Resource from the Taxonomy Custom Field in the loop.
本文标签: custom fieldGetField for Taxonomy Returns Error Id or Nothing ACF
版权声明:本文标题:custom field - Get_Field for Taxonomy Returns Error, ID, or Nothing ACF 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1738339548a1966478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论