Dynamic Shortcode in Query Loop

Support General Dynamic Shortcode in Query Loop

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #36032
    Brent WilsonBrent Wilson
    Participant

    I want to use a Metabox shortcode to pull data into posts in a query loop. But it doesn't seem to be working properly, and I am wondering if it is because I am not specifying to pull data from the custom field for the current post.

    For example, I would like to use this shortcode: [rwmb_meta id="event_start_date_time" format="l F j, Y"]. Is there something I need to add to this shortcode to specify that the data should be pulled for the current post?

    #36038
    Long NguyenLong Nguyen
    Moderator

    Hi Brent,

    You can use the attribute object_id and assign the post ID to it to get the field value of the post in the loop. For example:

        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            $post_id = get_the_ID();
            echo do_shortcode('[rwmb_meta id="event_start_date_time" object_id='. $post_id .' format="l F j, Y"]');
        }

    Read more on the documentation https://docs.metabox.io/shortcode/

    #36046
    Brent WilsonBrent Wilson
    Participant

    That code above seems to have caused a fatal error. I'm not proficient with PHP. So can you walk me through this a bit more?

    Basically, I want to use one date/time custom field, but display it in a couple of ways for posts in a query loop:

    • I want to display it as l F j, Y
    • I also want to display it as g:i A
    #36047
    Brent WilsonBrent Wilson
    Participant

    So a couple of possible ways to do this:

    1. Use a shortcode that includes the format I want the data rendered in (for example, [rwmb_meta id="event_start_date_time" format="l F j, Y"] ). The issue with this option is that I am having trouble getting the data to render on the front end of the post query loop. So I was wondering if I somehow need to include a parameter to tell the shortcode to pull data for the current post.

    2. Use the post meta field ID (for example, event_start_date_time). But in this case, it renders on the front end in the format YYYY-MM-DD HH:MM. So I would need some way to pull this data in a couple different formats using post meta field IDs.

    What would you suggest?

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