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
Add a comment  | 

1 Answer 1

Reset to default 0

The 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