<aside class="sidebar left">

    <?php
    $args = array(
        'hide_empty' => 0,
        'orderby' => 'ID',
        'parent' => 0
    );
    $categories = get_categories($args);
    if (!empty($categories)) : ?>
        <div class="top-boxs rt rt-bg rt-shadow rt-10px">
            <div class="title rt">
                <h3 class="name rt-15 rt-bold right rt-444">دسته بندی وبلاگ</h3>
            </div>
            <ul class="rt rt-14">
                <?php foreach ($categories as $cat) :
                    $the_id = $cat->term_id; ?>
                    <li class="right rt-relative rt-z-index"><a href="<?php echo get_category_link($the_id); ?>" rel="category" title="<?php echo (!empty($cat->name)) ? $cat->name : ""; ?>" class="rt rt-444 rt-8px rt-medium"><?php echo (!empty($cat->name)) ? $cat->name : ""; ?></a></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <?php if (is_singular()) :
        global $post;
        $post_id = $post->ID;
        $post_terms = wp_get_object_terms($post_id, 'category', array('fields' => 'ids'));
        $per_page = get_option('posts_per_page');
        $per_page = (!empty($per_page)) ? $per_page : 10;
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => $per_page,
            'post__not_in' => array($post_id),
            'orderby' => 'rand',
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => $post_terms,
                )
            )
        );
        $wp_query = new WP_Query($args);
        if ($wp_query->have_posts()) : ?>
            <section class="rt-box rt rt-bg rt-shadow rt-10px rt-relative">
                <div class="title rt">
                    <h3 class="name rt-15 rt-bold right rt-444">مطالب مرتبط</h3>
                </div>
                <ul class="rt">
                    <?php while ($wp_query->have_posts()) : $wp_query->the_post();
                        global $post;
                        $post_permalink = get_the_permalink($post->ID);
                        $post_title = get_the_title($post->ID);
                        $post_date = jdate('j F Y', get_post_timestamp($post->ID));
                        $thumb_url = get_the_post_thumbnail_url($post->ID, 'blog_related');
                        if (empty($thumb_url)) $thumb_url = THEME_TEMPLATE_URL . '/assets/front/img/default-thumb.svg'; ?>
                        <li class="rt rt-relative">
                            <a href="<?php echo $post_permalink; ?>"><img src="<?php echo $thumb_url; ?>" alt="<?php echo $post_title; ?>" class="pic right"></a>
                            <div class="inside left">
                                <h2 class="rt rt-14"><a href="<?php echo $post_permalink; ?>" rel="bookmark" title="<?php echo $post_title; ?>" class="rt-444 rt rt-medium"><?php echo $post_title; ?></a></h2>
                                <span class="rt-13 rt-999"><i class="fa fa-calendar-day right rt-15"></i><?php echo $post_date; ?></span>
                            </div>
                        </li>
                    <?php endwhile; ?>
                </ul>
            </section>
        <?php endif;
        wp_reset_query(); ?>
    <?php endif; ?>

</aside>