Php 如何在wordpress中创建短代码?
我有下面的代码,我希望它使用Php 如何在wordpress中创建短代码?,php,wordpress,shortcode,Php,Wordpress,Shortcode,我有下面的代码,我希望它使用 [insert_dos]*Content for dos here*[/insert_dos] [insert_donts]*Content for dos here*[/insert_donts] Dos 这里是dos的内容 不要 满足于这里的禁忌 我试图使用的代码 //dos的短代码 函数insert_dos_func($atts,$content){ 提取(短码)附件(数组)( “内容”=>“你好,世界”, )美元(附件);; 返回“DOs”; 返回“.$
[insert_dos]*Content for dos here*[/insert_dos]
[insert_donts]*Content for dos here*[/insert_donts]
Dos
这里是dos的内容
不要
满足于这里的禁忌
我试图使用的代码
//dos的短代码
函数insert_dos_func($atts,$content){
提取(短码)附件(数组)(
“内容”=>“你好,世界”,
)美元(附件);;
返回“DOs”;
返回“.$content.”;
}
添加_短代码('insert_dos','insert_dos_func');
//不允许的短代码
函数insert\u donts\u func($atts){
提取(短码)附件(数组)(
“内容”=>“你好,世界”,
)美元(附件);;
返回“不要”;
返回“$content.”;
}
添加快捷码('insert_donts','insert_donts_func');
您将面临的第一个问题是在单个函数中使用多个return语句。第一次返回后的任何内容都不会执行
第二个问题是你传递内容的方式。属性数组中有一个名为content
的元素。如果在该数组上运行extract,它将覆盖快捷码回调的$content
参数
function insert_dos_func( $atts, $content ) {
/**
* This is going to get attributes and set defaults.
*
* Example of a shortcode attribute:
* [insert_dos my_attribute="testing"]
*
* In the code below, if my_attribute isn't set on the shortcode
* it's going to default to Hello World. Extract will make it
* available as $my_attribute instead of $atts['my_attribute'].
*
* It's here purely as an example based on the code you originally
* posted. $my_attribute isn't actually used in the output.
*/
extract( shortcode_atts( array(
'my_attribute' => 'Hello World',
), $atts ) );
// All content is going to be appended to a string.
$output = '';
$output .= '<h2>DOs</h2>';
$output .= '<div>' . $content . '</div>';
// Once we've built our output string, we're going to return it.
return $output;
}
add_shortcode( 'insert_dos', 'insert_dos_func' );
函数插入函数($atts,$content){
/**
*这将获取属性并设置默认值。
*
*短代码属性的示例:
*[插入我的属性=“测试”]
*
*在下面的代码中,如果没有在短代码上设置my_属性
*它将默认为Hello World。Extract将使其生效
*可用作$my_属性,而不是$atts['my_属性']。
*
*这里纯粹是一个基于您最初编写的代码的示例
*已发布。$my_属性实际上未在输出中使用。
*/
提取(短码)附件(数组)(
“我的属性”=>“你好,世界”,
)美元(附件);;
//所有内容都将被追加到字符串中。
$output='';
$output.='DOs';
$output.=''.$content'.';
//一旦我们构建了输出字符串,我们将返回它。
返回$output;
}
添加_短代码('insert_dos','insert_dos_func');
2返回将不起作用。。。一旦你点击了退出函数的第一个,下一个就永远不会得到我所需要的执行。有很多
function insert_dos_func( $atts, $content ) {
/**
* This is going to get attributes and set defaults.
*
* Example of a shortcode attribute:
* [insert_dos my_attribute="testing"]
*
* In the code below, if my_attribute isn't set on the shortcode
* it's going to default to Hello World. Extract will make it
* available as $my_attribute instead of $atts['my_attribute'].
*
* It's here purely as an example based on the code you originally
* posted. $my_attribute isn't actually used in the output.
*/
extract( shortcode_atts( array(
'my_attribute' => 'Hello World',
), $atts ) );
// All content is going to be appended to a string.
$output = '';
$output .= '<h2>DOs</h2>';
$output .= '<div>' . $content . '</div>';
// Once we've built our output string, we're going to return it.
return $output;
}
add_shortcode( 'insert_dos', 'insert_dos_func' );