Show wrapper divs if query has results, then loop the results [closed]

Your second query was built up a little incorrectly – expanded and indented, it looks like this:

if ($portfolio->have_posts()) :
    while($portfolio->have_posts()) :
        $portfolio->the_post(); ?> 
        <div class="other-post col-sm-6 col-xs-12">
            <ul style="color:#f0ad4e;">
                <?php
                if($portfolio->have_posts()) :
                    while($portfolio->have_posts()) :
                        $portfolio->the_post();
                        ?>
                        <li><h2><a href="https://wordpress.stackexchange.com/questions/248440/<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a></h2></li>
                <?php endwhile; endif;  wp_reset_query(); ?>
            </ul>
        </div>

You’re starting the last query twice. I think what you’re trying to do is to only show the other-post-div when there are results for the second query, and then loop through them to make a list of the titles for the other posts. Here’s how you can achieve that:

if ($portfolio->have_posts()): ?> 
    <div class="other-post col-sm-6 col-xs-12">
        <ul style="color:#f0ad4e;">
        <?php while($portfolio->have_posts()) : $portfolio->the_post();?>
            <li><h2><a href="https://wordpress.stackexchange.com/questions/248440/<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a></h2></li>
        <?php endwhile; ?>
        </ul>
    </div>
<?php endif;  wp_reset_query(); ?>

Essentially what this does is check whether the query returns any results – if it does, show the container divs and create the while-loop to show the actual posts in the list.

Here’s the full changed code (with proper indentation for readability):

<?php
// Creating the widget
class wpb_box extends WP_Widget {
    function __construct() {
        parent::__construct(
            // Base ID of your widget
            'wpb_box',
            // Widget name will appear in UI
            __('ابزارک اختصاصی باکس مطالب', 'bigtheme'),
            // Widget description
            array( 'description' => __( 'ابزارک اختصاصی نمایش باکس مطالب دسته های مختلف در صفحه اصلی سایت', 'bigtheme' )
        ));
    }
    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {
        $name = apply_filters( 'widget_title', $instance['name'] );
        $link1 = apply_filters( 'widget_title', $instance['link1'] );
        $link = apply_filters( 'widget_title', $instance['link'] );
        $display = apply_filters( 'widget_title', $instance['display'] );
        $color = apply_filters( 'widget_title', $instance['color'] );
        // This is where you run the code and display the output
        ?>
        <article class="container-fluid">
            <h5 style="background:<?php echo $color ?>;"><a href="https://wordpress.stackexchange.com/questions/248440/<?php echo $link ?>" target="_blank"><?php echo $name ?></a></h5> 
            <?php  
            $portfolio = new WP_Query(array(
                            'post_status' =>'publish',
                            'post_type' =>'post',
                            'cat' =>''.$link1.'',
                            'posts_per_page' =>'1',
                            'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
            ));
            if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post(); ?> 
                <div class="first-post col-sm-6 col-xs-12">
                    <a href="https://wordpress.stackexchange.com/questions/248440/<?php the_permalink() ?>" target="_blank"><?php the_post_thumbnail('thumb',array( 'class'=> "img-responsive")); ?></a>
                    <h2><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></h2>
                    <?php the_excerpt(); ?>
                </div>  
            <?php endwhile; endif;  wp_reset_query(); ?>
            <?php  
            $portfolio = new WP_Query(array(
                            'post_status' =>'publish',
                            'post_type' =>'post',
                            'cat' =>''.$link1.'',
                            'offset' => '1',
                            'posts_per_page' =>''.$display.'',
                            'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1            
            ));
            if ($portfolio->have_posts()): ?> 
                <div class="other-post col-sm-6 col-xs-12">
                    <ul style="color:#f0ad4e;">
                        <?php while($portfolio->have_posts()) : $portfolio->the_post();?>
                            <li><h2><a href="https://wordpress.stackexchange.com/questions/248440/<?php the_permalink() ?>" target="_blank"><?php the_title(); ?></a></h2></li>
                        <?php endwhile; ?>
                    </ul>
                </div>
            <?php endif;  wp_reset_query(); ?>
        </article>
        <?php
        echo $args['after_widget'];
    }

    public function form( $instance ) {
        $name   = ( isset( $instance[ 'name' ] ) ) ? $instance[ 'name' ] : '';
        $link1  = ( isset( $instance[ 'link1' ] ) ) ? $instance[ 'link1' ] : '';
        $link   = ( isset( $instance[ 'link' ] ) ) ? $instance[ 'link' ] : '';
        $color  = ( isset( $instance[ 'color' ] ) ) ? $instance[ 'color' ] : '';
        $display    = ( isset( $instance[ 'display' ] ) ) ? $instance[ 'display' ] : '';
        ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'رنگ باکس مطالب:' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'عنوان باکس:' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'link1' ); ?>"><?php _e( 'دسته بندی مطلب' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'link1' ); ?>" name="<?php echo $this->get_field_name( 'link1' ); ?>" type="text" value="<?php echo esc_attr( $link1 ); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'لینک آرشیو' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
        </p>

        <p>
            <label><?php _e( 'نمایش توضیحات مطالب' ); ?></label>
            <select class="widefat" id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>">
                <option <?php selected( $instance['display'], 'block'); ?> value="block">بله</option> 
                <option <?php selected( $instance['display'], 'none'); ?> value="none">خیر</option> 
            </select>
        </p>
        <?php
    }
// Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : '';
        $instance['link1'] = ( ! empty( $new_instance['link1'] ) ) ? strip_tags( $new_instance['link1'] ) : '';
        $instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
        $instance['link2'] = ( ! empty( $new_instance['link2'] ) ) ? strip_tags( $new_instance['link2'] ) : '';
        $instance['link3'] = ( ! empty( $new_instance['link3'] ) ) ? strip_tags( $new_instance['link3'] ) : '';
        $instance['link4'] = ( ! empty( $new_instance['link4'] ) ) ? strip_tags( $new_instance['link4'] ) : '';
        $instance['link5'] = ( ! empty( $new_instance['link5'] ) ) ? strip_tags( $new_instance['link5'] ) : '';
        $instance['color'] = ( ! empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : '';
        $instance['display'] = ( ! empty( $new_instance['display'] ) ) ? strip_tags( $new_instance['display'] ) : '';
        $instance['source'] = ( ! empty( $new_instance['source'] ) ) ? strip_tags( $new_instance['source'] ) : '';
        $instance['time'] = ( ! empty( $new_instance['time'] ) ) ? strip_tags( $new_instance['time'] ) : '';
        return $instance;
    }
} // Class wpb_box ends here
// Register and load the widget
function wpb_box() {
register_widget( 'wpb_box' );
}
add_action( 'widgets_init', 'wpb_box' );
?>