Skip Navigation

[Gelöst] Display category custom field for custom post type using PHP

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 support ticket is created vor 3 Jahre. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 Antworten, has 2 Stimmen.

Last updated by chrisH-10 vor 3 Jahre.

Assisted by: Minesh.

Author
Artikel
#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: Englisch (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!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.