ADD DYNAMIC attribute for category to accordion

Ok, you were basically almost there, all you had to do was output the slug value in two places in the sprint().

Answer edited/added to:

<ul class="accordion">
<?php
    $categories = get_categories( array(
        'orderby'   => 'name',
        'order'     => 'DESC',
        'offset'    => 1
    ) );
    $cat_array = array();
    foreach( $categories as $category ) {
        $category_link = sprintf( 
            '<li><a href="https://wordpress.stackexchange.com/questions/364110/%1$s" data-class=".%2$s" alt="%2$s">%3$s</a></li>',
            esc_url( get_category_link( $category->term_id ) ),
            esc_attr( sprintf( __( '%s', 'textdomain' ), $category->slug ) ),
            esc_html( $category->slug )
        );
        echo sprintf( esc_html__( '%s', 'textdomain' ), $category_link );
        $cat_array[] = $category->term_id;
    }
?>
</ul>

    <?php
    global $post;
    if( !empty( $cat_array ) ) :
        foreach( $cat_array as $cat ) :
            $category = get_term( $cat, 'category' );
            $cat_slug = $category->slug;
            echo '<div class="col-lg-4 s3_shuffle_image ' . $cat_slug . '">';
            $postslist = get_posts( array(
                'posts_per_page'    => 3,
                'cat'               => $cat,
            ) );
            if( $postslist ) :
                foreach( $postslist as $post ) :
                    setup_postdata( $post ); ?>
                    <h2><a href="https://wordpress.stackexchange.com/questions/364110/<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <?php the_excerpt(); ?>
                <?php
                endforeach; 
                wp_reset_postdata();
            endif;
            echo '</div>';
        endforeach;
    endif;
    ?>

So in the line where you return the <li> formatted string (sprint()), you are simply adding your data-class attribute and repeating the token (%2$s) to the second item your returning.

You did also have an error on this line where you placed a period outside of the quotes so PHP tried to execute it instead of treating it as part of the string.

esc_attr( sprintf( __( '%s',. 'textdomain' ), $category->slug )),

Please note that you should also match all instances of the generic textdomain with the actual textdomain of your theme… if you look in your style.css at the top you should see something like this:

/*
Theme Name: The Name of the Theme
Theme URI: http://domain.com
Author: your name or some other developer's name
Author URI: http://domain.com
Description: A description for the theme.
Version: 1.0 (updated version number)
License: GNU General Public License v2 or later
License URI: LICENSE
Text Domain: make a custom text domain if there isn't one
Tags: custom-background, custom-logo, custom-menu, featured-images, translation-ready
*/

If the Text Domain: is there, make sure you’re using that when you write other functions, if it’s not there, add it and make it something unique. Fill out all those lines with something unique and accurate to your theme, keep the version number updated, etc.

To explain the edit/addition, what we’re doing is, as we run the for each on all of the categories that were returned, we’re adding them to an array we created before the for each: $cat_array = array();. The last item in the foreach() now ads each category’s ID to at the array: $cat_array[] = $category->term_id;

Then, inside the s3_shuffle_image div, we run a foreach() on the array we just created. Within that foreach we run a `get_posts()’ function that calls for the 3 most recent posts of each category based on the ID.

Then, if that get_posts() query returns any results, we run another foreach and spit out the content/data from each of the returned posts inside of the accordion div.

There are two issues I see. $cat_array is an array of IDs, not objects, so the ‘cat’ query arg should just be ‘cat’ => $cat,
The other is when you use setup_postdata( $post ), you must explicitly declare global $post;