Skip Navigation

[Resolved] Display category custom field for custom post type using PHP

This thread is resolved. Here is a description of the problem and solution.

Problem: Display category custom field for custom post type using PHP

Solution: To display the term field using PHP - the correct way is to use the Types field API function: types_render_termmeta()

You can find the proposed solution in this case with the following reply: https://toolset.com/forums/topic/display-category-custom-field-for-custom-post-type-using-php/#post-2007069

Relevant Documentation: => https://toolset.com/documentation/user-guides/views/displaying-wordpress-term-fields/#displaying-term-fields-with-types-api => https://toolset.com/documentation/customizing-sites-using-php/functions/#image

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 2 replies, has 2 voices.

Last updated by chrisH-10 6 months, 2 weeks ago.

Assigned support staff: Minesh.

Author
Posts
#2006863

Tell us what you are trying to do?
I am using an Oxygen template to iterate over a toolset custom post type. I would like to show the image of the category assigned to that post. The Oxygen template is just PHP. I wrote the following line.
<?php echo do_shortcode("[wpv-post-taxonomy-iterator taxonomy='category'][types termmeta='wpcf-category-image'][wpv-taxonomy-title][/wpv-post-taxonomy-iterator]"); ?>
I use the title just to validate that the issue isn't the custom field. This works partially in that toolset appears to grab the ID of the assigned category. The output is "[types term_id="30" termmeta='category-image'][wpv-taxonomy-title term_id="30"]". However, it appears that there should be a second step where the shortcode is processed further.

Is there a similar example that we can see?
Nope.

What is the link to your site?
hidden link this shows the output from the above line of PHP in the image area (second item).

I saw a suggestion to use a View elsewhere here in the forums but this site is using Toolset Blocks which appears to preclude the use of Views - which is a pity.

#2007069

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To display the term field using PHP - the correct way is to use the Types field API function: types_render_termmeta()

I suggest you should grab the value of assigned terms of your current post using the native WordPress function: wp_get_object_terms()
=> https://developer.wordpress.org/reference/functions/wp_get_object_terms/

And then loop over the terms you get and use the function types_render_termmeta() to render the custom term field.

Please check the following Doc:
=> https://toolset.com/documentation/user-guides/views/displaying-wordpress-term-fields/#displaying-term-fields-with-types-api

For example:

<?php echo types_render_termmeta("cat-featured-image", array( "alt" => "Category Featured Image", "width" => "300", "height" => "200" ) ); ?>

For all available attributes for image field type, you should check the following doc:
=> https://toolset.com/documentation/customizing-sites-using-php/functions/#image

#2012283

My issue is resolved now. Thank you!