Support

Account

Home Forums General Issues Content not showing when using my self-programmed shortcode in a custom subfield

Unread

Content not showing when using my self-programmed shortcode in a custom subfield

  • I’m using Advanced Custom Fields with a Repeater-Field to display the Content of my page. So I made three fields per Row (Left Content, Middle Content and Right Content). In my Template I get the fields contents with get_sub_field()-function from ACF. But when I fill one of the fields with a shortcode coded by me, no content at all is showing… I already tried to debug but I failed… I used my shortcode before on normal content (the_content()) and it worked fine!

    // EDIT: I figured out that in my template when I var_dump have_rows() AFTER the “if ( have_rows( ‘content-blocks’ ) )” when the while begins… have_rows() is FALSE! When I remove my shortcode in wordpress, its TRUE after the if. I dont’ know why šŸ™

    // EDIT 2: When I change the Field-Type from WYSIWYG to Textfield, everything works fine with my shortcode. Everything is displayed as I would expect it… But why isn’t it working with the WYSIWYG Editor…

    My ShortCode:

    function ws_shortcode_cards( $atts, $content = null )
    {
        $categorySlug = '';
    
        extract( shortcode_atts( array(
            'category' => '',
            'id' => ''
        ), $atts ) );
    
        $args = array(
            'post_type' => 'ws_cards',
            'posts_per_page' => 10,
            'category_name' => esc_attr( $category ),
            'page_id' => esc_attr( $id )
        );
    
        $loop = new WP_Query( $args );
    
        $curPageID = get_the_id();
    
        if ( $loop->have_posts() )
        {
            $output = "<ul class=\"uk-grid\" data-uk-grid-match data-uk-margin=\"{cls:'uk-margin-top'}\">\n";
    
            // Check if the page where the shortcode is used is generally password-protected
            global $wpdb;
            $query = "SELECT post_password FROM $wpdb->posts WHERE ID = $curPageID";
            $isPasswordProtected = $wpdb->get_var($query) == '' ? false : true;
    
            while ( $loop->have_posts() )
            {
                $loop->the_post();
    
                $output .= "\t<li class=\"uk-width-large-1-3 uk-width-medium-1-2 uk-width-1-1\">\n";
                $output .= "\t\t<div class=\"uk-panel uk-panel-box uk-panel-box-primary\">\n";
                $output .= "\t\t\t<h3 class=\"uk-text-primary\">" . get_the_title() . "</h3>\n";
                $output .= "\t\t</div>\n";
    
                if ( post_custom( 'slider-shortcode' ) != '' )
                {
                    $output .= "\t\t<div class=\"uk-panel uk-margin-small-top\">\n";
                    $output .= "\t\t\t" . do_shortcode( post_custom( 'slider-shortcode' ) ) . "\n";
                    $output .= "\t\t</div>\n";
                }
    
                if ( have_rows( 'panels' ) )
                {
                    while ( have_rows( 'panels' ) )
                    {
                        the_row();
    
                        $output .= "\t\t<div class=\"uk-panel uk-margin-small-top uk-panel-box uk-panel-box-primary\">\n";
                        $output .= "\t\t\t<strong class=\"uk-panel-title uk-margin-remove uk-display-block\">" . get_sub_field( 'panel-headline' ) . "</strong>\n";
                        $output .= "\t\t\t" . get_sub_field( 'panel-text' ) . "\n";
                        $output .= "\t\t</div>\n";
                    }
                }
    
                // Display places only if the page is generally password protected
                if ( $isPasswordProtected && ( ( post_custom( 'reserved_places_male' ) && post_custom( 'max_places_male' ) ) || ( post_custom( 'reserved_places_female' ) && post_custom( 'max_places_female' ) ) ) )
                {
                    $output .= "\t\t<div class=\"uk-panel uk-margin-small-top uk-panel-box uk-panel-box-primary\">\n";
                    $output .= "\t\t\t<strong class=\"uk-panel-title uk-margin-remove uk-display-block\">BehandlungsplƤtze</strong>\n";
                    $output .= "\t\t\t<div class=\"uk-grid uk-margin-top\">";
    
                    if ( post_custom( 'reserved_places_male' ) && post_custom( 'max_places_male' ) )
                    {
                        $output .= "\t\t\t\t<div class=\"uk-width-1-2\">";
                        $output .= "\t\t\t\t\t<i class=\"uk-icon-male uk-margin-right\"></i>" . post_custom( 'reserved_places_male' ) . " / " . post_custom( 'max_places_male' ) . "
    \n";
                        $output .= "\t\t\t\t</div>";
                    }
    
                    if ( post_custom( 'reserved_places_female' ) && post_custom( 'max_places_female' ) )
                    {
                        $output .= "\t\t\t\t<div class=\"uk-width-1-2\">";
                        $output .= "\t\t\t\t\t<i class=\"uk-icon-female uk-margin-right\"></i>" . post_custom( 'reserved_places_female' ) . " / " . post_custom( 'max_places_female' ) . "
    \n";
                        $output .= "\t\t\t\t</div>";
                    }
    
                    $output .= "\t\t\t</div>";
                    $output .= "\t\t</div>\n";
                }
    
                $output .= "\t\t<a class=\"btn-cards-contact uk-button uk-button-primary uk-button-large uk-margin-small-top\" href=\"" . get_permalink( post_custom( 'contact-page' ) ) . "\"><i class=\"uk-float-left btn-arrow-right\"></i> Jetzt Kontakt aufnehmen <span class=\"uk-float-right icon-wrapper\"><i class=\"uk-icon-envelope\"></i></span></a>";
                $output .= "\t</li>";
            }
    
            $output .= "</ul>";
        }
        else
        {
            $output = "Keine Visitenkarten vorhanden";
        }
    
        return $output;
    }

    My template for displaying the ACF Rows and Fields:

    <?php
    
    get_header();
    
    if (have_posts())
    {
        while (have_posts())
        {
            the_post();
    
            ?>
    
            <div id="content">
                <div id="breadcrumb">
                    <?php ws_breadcrumb(); ?>
                </div>
    
                <div id="content-inner" class="uk-container uk-container-center">
    
                <?php
    
                if ( have_rows( 'content-blocks' ) )
                {
                    while ( have_rows( 'content-blocks' ) )
                    {
                        the_row();
    
                        $rowCount = 0;
    
                        if ( get_sub_field( 'left-column' ) ) $rowCount++;
                        if ( get_sub_field( 'middle-column' ) ) $rowCount++;
                        if ( get_sub_field( 'right-column' ) ) $rowCount++;
    
                        echo "<div class=\"uk-grid\">\n";
    
                        switch ($rowCount)
                        {
                            case 3:
    
                                echo "<div class=\"uk-width-1-3\">" . get_sub_field( 'left-column' ) . "</div>";
                                echo "<div class=\"uk-width-1-3\">" . get_sub_field( 'middle-column' ) . "</div>";
                                echo "<div class=\"uk-width-1-3\">" . get_sub_field( 'right-column' ) . "</div>";
    
                                break;
    
                            case 2:
    
                                if ( get_sub_field( 'left-column' ) && get_sub_field( 'middle-column' ) )
                                {
                                    echo "<div class=\"uk-width-1-3\">" . do_shortcode( get_sub_field( 'left-column' ) ) . "</div>";
                                    echo "<div class=\"uk-width-2-3\">" . do_shortcode( get_sub_field( 'middle-column' ) ) . "</div>";
                                }
                                elseif ( get_sub_field( 'left-column' ) && get_sub_field( 'right-column' ) )
                                {
                                    echo "<div class=\"uk-width-1-2\">" . do_shortcode( get_sub_field( 'left-column' ) ) . "</div>";
                                    echo "<div class=\"uk-width-1-2\">" . do_shortcode( get_sub_field( 'right-column' ) ) . "</div>";
                                }
                                elseif ( get_sub_field( 'middle-column' ) && get_sub_field( 'right-column' ) )
                                {
                                    echo "<div class=\"uk-width-2-3\">" . do_shortcode( get_sub_field( 'middle-column' ) ) . "</div>";
                                    echo "<div class=\"uk-width-1-3\">" . do_shortcode( get_sub_field( 'right-column' ) ) . "</div>";
                                }
    
                                break;
    
                            case 1:
    
                                if ( get_sub_field( 'left-column' ) ) $col = get_sub_field( 'left-column' );
                                if ( get_sub_field( 'middle-column' ) ) $col = get_sub_field( 'middle-column' );
                                if ( get_sub_field( 'right-column' ) ) $col = get_sub_field( 'right-column' );
    
                                echo "<div class=\"uk-width-1-1\">" . do_shortcode( $col ) . "</div>";
    
                                break;
    
                            default:
                                break;
                        }
    
                        echo "</div>\n";
    
                    }
                }
    
                ?>
    
                </div>
            </div>
    
            <?php
        }
    }
    
    get_footer();
    
    ?>

    I already tried variants of using do_shortcode but nothing worked. It must be a problem with my self-coded shortcode, because if I remove the code in it for example and echo just a word like “hello”, it shows the content AND my “hello” … Anything in my function must prevent the code from working with the ACF…

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.