<?php
$recipe_blogz_options = recipe_blogz_theme_options();

$bottomblog_sec_title = $recipe_blogz_options['bottomblog_sec_title'];
$bottomblog_category = $recipe_blogz_options['bottomblog_category'];

?>
<div class="blog-section section-spacing">
    <div class="container">
        <div class="row">

                <div class="section-headings custom-section-title">
                        <?php
                        if ($bottomblog_sec_title)
                            echo '<h2>' . esc_html($bottomblog_sec_title) . '</h2>';

                        ?>
                </div>

                    <?php 

                    if($bottomblog_category){
                        $args = array(
                            'post_type' => 'post',
                            'posts_per_page' => 4,
                            'post_status' => 'publish',
                            'order' => 'desc',
                            'orderby' => 'menu_order date',
                            'tax_query' =>
                                array(
                                    array(
                                        'taxonomy' => 'category',
                                        'field' => 'slug',
                                        'terms' => $bottomblog_category,
                                    ),
                                ),
                        );
                    }
                    else{
                        $args = array(
                            'post_type' => 'post',
                            'posts_per_page' => 4,
                            'post_status' => 'publish',
                            'order' => 'desc',
                            'orderby' => 'menu_order date',
                        );
                    }

        $query = new \WP_Query($args);

         if ($query->have_posts()):
            ?>
            <div class="blog-element">
                        <?php
                        while ($query->have_posts()) : $query->the_post();
                            global $post;
                            $recipe_blogz_post_format = get_post_format($post->ID);
                            $recipe_blogz_post_thumbnail_id = get_post_thumbnail_id(get_the_ID());
                            $recipe_blogz_image = wp_get_attachment_image_src($recipe_blogz_post_thumbnail_id, 'recipe-blogz-blog-thumbnail-img');
                            $recipe_blogz_content = get_the_content();
  

                            if (!empty($recipe_blogz_image)) {
                                $recipe_blogz_image_style = "style='background-image:url(" . esc_url($recipe_blogz_image[0]) . ")'";
                            } else {
                                $recipe_blogz_image_style = '';
                            }

                            
                                ?>
                                <article class="blog-post">
                                    <?php if (!empty($recipe_blogz_image)) { ?>
                                    <img src="<?php echo esc_url($recipe_blogz_image[0]); ?>" alt="" />
                                    <?php } ?>
                                    <div class="title">
                                        <h2><a href="<?php echo esc_url(get_the_permalink()); ?>"><?php the_title(); ?></a></h2>
                                    </div>
                                    <ul class="post-meta">
                                        <li class="post-cat">
                                                                <?php 
                                                                $recipe_blogz_categories = get_the_category();
                                                                if ( ! empty( $recipe_blogz_categories ) ) {
                                                                    echo '<a href="' . esc_url( get_category_link( $recipe_blogz_categories[0]->term_id ) ) . '">' . esc_html( $recipe_blogz_categories[0]->name ) . '</a>';
                                                                }?>
                                                                </li>
                                              <li class="meta-date"><a href="<?php echo esc_url(recipe_blogz_archive_link($post)); ?>"><time class="entry-date published" datetime="<?php echo esc_url(recipe_blogz_archive_link($post)); ?>"><?php echo esc_html(the_time( get_option( 'date_format' ) )); ?></time></a>
                                              </li>
                                        </ul>
                                    </article>
                                <?php   endwhile;
                        wp_reset_postdata(); ?>
                    </div>
        <?php endif;


                        ?>
                </div>
            </div>
</div>
