Example #1
0
        /**
         * Function to Creating widget front-end. This is where the action happens
         *
         * @access public
         * @since 1.0.0
         *
         * @param array $args widget setting
         * @param array $instance saved values
         * @return array
         *
         */
        public function widget($args, $instance)
        {
            $supermag_sidebar_id = $args['id'];
            if (isset($instance['supermag_cat'])) {
                $supermag_cat = $instance['supermag_cat'];
            } else {
                $supermag_cat = -1;
            }
            if (isset($instance['supermag_enable_first_featured'])) {
                $supermag_enable_first_featured = $instance['supermag_enable_first_featured'];
            } else {
                $supermag_enable_first_featured = 0;
            }
            if (1 == $supermag_enable_first_featured) {
                $supermag_number = 4;
            } else {
                $supermag_number = 6;
            }
            $supermag_other_class = '';
            if ('supermag-home' != $supermag_sidebar_id) {
                if (1 != $supermag_enable_first_featured) {
                    $supermag_number = 3;
                }
                if ('supermag-sidebar') {
                    $supermag_other_class = 'supermag-except-home';
                }
            } else {
                $supermag_other_class = '';
            }
            /*column layout*/
            if (isset($instance['supermag_post_col_layout'])) {
                $supermag_post_col_layout = absint($instance['supermag_post_col_layout']);
            } else {
                $supermag_post_col_layout = 0;
            }
            /**
             * Filter the arguments for the Recent Posts widget.
             *
             * @since 1.0.0
             *
             * @see WP_Query
             *
             */
            $supermag_cat_post_args = array('posts_per_page' => $supermag_number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true);
            if (-1 != $supermag_cat) {
                $supermag_cat_post_args['cat'] = $supermag_cat;
                $supermag_title = get_cat_name($supermag_cat);
            } else {
                $supermag_title = __('Recent Posts', 'supermag');
            }
            $supermag_featured_query = new WP_Query($supermag_cat_post_args);
            if ($supermag_featured_query->have_posts()) {
                ?>
                <?php 
                echo $args['before_widget'];
                echo $args['before_title'] . esc_html($supermag_title) . $args['after_title'];
                $supermag_post_col_layout_class = '';
                if (1 == $supermag_post_col_layout) {
                    $supermag_post_col_layout_class = 'sm-col-post-type-2';
                }
                ?>
                <ul class="<?php 
                echo esc_attr($supermag_post_col_layout_class);
                ?>
 featured-entries-col featured-entries <?php 
                echo esc_attr($supermag_other_class);
                ?>
 featured-col-posts <?php 
                echo esc_attr($supermag_sidebar_id);
                ?>
">
                    <?php 
                $supermag_featured_index = 1;
                while ($supermag_featured_query->have_posts()) {
                    $supermag_featured_query->the_post();
                    if (1 == $supermag_featured_index && 1 == $supermag_enable_first_featured) {
                        $thumb = 'medium';
                        $supermag_list_classes = 'acme-col-3 featured-post-main';
                        $supermag_sidebar_no_thumbnail = 'no-image-660-365.png';
                        $supermag_words = 48;
                    } else {
                        $thumb = 'thumbnail';
                        $supermag_list_classes = 'acme-col-3';
                        $supermag_sidebar_no_thumbnail = 'no-image-500-280.png';
                        $supermag_words = 16;
                        if (1 == $supermag_post_col_layout) {
                            $supermag_words = 8;
                        }
                    }
                    ?>
                        <li class="<?php 
                    echo esc_attr($supermag_list_classes);
                    ?>
">
                            <figure class="widget-image">
                                <?php 
                    if (has_post_thumbnail()) {
                        $post_thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), $thumb);
                    } else {
                        $post_thumb[0] = get_template_directory_uri() . '/assets/img/' . $supermag_sidebar_no_thumbnail;
                    }
                    ?>
                                <a href="<?php 
                    the_permalink();
                    ?>
">
                                    <img src="<?php 
                    echo esc_url($post_thumb[0]);
                    ?>
" alt="<?php 
                    the_title_attribute();
                    ?>
" title="<?php 
                    the_title_attribute();
                    ?>
" />
                                </a>
                            </figure>
                            <div class="featured-desc">
                                <div class="above-entry-meta">
                                    <?php 
                    $archive_year = get_the_date('Y');
                    $archive_month = get_the_date('m');
                    $archive_day = get_the_date('d');
                    ?>
                                    <span>
                                        <a href="<?php 
                    echo esc_url(get_day_link($archive_year, $archive_month, $archive_day));
                    ?>
">
                                            <i class="fa fa-calendar"></i>
                                            <?php 
                    echo esc_html(get_the_date('F d, Y'));
                    ?>
                                        </a>
                                    </span>
                                    <span>
                                        <a href="<?php 
                    echo esc_url(get_author_posts_url(get_the_author_meta('ID')));
                    ?>
" title="<?php 
                    echo esc_attr(get_the_author());
                    ?>
">
                                            <i class="fa fa-user"></i>
                                            <?php 
                    echo esc_html(get_the_author());
                    ?>
                                        </a>
                                    </span>
                                    <span>
                                        <?php 
                    comments_popup_link('<i class="fa fa-comment"></i>0', '<i class="fa fa-comment"></i>1', '<i class="fa fa-comment"></i>%');
                    ?>
                                    </span>
                                </div>
                                <a href="<?php 
                    the_permalink();
                    ?>
">
                                    <h4 class="title">
                                        <?php 
                    the_title();
                    ?>
                                    </h4>
                                </a>
                                <?php 
                    $content = supermag_words_count(get_the_excerpt(), $supermag_words);
                    echo '<div class="details">' . esc_html($content) . '</div>';
                    ?>
                                <div class="below-entry-meta">
                                    <?php 
                    supermag_list_category();
                    ?>
                                </div>
                            </div>
                        </li>
                    <?php 
                    if (1 == $supermag_enable_first_featured) {
                        if (1 == $supermag_featured_index) {
                            if (1 == $supermag_post_col_layout) {
                                echo '<div class="sm-col-post-type-2-beside">';
                            } else {
                                echo '<div class="clearfix"></div>';
                            }
                        }
                        if (($supermag_featured_index - 1) % 3 == 0) {
                            echo '<div class="clearfix visible-lg"></div>';
                        }
                        if (($supermag_featured_index - 1) % 2 == 0) {
                            echo '<div class="clearfix visible-sm"></div>';
                        }
                    } else {
                        if ($supermag_featured_index % 3 == 0) {
                            echo '<div class="clearfix visible-lg"></div>';
                        }
                        if ($supermag_featured_index % 2 == 0) {
                            echo '<div class="clearfix visible-sm"></div>';
                        }
                    }
                    $supermag_featured_index++;
                }
                if (1 == $supermag_post_col_layout && 1 == $supermag_enable_first_featured) {
                    echo '</div>';
                }
                ?>
                </ul>
                <?php 
                echo $args['after_widget'];
                echo "<div class='clearfix'></div>";
                // Reset the global $the_post as this query will have stomped on it
                wp_reset_postdata();
            }
        }
    function supermag_related_post_below($post_id)
    {
        global $supermag_customizer_all_values;
        if (0 == $supermag_customizer_all_values['supermag-show-related']) {
            return;
        }
        $categories = get_the_category($post_id);
        if ($categories) {
            $category_ids = array();
            foreach ($categories as $category) {
                $category_ids[] = $category->term_id;
            }
            ?>
            <h2 class="widget-title">
                <?php 
            _e('Related posts', 'supermag');
            ?>
            </h2>
            <ul class="featured-entries-col featured-entries featured-col-posts featured-related-posts">
                <?php 
            $supermag_cat_post_args = array('category__in' => $category_ids, 'post__not_in' => array($post_id), 'post_type' => 'post', 'posts_per_page' => 3, 'post_status' => 'publish', 'ignore_sticky_posts' => true);
            $supermag_featured_query = new WP_Query($supermag_cat_post_args);
            while ($supermag_featured_query->have_posts()) {
                $supermag_featured_query->the_post();
                $supermag_sidebar_no_thumbnail = 'no-image-500-280.png';
                ?>
                    <li class="acme-col-3">
                        <figure class="widget-image">
                            <?php 
                if (has_post_thumbnail()) {
                    $post_thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail');
                } else {
                    $post_thumb[0] = get_template_directory_uri() . '/assets/img/' . $supermag_sidebar_no_thumbnail;
                }
                ?>
                            <a href="<?php 
                the_permalink();
                ?>
">
                                <img src="<?php 
                echo esc_url($post_thumb[0]);
                ?>
" alt="<?php 
                esc_attr(the_title_attribute());
                ?>
" title="<?php 
                esc_attr(the_title_attribute());
                ?>
" />
                            </a>
                        </figure>
                        <div class="featured-desc">
                            <div class="above-entry-meta">
                                <?php 
                $archive_year = get_the_date('Y');
                $archive_month = get_the_date('m');
                $archive_day = get_the_date('d');
                ?>
                                <span>
                                    <a href="<?php 
                echo esc_url(get_day_link($archive_year, $archive_month, $archive_day));
                ?>
">
                                        <i class="fa fa-calendar"></i>
                                        <?php 
                echo esc_html(get_the_date('F d, Y'));
                ?>
                                    </a>
                                </span>
                                <span>
                                    <a href="<?php 
                echo esc_url(get_author_posts_url(get_the_author_meta('ID')));
                ?>
" title="<?php 
                echo esc_attr(get_the_author());
                ?>
">
                                        <i class="fa fa-user"></i>
                                        <?php 
                echo esc_html(get_the_author());
                ?>
                                    </a>
                                </span>
                                <span>
                                    <?php 
                comments_popup_link('<i class="fa fa-comment"></i>0', '<i class="fa fa-comment"></i>1', '<i class="fa fa-comment"></i>%');
                ?>
                                </span>
                            </div>
                            <a href="<?php 
                the_permalink();
                ?>
">
                                <h4 class="title">
                                    <?php 
                the_title();
                ?>
                                </h4>
                            </a>
                            <?php 
                $content = supermag_words_count(get_the_excerpt());
                echo '<div class="details">' . esc_html($content) . '</div>';
                ?>
                            <div class="below-entry-meta">
                                <?php 
                supermag_list_category();
                ?>
                            </div>
                        </div>
                    </li>
                    <?php 
            }
            wp_reset_query();
            ?>
            </ul>
            <div class="clearfix"></div>
            <?php 
        }
    }
    function supermag_display_feature_slider()
    {
        global $supermag_customizer_all_values;
        $supermag_feature_cat = $supermag_customizer_all_values['supermag-feature-cat'];
        if (0 != $supermag_feature_cat) {
            $supermag_cat_post_args = array('cat' => $supermag_feature_cat, 'posts_per_page' => 5, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true);
            $slider_query = new WP_Query($supermag_cat_post_args);
            if ($slider_query->have_posts()) {
                while ($slider_query->have_posts()) {
                    $slider_query->the_post();
                    if (has_post_thumbnail()) {
                        $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'medium');
                    } else {
                        $image_url[0] = get_template_directory_uri() . '/assets/img/no-image-660-365.png';
                    }
                    ?>
                    <li>
                        <a href="<?php 
                    the_permalink();
                    ?>
">
                            <img src="<?php 
                    echo esc_url($image_url[0]);
                    ?>
"/>
                        </a>
                        <div class="slider-desc">
                            <div class="above-slider-details">
                                <?php 
                    $archive_year = get_the_date('Y');
                    $archive_month = get_the_date('m');
                    $archive_day = get_the_date('d');
                    ?>
                                <a href="<?php 
                    echo esc_url(get_day_link($archive_year, $archive_month, $archive_day));
                    ?>
">
                                    <i class="fa fa-calendar"></i>
                                    <?php 
                    echo esc_html(get_the_date('F d, Y'));
                    ?>
                                </a>
                                <a href="<?php 
                    echo esc_url(get_author_posts_url(get_the_author_meta('ID')));
                    ?>
" title="<?php 
                    echo esc_attr(get_the_author());
                    ?>
">
                                    <i class="fa fa-user"></i>
                                    <?php 
                    echo esc_html(get_the_author());
                    ?>
                                </a>
                                <?php 
                    comments_popup_link('<i class="fa fa-comment"></i> 0', '<i class="fa fa-comment"></i> 1', '<i class="fa fa-comment"></i> %');
                    ?>
                            </div>
                            <div class="slider-details">
                                <a href="<?php 
                    the_permalink();
                    ?>
">
                                    <div class="slide-title">
                                        <?php 
                    the_title();
                    ?>
                                    </div>
                                    <?php 
                    $content = supermag_words_count(get_the_excerpt(), 12);
                    echo '<div class="slide-caption">' . esc_html($content) . '</div>';
                    ?>
                                </a>
                            </div>
                            <div>
                                <?php 
                    supermag_list_category();
                    ?>
                            </div>
                        </div>
                    </li>
                <?php 
                }
                wp_reset_postdata();
                ?>
            <?php 
            }
            ?>
        <?php 
        } else {
            supermag_default_slider();
        }
    }