最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom field - Get_Field for Taxonomy Returns Error, ID, or Nothing ACF

programmeradmin5浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论