Use the result of a shortcode as a variable

Support MB Views Use the result of a shortcode as a variable

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #33170
    AMXAMX
    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

    #33171
    AMXAMX
    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 NguyenLong 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 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.