$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Shortcode attributes causes 500 error after updating the page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Shortcode attributes causes 500 error after updating the page

matteradmin11PV0评论

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();
}

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();
}
Share Improve this question asked Nov 11, 2018 at 12:34 Dominic NeoDominic Neo 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

Post a comment

comment list (0)

  1. No comments so far