0

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' );

1 Answer 1

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) 
1
  • Thank you! That fixed it
    – Dab0416
    Commented Apr 3 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.