如何在短码中返回多行?

时间:2011-12-30 作者:janoChen

我在互联网上找到了一个短代码示例:

function project_shortcode( $atts, $content = null ) {
    extract( shortcode_atts( array(
        \'class\' => \'\',
        \'id\' => \'\',
        ), $atts ) );

        return \'<div id="\' . $id . \'" class="\' . $class . \'">\' . $content . \'</div>\';

}
add_shortcode(\'button\', \'project_shortcode\');
这是我想要的输出:

<div class="container item">
    <div class="row">
        <div class="intro twelvecol">
            <div class="top-border"></div>
        </div>
        <div class="screenshot eightcol">
            <img src="<?php bloginfo( \'template_url\' ); ?>/images/studyatbest.png">
        </div>
        <div class="screenshot fourcol last">
            <h3>BEST LANGUAGE CENTER</h3>
            <p>BEST Language Center is an educational establishment in Taichung, Taiwan. I was asked to build and design a website they could use to offer classes, programs and display photos online.</p>
            <ul>
                <li>
                    <h4>ROLE</h4>
                    <p>Design, HTML/CSS, JavaScript</p>
                <li>
                    <h4>YEAR</h4>
                    <p>2010</p>
                </li>
                <li>
                    <h4>WEBSITE</h4>
                    <p><a href="http://studyatbest.com/">studyatbest.com</a></p>
                </li>
            </ul>
        </div>
    </div>
</div>
但我不知道如何才能在短代码中返回多行代码(以保持代码干净)。有什么建议吗?

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

不要使用输出缓冲。它太慢,有时很难调试。使用heredoc.

示例:

$output = <<<MYUNIQUENAME
    <div>$foo
      <p>Some $bar</p>
    </div>
MYUNIQUENAME;

SO网友:janoChen

明白了:

// Project shortcode
function project_shortcode( $atts, $content = null ) {
    extract( shortcode_atts( array(
        \'client\' => \'\',
        \'screenshot\' => \'\',
        \'role\' => \'\',
        \'year\' => \'\',
        \'website\' => \'\',
        ), $atts ) );

        ob_start();
    ?>

    <div class="container item">
        <div class="row">
            <div class="intro twelvecol">
                <div class="top-border"></div>
            </div>
            <div class="screenshot eightcol">
                <img src="<?php bloginfo( \'template_url\' ); ?>/images/<?php echo $screenshot ?>">
            </div>
            <div class="screenshot fourcol last">
                <h3><?php echo $client ?></h3>
                <p><?php echo $content ?></p>
                <ul>
                    <li>
                        <h4>ROLE</h4>
                        <p><?php echo $role ?></p>
                    <li>
                        <h4>YEAR</h4>
                        <p><?php echo $year ?></p>
                    </li>
                    <li>
                        <h4>WEBSITE</h4>
                        <p><a href="http://<?php echo $website ?>"><?php echo $website ?></a></p>
                    </li>
                </ul>
            </div>
        </div>
    </div>

    <?php
        return ob_get_clean();
}
add_shortcode(\'project\', \'project_shortcode\');
(请告诉我该代码中是否有任何不良做法)。

结束

相关推荐

在Image.php图库的末尾添加“更多图库”页面

在许多杂志网站上,当你点击一个图库(在WP中使用image.php模板完成)时,最后一次点击会将你带到一个“更多图库”页面。我想实现类似的目标,并希望通过为“更多画廊”页面制作模板来实现这一目标。问题是我不知道如何让我的下一个/上一个链接在最后一张图片上显示上一个链接,该链接会重定向到“更多画廊”页面,而不是另一张图片。I figure the best way to do this would be to add a statement to the previous link which sends