Custom Fields
Re-Envisioned
Support MB Views Use the result of a shortcode as a variable

  • Creator
    Topic
  • #33170
    AMX
    Participant

    Hi,

    I have a third-party plugin that imports data from external sources.
    The plugin's shortcode works fine in Views for displaying data.

    But I need to make some operations on the data and I'm having some difficulties.

    When I do this:

    {% set my_var %}
    [my_shortcode]
    {% endset %}
    
    {{my_var}}

    it will display the shortcode result as a number on the front-end, as expected.
    However, in reality my_var is still equal to the text [my_shortcode], so I cannot do mathematical operations on it or save it to a custom field.

    Is there a way to execute the shortcode first, and then have the shortcode's result as a variable (number)?
    I have tried mb.do_shortcode('[my_shortcode]') but it doesn't seem to work...

    Greetings,
    Tom

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #33171
    AMX
    Participant

    I suppose the shortcode should be evaluated before rendering the template, but I don't know how to achieve that. Perhaps there is a custom function for that?

    #33190
    Long Nguyen
    Moderator

    Hi Tom,

    I think you can create a function that returns a value in PHP code, then assign it to a variable in Twig. Refer to the documentation https://docs.metabox.io/extensions/mb-views/#running-php-functions

    {% set my_function = mb.my_function() %}

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