Check for the main query from the template

Use in_the_loop(), it returns true if we are in the main loop, false if we are not, like on a custom query loop

Return: (bool) True if caller is within loop, false if loop hasn’t started or ended.

The difference between is_main_query() and in_the_loop() is that is_main_query() uses WP_Query::is_main_query() which in turn uses the global $wp_the_query. $wp_is_query stores the main query, that is why is_main_query() always returns true inside the loop. You should also read this post

So, in short, you can do

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <?php if ( in_the_loop() ) {
        // Do some staff
    } else {
        // Do other staff

    } ?>

</article>