How to display term field in post sidebar

Support MB Views How to display term field in post sidebarResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #41403
    RoderickRoderick
    Participant

    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

    #41408
    PeterPeter
    Moderator

    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

    #41444
    RoderickRoderick
    Participant

    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

    #41461
    PeterPeter
    Moderator

    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

    #41473
    RoderickRoderick
    Participant

    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.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.