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

1 Answer 1

Reset to default 1

You 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

  1. $atts - array
  2. $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