Support Forum
Hi, 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,
Rod
Hello 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 function rwmb_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-value
Thank 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,
Rod
Hello,
You can use the proxy mb.
to call the PHP function do_shortcode()
directly without using function()
. 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
Thank 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.