我在互联网上找到了一个短代码示例:
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>
但我不知道如何才能在短代码中返回多行代码(以保持代码干净)。有什么建议吗?
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\');
(请告诉我该代码中是否有任何不良做法)。