此处的答案假设用户具备以下知识
PHP,并知道如何从PHP联机手册中找到参考shortcode_atts() - 将用户属性与已知属性相结合,并在需要时填写默认值我想你没有用正确的形式写下这个短代码。
非法字符串偏移量“title”表示$atts[“title”]不存在。它不存在,因为在调用shortcode时,title
未给出
To resolve this, you need to set default values and then combine it with user attributes(the attributes supply when anyone call the shortcode).
下面是一个推荐的短码写作风格
add_shortcode( \'recent-blogs\', \'cp_sidebar_recent_blogs_shortcode\');
function cp_sidebar_recent_blogs_shortcode( $atts, $content = null ) {
// the following is default value + merge value from shortcode calling,
// it can prevent errors in case any attribute is empty such as title is not defined
extract(shortcode_atts(array(
\'title\' => \'\',
\'layout\' => \'\',
), $attr));
// with extract() you don\'t need to explicitly create a $title_default
// because extract() will create $title with value \'\'
if ( $atts[\'title\'] == \'Related Posts\' && $atts[\'layout\'] == \'horizontal\' ) {
$title = \'Related Posts\';
}
else {
$title = \'Recent Posts\';
}
// ... your other code continue
}