Support

Account

Home Forums Front-end Issues Issue with Displaying Custom Shortcode

Solved

Issue with Displaying Custom Shortcode

  • I’m having trouble getting a custom shortcode to display in a post.

    I have a repeater named “my_presto_player” with two text sub fields named “presto_time” and “presto_topic”.

    I created the following function:

    function display_presto_content($content) {
        return do_shortcode($content);
    }
    
    if (have_rows('my_presto_player')) {
        while (have_rows('my_presto_player')) : the_row();
            $timestamp = get_sub_field('presto_time');
            $optional_text = get_sub_field('presto_topic');
            if (!empty($timestamp)) {
                $shortcode_content = '[pptime time="' . esc_attr($timestamp) . '"]' . esc_html($optional_text) . '[/pptime]';
                return display_presto_content($shortcode_content);
            }
        endwhile;
    }
    
    add_shortcode('showpptime', 'display_presto_content');

    Adding the shortcode [showpptime] doesn’t display any input from the sub fields at all.

    What am I doing wrong? I’m very new to ACF.

  • Your shortcode is just running shortcodes and not producing anything.

    First your loop should be inside of your shortcode function.

    Second you need to return the html

    this is just a guess, but hopefully you get the idea.

    
    function display_presto_content($content) {
    ob_start();
    if (have_rows('my_presto_player')) {
        while (have_rows('my_presto_player')) : the_row();
            $timestamp = get_sub_field('presto_time');
            $optional_text = get_sub_field('presto_topic');
            if (!empty($timestamp)) {
                $shortcode_content = '[pptime time="' . esc_attr($timestamp) . '"]' . esc_html($optional_text) . '[/pptime]';
                display_presto_content($shortcode_content);
            }
        endwhile;
    
      return ob_get_clean();
    }
    
    add_shortcode('showpptime', 'display_presto_content');
    
  • Hi John,

    Thank you so much for helping me with this. I’m very new to ACF & obviously not that strong at writing functions.

    I tried your suggestion adding the shortcode [showpptime] to a post, but doesn’t show the fields output, it just displays [showpptime]
    It should be displaying the “optional text” as a link to a timestamp in a Presto Player video.

    The original shortcode I am trying to use as a repeater is:
    [pptime time=”1:08″]Optional Text[/pptime]

    “pptime time” being the field “presto_time” and “Optional Text” being the field “presto_topic”.

    The developer of the Prestom Player sent me the following to use:

    $timestamp = get_field('timestamp_field');
    $optional_text = get_field('optional_text_field');
    
    // Use do_shortcode to display the content
    echo do_shortcode('[pptime time="' . esc_attr($timestamp) . '"]' . esc_html($optional_text) . '[/pptime]');

    “This code retrieves the values from the ACF fields and processes them through do_shortcode, creating the desired output.”

    Big thanks in advance for any help you can offer.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.