0

I have created a shortcode that allows me to pass an attribute like so [product_swatch swatchtype="swatch"]. It works fine on the front end, but every time I update the page it leads to a 500 error. If no attributes are passed or the attribute returns nothing, updating the page is as per normal.

I'm quite stumped because WP_DEBUG doesn't bring up anything either.

Here's the relevant function:

function product_swatch_shortcode($atts)
{
    $swatch_shortcode_atts = extract( shortcode_atts( array(
        'swatchtype' => '',
    ), $atts));

    $swatch_args = array (
        'post_type'      => 'swatch',
        'posts_per_page' => -1,
        'tax_query'      => array(
            array(
                'taxonomy' => 'swatch-type',
                'field'    => 'slug',
                'terms'    => $swatchtype,
            ),
        ),
    );

    // The Query
    $swatch_query = new WP_Query( $swatch_args );

    // The Loop
    if ( $swatch_query->have_posts() ) {
        while ( $swatch_query->have_posts() ) {

            $swatch_query->the_post();

            $swatch_name = get_the_title();
            $swatch_image_small = get_the_post_thumbnail_url( '', 'medium' );
            $swatch_image_big = get_the_post_thumbnail_url( '', 'full' );
            $interactive_designer_link = types_render_field( 'swatch-interactive-designer-link', array('output' => 'raw') );
            $preview_image = types_render_field( 'swatch-preview-image', array('output' => 'raw') );
            $swatch_id = strtolower( preg_replace('/\s+/', '', get_the_title()) );

            $output  = '<a href="javascript:;" data-fancybox data-src="#' . $swatch_id . '" class="swatch"><img src="' . $swatch_image_small . '">' . $swatch_name . '</a>';
            $output .= '<div id="' . $swatch_id . '" class="lightbox swatch-details">';
            $output .= '<div class="swatch-col1"><h5>' . $swatch_name . '</h5><img src="' . $swatch_image_big . '">';
            $output .= '<a href="' . $interactive_designer_link . '" class="btn" style="margin:30px 0;">VIEW PRODUCT IN SIMULATION</a></div>';
            $output .= '<div class="swatch-col2"><img src="' . $preview_image . '"></div></div>';

            echo $output;

        }
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
}
0

Found the issue to be the types_render_field() function (I am using the Toolset plugin). Switched to using get_post_meta() instead and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.