我如何在 wordpress 中创建简码?
How can i create a shortcode in wordpress?
我有下面的代码,我希望它显示使用
实现的内容
[insert_dos]*Content for dos here*[/insert_dos]
[insert_donts]*Content for dos here*[/insert_donts]
DOS
此处为dos内容
不要做
这里有不该做的内容
代码正在尝试使用
// Shortcode for dos
function insert_dos_func( $atts,$content ) {
extract( shortcode_atts( array(
'content' => 'Hello World',
), $atts ) );
return '<h2>DOs</h2>';
return '<div>' . $content . '</div>';
}
add_shortcode( 'insert_dos', 'insert_dos_func' );
// Shortcode for don'ts
function insert_donts_func( $atts ) {
extract( shortcode_atts( array(
'content' => 'Hello World',
), $atts ) );
return "<h2>DON'Ts</h2>";
return "<div>" . $content . "</div>";
}
add_shortcode( 'insert_donts', 'insert_donts_func' );
您要面对的第一个问题是在单个函数中使用多个 return 语句。第一个 return 之后的任何内容都不会被执行。
第二个问题是您传递内容的方式。您的属性数组中有一个名为 content
的元素。如果您 运行 在该数组上提取,它将覆盖您的短代码回调的 $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' );
我有下面的代码,我希望它显示使用
实现的内容[insert_dos]*Content for dos here*[/insert_dos]
[insert_donts]*Content for dos here*[/insert_donts]
DOS
此处为dos内容
不要做
这里有不该做的内容
代码正在尝试使用
// Shortcode for dos
function insert_dos_func( $atts,$content ) {
extract( shortcode_atts( array(
'content' => 'Hello World',
), $atts ) );
return '<h2>DOs</h2>';
return '<div>' . $content . '</div>';
}
add_shortcode( 'insert_dos', 'insert_dos_func' );
// Shortcode for don'ts
function insert_donts_func( $atts ) {
extract( shortcode_atts( array(
'content' => 'Hello World',
), $atts ) );
return "<h2>DON'Ts</h2>";
return "<div>" . $content . "</div>";
}
add_shortcode( 'insert_donts', 'insert_donts_func' );
您要面对的第一个问题是在单个函数中使用多个 return 语句。第一个 return 之后的任何内容都不会被执行。
第二个问题是您传递内容的方式。您的属性数组中有一个名为 content
的元素。如果您 运行 在该数组上提取,它将覆盖您的短代码回调的 $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' );