更新页面后,快捷码属性导致500错误

时间:2018-11-11 作者:Dominic Neo

我创建了一个短代码,允许我传递这样的属性[product_swatch swatchtype="swatch"]. 它在前端工作正常,但每次我更新页面时都会导致500个错误。如果未传递任何属性或属性未返回任何内容,则按照正常方式更新页面。

我很困惑,因为WP\\u DEBUG也没有显示任何内容。

以下是相关功能:

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

1 个回复
最合适的回答,由SO网友:Dominic Neo 整理而成

发现问题为types_render_field() 函数(我正在使用工具集插件)。切换到使用get_post_meta() 相反,它工作得很好。

结束

相关推荐

Shortcode with foreach

我不能对foreach使用shortcode。它给了我语法错误。我错在哪里?function create_galeri_shortcode($atts) { $atts = shortcode_atts( array( ), $atts, \'galeri\' ); if ( has_post_format( \'gallery\' )) { $images = get_post_