admin管理员组文章数量:1387360
I have a wordpress site and I am using a plugin that uses shortcode as a widget to pull recent blog posts. In the column I am getting an error that reads:
Warning: Illegal string offset 'title' in /home3/xxxxx/xxxx/wp-content/plugins/recent-blogs-shortcode/recent-blogs-shortcode.php on line 44
So the Title would be the blog post title, but I am just having trouble on how to fix it.
add_shortcode( 'recent-blogs', 'cp_sidebar_recent_blogs_shortcode');
function cp_sidebar_recent_blogs_shortcode( $atts, $content = null ) {
// shortcode_atts will only allow us to supply 1 default; for the horizontal layout submitted with no title, use a different default
$title_default = '';
if ($atts['title'] == 'Related Posts' && $atts['layout'] == 'horizontal' ) {
$title_default = 'Related Posts';
}
else {
$title_default = 'Recent Posts';
}
I have a wordpress site and I am using a plugin that uses shortcode as a widget to pull recent blog posts. In the column I am getting an error that reads:
Warning: Illegal string offset 'title' in /home3/xxxxx/xxxx/wp-content/plugins/recent-blogs-shortcode/recent-blogs-shortcode.php on line 44
So the Title would be the blog post title, but I am just having trouble on how to fix it.
add_shortcode( 'recent-blogs', 'cp_sidebar_recent_blogs_shortcode');
function cp_sidebar_recent_blogs_shortcode( $atts, $content = null ) {
// shortcode_atts will only allow us to supply 1 default; for the horizontal layout submitted with no title, use a different default
$title_default = '';
if ($atts['title'] == 'Related Posts' && $atts['layout'] == 'horizontal' ) {
$title_default = 'Related Posts';
}
else {
$title_default = 'Recent Posts';
}
Share
Improve this question
asked Apr 13, 2020 at 20:25
Kevin RubelKevin Rubel
1
1 Answer
Reset to default 0The answer here is assumed the user equipped with the following knowledge
- PHP and know how to find reference from php manual online
- WP built-in shortcode_atts() - Combine user attributes with known attributes and fill in defaults when needed.
I guess you haven't written the shortcode in a proper form.
Illegal string offset 'title' means $atts['title'] does not exist.
It does not exist because when shortcode is being called, title
is not given
To resolve this, you need to set default values and then combine it with user attributes(the attributes supply when anyone call the shortcode).
Here is a recommended writing style for a 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
}
本文标签: phpI am having an issue with this shortcode plugin Warning Illegal string offset 39title39
版权声明:本文标题:php - I am having an issue with this shortcode plugin.. Warning: Illegal string offset 'title' 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744570592a2613316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论