admin管理员组

文章数量:1026989

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.

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 ErrorIdor Nothing ACF