短代码在列表中不起作用,因为它不是在第二个参数上具有有效函数回调的有效声明
尝试此操作以查看附加内容:
// shortcode data
add_filter("shortcode_list", function ($shortcode_list) {
$shortcode_list["slugA"] = array(
"html" => "test html 1",
);
return $shortcode_list;
});
add_filter("shortcode_list", function ($shortcode_list) {
$shortcode_list["slugB"] = array(
"html" => "test <strong>html 2 - [slugA]</strong>",
);
return $shortcode_list;
});
// shortcodes declaration
add_action("init", function () {
$shortcodes = apply_filters("shortcode_list", array());
foreach (array_keys($shortcodes) as $shortcode_slug) {
add_shortcode($shortcode_slug, "shortcode_callback");
};
});
// callback function
function shortcode_callback($atts, $content = "", $tag) {
// Build shortcode attributes.
$atts = shortcode_atts( array(
\'name\' => \'john\',
\'type\' => \'mammal\'
), $atts);
// shortcode data
$shortcodes = apply_filters("shortcode_list", array());
$shortcode = $shortcodes[$tag];
// construct result of the shortcode
$markup = "";
$markup .= \'<div class="\'.$atts[\'name\'].\'">\';
$markup .= \'<div>\';
// $shortcode should contain custom markup from the array.
$markup .= $shortcode[\'html\'];
//$markup .= do_shortcode($shortcode[\'html\']); // to replace shortcodes in the "html" data
$markup .= \'<pre>\';
$markup .= print_r($atts, TRUE);
$markup .= \'</pre>\';
$markup .= \'</div>\';
$markup .= \'</div>\';
return $markup;
}