<?php

/**
 * Must Read
 *
 * @package Vintuna
 */
if (!function_exists('vintuna_must_read')) :
    function vintuna_must_read($vintuna_home_section, $repeat_times)
    {
        $section_category_6 = esc_html(isset($vintuna_home_section->section_category_6) ? $vintuna_home_section->section_category_6 : '');
        $home_section_must_read = esc_html(isset($vintuna_home_section->home_section_must_read) ? $vintuna_home_section->home_section_must_read : '');
        $section_post_offset = isset($vintuna_home_section->section_post_offset) ? absint($vintuna_home_section->section_post_offset) : 0;
        $section_view_more_title = esc_html(isset($vintuna_home_section->section_view_more_title) ? $vintuna_home_section->section_view_more_title : '');

        $must_read_section = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 5, 'post__not_in' => get_option("sticky_posts"),'offset'=> $section_post_offset, 'category_name' => esc_html($section_category_6)));
        $category_id = get_cat_ID( $section_category_6 );
        $category_link = get_category_link( $category_id );
        if ($must_read_section->have_posts()) :
?>
            <div id="theme-block-<?php echo esc_attr($repeat_times); ?>" class="theme-block theme-must-read">
                <?php if ($home_section_must_read) { ?>
                    <header class="theme-block-header">
                        <h2 class="theme-block-title">
                            <?php echo esc_html($home_section_must_read); ?>
                        </h2>
                        <span class="block-title-dash"></span>
                        <?php if (!empty($category_link)&& !empty($section_view_more_title)){ ?>
                            <a href="<?php echo esc_url($category_link); ?>" class="theme-btn theme-primary-btn title-view-more"><?php echo esc_html($section_view_more_title);?><?php vintuna_theme_svg('arrow-right'); ?></a>
                        <?php }?>
                    </header>
                <?php } ?>


                <div class="theme-panel-body">
                    <?php
                    while ($must_read_section->have_posts()) {
                        $must_read_section->the_post();
                        $featured_image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'medium_large');
                        $featured_image = isset($featured_image[0]) ? $featured_image[0] : '';
                    ?>
                        <article class="news-article">
                            <div class="theme-article-image">
                                <div class="data-bg data-bg-medium" data-background='<?php echo esc_url($featured_image); ?>'></div>
                            </div>

                            <div class="article-content">
                                <h3 class="entry-title entry-title-medium">
                                    <a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a>
                                </h3>
                                <?php
                                the_excerpt();
                                ?>

                                <div class="entry-meta">
                                        <?php vintuna_posted_by(); ?>
                                </div>
                            </div>
                        </article>
                    <?php } ?>
                </div>
            </div>

<?php wp_reset_postdata();
        endif;
    }
endif; ?>