Return vs Echo Shortcode

时间:2014-03-25 作者:Austin Biggs

好的,我创建了一个简短的代码,其中echo是一些输出;我发现的问题是,必须返回值而不是echo\'d。我在转换快捷码以返回日期时遇到问题。。

我试过了,但它打破了我的短码。。

如果此短代码正确返回代码而不是回显代码,那么它会是什么样子?

// [service slug="foobar"]
function services_shortcode( $atts ) {

    // Attributes
    extract( shortcode_atts(
        array(
            \'slug\' => \'\',
        ), $atts )
    );

if ( isset( $slug ) ) {
    $args = array( \'post_type\' => \'cbd_services\', \'name\' => $slug ); // -1 Shows ALL Posts
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    $custom = get_post_custom($post->ID);
    $galleryPhotos = unserialize($custom["gallery_photos"][0]);
 ?>
    <div class="accordion closed">
        <h4 class="accordionTitle"><?php the_title(); ?><span>+</span></h4>
        <div class="accordionContent">
            <?php the_content(); ?>
            <?php if(!empty($galleryPhotos)){
                foreach($galleryPhotos as $photo){ ?>
                    <div class="photoContainer">
                        <a class="fresco" data-fresco-group="<?php echo $slug; ?>" href="<?php echo $photo[\'gallery_imagesrc\']; ?>">
                            <div class="hover">#makeitbigger</div><img src="<?php echo get_template_directory_uri(); ?>/thumb.php?src=<?php echo urlencode($photo[\'gallery_imagesrc\']); ?>&w=452&h=275&zc=1" />
                            <span class="photoCaption"><?php echo $photo[\'gallery_title\']; ?></span>
                        </a>
                    </div>
            <?php   }
            } ?>
        </div>
    </div>
<?php  endwhile; wp_reset_query();

}}
add_shortcode( \'service\', \'services_shortcode\' );

3 个回复
SO网友:Rajeev Vyas

只需定义一个变量,将所有html压缩为字符串并返回它。

<?php
function services_shortcode( $atts ) {

    // Attributes
    extract( shortcode_atts(
        array(
            \'slug\' => \'\',
        ), $atts )
    );
$html=\'\';
if ( isset( $slug ) ) {
    $args = array( \'post_type\' => \'cbd_services\', \'name\' => $slug ); // -1 Shows ALL Posts
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    $custom = get_post_custom($post->ID);
    $galleryPhotos = unserialize($custom["gallery_photos"][0]);

    $html.=\'<div class="accordion closed">
        <h4 class="accordionTitle">\'.get_the_title($post->ID).\'<span>+</span></h4>
        <div class="accordionContent">\'.get_the_content();
        if(!empty($galleryPhotos)){
                foreach($galleryPhotos as $photo){ 
                    $html.=\'<div class="photoContainer">
                        <a class="fresco" data-fresco-group="\'.$slug.\'" href="\'.$photo[\'gallery_imagesrc\'].\'">
                            <div class="hover">#makeitbigger</div><img src="\'.get_template_directory_uri().\'/thumb.php?src=\'.urlencode($photo[\'gallery_imagesrc\']).\'&w=452&h=275&zc=1" />
                            <span class="photoCaption">\'.$photo[\'gallery_title\'].\'</span>
                        </a>
                    </div>\';
              }
            } 
        $html.=\'</div>\';
    $html.=\'</div>\';
    endwhile; wp_reset_query();
    }

    return $html;
}
add_shortcode( \'service\', \'services_shortcode\' );

SO网友:PJ3

别忘了ob_end_clean() 所以它不会回显您的代码!

希望我能对@Shazzad的答案添加评论,但我必须发布一个答案:

ob_start() //start buffering
?>

<div>...your html...</div>

<?php
$html = ob_get_contents() //get your content
ob_end_clean() //clean echoed html code

return $html

SO网友:Shazzad

最简单的方法是使用缓冲。您可能已经使用ob_start()ob_get_contents() PHP本机函数。所以你的代码是-

ob_start();
if ( isset( $slug ) ) {
.... rest of your code ....
<?php  endwhile; wp_reset_query();
$content = ob_get_contents();

return $content;

结束

相关推荐

Overwriting a Shortcode

我有一个扩展Visual Composer插件的主题,在首页上有一个滑块。滑块将显示来自五个不同客户的五份推荐信。我想在滑块中添加每个推荐信的特色图像作为缩略图。以下是父主题的简短代码:function jo_customers_testimonials_slider( $atts ) { extract( shortcode_atts( array( \'limit\' => 5, \"widget_title\" => __(\'What Are People Saying