How to display term field in post sidebar
- This topic has 4 replies, 2 voices, and was last updated 2 years, 6 months ago by
Roderick.
-
AuthorPosts
-
April 10, 2023 at 1:29 PM #41403
Roderick
ParticipantHi, I'm trying to display a category custom field value inside of the sidebar of a post with that same category. I would like to use MB Views so I can add a few conditions.
Using {{ term.bookinglink }} doesnt display anything and only seems to work on the category page. What do I have to do to make this work?
Thanks in advance,
RodApril 10, 2023 at 7:39 PM #41408Peter
ModeratorHello Rod,
Please follow the documentation to understand the main query https://docs.metabox.io/extensions/mb-views/#main-query
So in this case, if you use
term.bookinglink, it will not return the term meta value. You can use the helper functionrwmb_meta()to get the term meta. For example:{{ mb.rwmb_meta( 'bookinglink', { object_type: 'term' }, 123 ) }}Please read more on the documentation
https://docs.metabox.io/extensions/mb-views/#running-php-functions
https://docs.metabox.io/extensions/mb-term-meta/#getting-field-valueApril 12, 2023 at 2:18 AM #41444Roderick
ParticipantThank you very much Peter. I was wondering, is it also possible to use a shortcode to set the term ID? This shortcode is placed in my functions.php file and outputs the ID of the primary category of my post: [yoast-primary-category-id]. I want to use this to set the term ID dynamically.
I tried to use a function 'do_shortcode' for the shortcode, but this does not seem to work.
Hope you have an idea for this too?{% set category_id = mb.function('do_shortcode', '[yoast-primary-category-id]') %} {% set term_bookinglink = mb.rwmb_meta( 'bookinglink', { object_type: 'term' }, category_id ) %} {{ term_bookinglink }}Thanks a million,
RodApril 12, 2023 at 6:58 PM #41461Peter
ModeratorHello,
You can use the proxy
mb.to call the PHP functiondo_shortcode()directly without usingfunction(). For example:
{% set category_id = mb.do_shortcode('[yoast-primary-category-id]') %}Or you can try to use the PHP function
yoast_get_primary_term_id()to get the Yoast primary term ID{% set category_id = mb.yoast_get_primary_term_id( 'taxonomy_slug', post_id_or_object ) %}Please read more here https://stackoverflow.com/questions/38800626/how-to-get-primary-category-set-via-yoast-seo-plugin-in-wordpress
April 13, 2023 at 11:49 AM #41473Roderick
ParticipantThank you once again Peter! I got it working thanks to you.
I wish Metabox tutorials would contain a bit more of these types of 'micro lessons' and how to's.
-
AuthorPosts
- You must be logged in to reply to this topic.