admin管理员组文章数量:1122846
I have a shortcode that I have been using for awhile that is suddenly causing a warning in WordPress 6.5 the error is "Warning: Array to string conversion in ... on line 109" - line 109 is below at $content .= ob_get_clean();
What I am finding on researching this warning is that I need to implode or join the array but I am not sure how that applies to this. Any ideas?
function my_annoucement_shortcode($content) {
global $post;
$args = array(
'post_type' => array( 'announcement' ),
'nopaging' => true,
'posts_per_page' => '1',
);
$ann_query = new WP_Query( $args );
if ( $ann_query->have_posts() ) {
ob_start();
while ( $ann_query->have_posts() ) { $ann_query->the_post();
echo '<div class="announcement-content">';
the_content();
echo '</div>';
}
wp_reset_query();
$content .= ob_get_clean();
return $content;
}
}
add_shortcode( 'announcements', 'my_annoucement_shortcode' );
I have a shortcode that I have been using for awhile that is suddenly causing a warning in WordPress 6.5 the error is "Warning: Array to string conversion in ... on line 109" - line 109 is below at $content .= ob_get_clean();
What I am finding on researching this warning is that I need to implode or join the array but I am not sure how that applies to this. Any ideas?
function my_annoucement_shortcode($content) {
global $post;
$args = array(
'post_type' => array( 'announcement' ),
'nopaging' => true,
'posts_per_page' => '1',
);
$ann_query = new WP_Query( $args );
if ( $ann_query->have_posts() ) {
ob_start();
while ( $ann_query->have_posts() ) { $ann_query->the_post();
echo '<div class="announcement-content">';
the_content();
echo '</div>';
}
wp_reset_query();
$content .= ob_get_clean();
return $content;
}
}
add_shortcode( 'announcements', 'my_annoucement_shortcode' );
Share
Improve this question
asked Apr 3, 2024 at 16:56
Dab0416Dab0416
32 bronze badges
1 Answer
Reset to default 1You are getting that error because in the function parameter you are passing $content as 1st parameter but as per add_shortcode
callback function it accepts 2 argument
- $atts - array
- $content - string
It is basically trying to concat $atts param which is an array hence you are getting warning.
Just update your code by
function my_annoucement_shortcode($atts,$content)
本文标签: Array to String Conversion warning in shortcode
版权声明:本文标题:Array to String Conversion warning in shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311701a1934832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论