[英]How to add a closing shortcode to Wordpress template file?
好的,所以你可以在WP模板文件中添加短代碼,例如:
<?php echo do_shortcode('[my_awesome_shortcode]'); ?>
但是如果短代碼打算包含這樣的內容呢:
[my_awesome_shortcode]
Hey, this is the content within the awesome shortcode.
[/my_awesome_shortcode]
我有點不確定如何將它放入模板文件中。
對我有用的解決方案是將短代碼組合成一個字符串,所以
<?php echo do_shortcode('[my_awesome_shortcode]<h1>Hello world</h1>[/my_awesome_shortcode]'); ?>
將工作!
如果您想要執行一長串短代碼命令,請為它們創建一個單獨的字符串
$shortcodes = '[row]';
$shortcodes .= '[column width="2/3"]';
$shortcodes .= 'Content';
$shortcodes .= '[/column]';
$shortcodes .= '[column width="1/3"]';
$shortcodes .= 'More Content';
$shortcodes .= '[/column]';
$shortcodes .= '[/row]';
然后像這樣執行整個事情
<?php echo do_shortcode($shortcodes); ?>
根據http://codex.wordpress.org/Shortcode_API#Enclosing_vs_self-closing_shortcodes
將$content = null
添加到快捷方式功能應該可以解決問題:
function my_awesome_shortcode_func( $atts, $content = null ) {
return '<awesomeness>' . $content . '</awesomeness>';
}
add_shortcode( 'my_awesome_shortcode', 'my_awesome_shortcode_func' );
以便:
[my_awesome_shortcode]
Hey, this is the content within the awesome shortcode.
[/my_awesome_shortcode]
會導致:
<awesomeness>Hey, this is the content within the awesome shortcode.</awesomeness>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.