/** * Start the element output. * * @see Walker::start_el() * * @since 3.0.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $item Menu item data object. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of arguments. @see wp_nav_menu() * @param int $id Current item ID. */ function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { $indent = $depth ? str_repeat("\t", $depth) : ''; $classes = empty($item->classes) ? array() : (array) $item->classes; $classes[] = 'menu-item-' . intval($item->ID); $barcelona_has_mega_menu = $item->menu_item_parent == '0' && $item->barcelonamegamenu == '1'; if ($barcelona_has_mega_menu) { $classes = array_unique(array_merge($classes, array('menu-item-mega-menu', 'menu-item-has-children'))); } /** * Filter the CSS class(es) applied to a menu item's list item element. * * @since 3.0.0 * @since 4.1.0 The `$depth` parameter was added. * * @param array $classes The CSS classes that are applied to the menu item's `<li>` element. * @param object $item The current menu item. * @param array $args An array of {@see wp_nav_menu()} arguments. * @param int $depth Depth of menu item. Used for padding. */ $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args, $depth)); $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : ''; /** * Filter the ID applied to a menu item's list item element. * * @since 3.0.1 * @since 4.1.0 The `$depth` parameter was added. * * @param string $menu_id The ID that is applied to the menu item's `<li>` element. * @param object $item The current menu item. * @param array $args An array of {@see wp_nav_menu()} arguments. * @param int $depth Depth of menu item. Used for padding. */ $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args, $depth); $id = $id ? ' id="' . esc_attr($id) . '"' : ''; $output .= $indent . '<li' . $id . $class_names . '>'; /** * Filter the HTML attributes applied to a menu item's anchor element. * * @since 3.6.0 * @since 4.1.0 The `$depth` parameter was added. * * @param array $atts { * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. * * @type string $title Title attribute. * @type string $target Target attribute. * @type string $rel The rel attribute. * @type string $href The href attribute. * } * @param object $item The current menu item. * @param array $args An array of {@see wp_nav_menu()} arguments. * @param int $depth Depth of menu item. Used for padding. */ $atts = array(); $atts['title'] = !empty($item->attr_title) ? $item->attr_title : ''; $atts['target'] = !empty($item->target) ? $item->target : ''; $atts['rel'] = !empty($item->xfn) ? $item->xfn : ''; $atts['href'] = !empty($item->url) ? $item->url : ''; $atts = apply_filters('nav_menu_link_attributes', $atts, $item, $args, $depth); $attributes = ''; foreach ($atts as $attr => $value) { if (!empty($value)) { $value = 'href' === $attr ? esc_url($value) : esc_attr($value); $attributes .= ' ' . $attr . '="' . $value . '"'; } } $item_output = $args->before; $item_output .= '<a' . $attributes . '>'; /** This filter is documented in wp-includes/post-template.php */ $item_output .= $args->link_before . do_shortcode($item->title) . $args->link_after; $item_output .= '</a>'; $item_output .= $args->after; /** * Filter a menu item's starting output. * * The menu item's starting output only includes `$args->before`, the opening `<a>`, * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is * no filter for modifying the opening and closing `<li>` for a menu item. * * @since 3.0.0 * * @param string $item_output The menu item's starting HTML output. * @param object $item Menu item data object. * @param int $depth Depth of menu item. Used for padding. * @param array $args An array of {@see wp_nav_menu()} arguments. */ $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args); if ($barcelona_has_mega_menu) { $args = array('cat' => $item->object_id, 'posts_per_page' => -1, 'post_status' => 'publish', 'ignore_sticky_posts' => 1); /* * Posts Ordering */ switch (barcelona_get_option('mm_orderby')) { case 'views': $args['orderby'] = 'meta_value_num'; $args['meta_key'] = '_barcelona_views'; break; case 'comments': $args['orderby'] = 'comment_count'; break; case 'votes': $args['orderby'] = 'meta_value_num'; $args['meta_key'] = '_barcelona_vote_up'; break; case 'random': $args['orderby'] = 'rand'; break; case 'posts': $args['orderby'] = 'post__in'; break; default: $args['orderby'] = 'date'; } $args['order'] = barcelona_get_option('mm_order') != 'asc' ? 'DESC' : 'ASC'; $latest_posts = new WP_Query($args); $post_ids = array(); $i = 0; if ($latest_posts->have_posts()) { $barcelona_mm_posts = '<div class="posts-wrapper row">'; while ($latest_posts->have_posts()) { $latest_posts->the_post(); $post_ids[] = get_the_ID(); $barcelona_mm_posts .= '<div class="col col-xs-3"> <div class="post-summary post-format-' . sanitize_html_class(barcelona_get_post_format()) . '"> <div class="post-image"> <a href="' . esc_url(get_the_permalink()) . '" title="' . esc_attr(get_the_title()) . '">' . barcelona_psum_overlay(false) . barcelona_get_thumbnail('barcelona-sm') . '</a> </div> <div class="post-details"> <h2 class="post-title"> <a href="' . esc_url(get_the_permalink()) . '">' . esc_html(get_the_title()) . '</a> </h2> <ul class="post-meta no-sep"> <li class="post-date"> <span class="fa fa-clock-o"></span>' . get_the_time(BARCELONA_DATE_FORMAT) . ' </li> </ul> </div> </div> </div>'; $i++; if ($i == 4) { break; } } wp_reset_postdata(); $barcelona_mm_posts .= '</div>'; // Mega Menu Bottom $barcelona_mm_bottom = '<div class="mm-bottom row"><div class="col col-sm-9">'; // Show popular tags under mega menu if (barcelona_get_option('show_tags_under_mm') == 'on') { $tags = array(); foreach ($post_ids as $k) { $post_tags = wp_get_post_tags($k, array('orderby' => 'count', 'order' => 'DESC')); foreach ($post_tags as $tag) { $tags[$tag->term_id] = $tag; } } if (count($tags)) { usort($tags, function ($a, $b) { return $a->count < $b->count; }); $tags = array_slice($tags, 0, 5); $barcelona_mm_bottom .= '<div class="tag-list"><div class="title">' . esc_html__('Popular Tags:', 'barcelona') . '</div><div class="list">'; foreach ($tags as $tag) { $barcelona_mm_bottom .= '<a href="' . esc_url(get_term_link($tag)) . '">' . esc_html($tag->name) . '</a>,'; } $barcelona_mm_bottom = rtrim($barcelona_mm_bottom, ',') . '</div></div>'; } } // Add "see all" link to the bottom $barcelona_mm_bottom .= '</div><div class="col col-sm-3"><div class="see-all"><a href="' . esc_url($item->url) . '">' . esc_html__('See All', 'barcelona') . '</a></div></div>'; // Close .mm-bottom $barcelona_mm_bottom .= '</div>'; $output .= '<div class="mega-menu">' . $barcelona_mm_posts . $barcelona_mm_bottom . '</div>'; } // have_posts } }
<article class="post-summary post-format-<?php echo sanitize_html_class(barcelona_get_post_format()); ?> clearfix"> <div class="post-image"> <a href="<?php echo esc_url(get_the_permalink()); ?> " title="<?php echo esc_attr(get_the_title()); ?> "> <?php barcelona_psum_overlay(); barcelona_thumbnail('barcelona-md'); ?> </a> </div><!-- .post-image --> <div class="post-details"> <h2 class="post-title"> <a href="<?php echo esc_url(get_the_permalink()); ?> "><?php echo esc_html(get_the_title()); ?>
public function widget($args, $instance) { $barcelona_cache = array(); if (!$this->is_preview()) { $barcelona_cache = wp_cache_get('barcelona-posts', 'widget'); } if (!is_array($barcelona_cache)) { $barcelona_cache = array(); } if (!isset($args['widget_id'])) { $args['widget_id'] = $this->id; } if (isset($barcelona_cache[$args['widget_id']])) { echo wp_kses_post($barcelona_cache[$args['widget_id']]); return; } ob_start(); $barcelona_params = array('no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true); if (empty($instance['offset']) || !($barcelona_params['offset'] = absint($instance['offset']))) { $barcelona_params['offset'] = 0; } if (empty($instance['number']) || !($barcelona_params['posts_per_page'] = absint($instance['number']))) { $barcelona_params['posts_per_page'] = 5; } if (!empty($instance['category'])) { $barcelona_params['cat'] = $instance['category']; } if (!empty($instance['filter_posts'])) { $barcelona_params['post__in'] = array_values(array_filter(array_map(function ($v) { $v = trim($v); if (!is_numeric($v) || $v <= 0) { $v = false; } return $v; }, explode(',', $instance['filter_posts'])), function ($v) { return is_numeric($v); })); if (empty($barcelona_params['post__in'])) { unset($barcelona_params['post__in']); } } switch ($instance['orderby']) { case 'views': $barcelona_params['orderby'] = 'meta_value_num'; $barcelona_params['meta_key'] = '_barcelona_views'; break; case 'comments': $barcelona_params['orderby'] = 'comment_count'; break; case 'votes': $barcelona_params['orderby'] = 'meta_value_num'; $barcelona_params['meta_key'] = '_barcelona_vote_up'; break; case 'random': $barcelona_params['orderby'] = 'rand'; break; case 'posts': $barcelona_params['orderby'] = 'post__in'; break; default: $barcelona_params['orderby'] = 'date'; } $barcelona_params['order'] = $instance['order'] != 'asc' ? 'DESC' : 'ASC'; $barcelona_query = new WP_Query($barcelona_params); if ($barcelona_query->have_posts()) { echo wp_kses_post($args['before_widget']); if (!empty($instance['title'])) { echo wp_kses_post($args['before_title']) . esc_html($instance['title']) . wp_kses_post($args['after_title']); } ?> <div class="posts-box posts-box-sidebar row"> <?php while ($barcelona_query->have_posts()) { $barcelona_query->the_post(); ?> <div class="col col-md-12 col-sm-6 col-xs-12"> <div class="post-summary post-format-<?php echo sanitize_html_class(barcelona_get_post_format()); ?> psum-horizontal psum-small clearfix"> <div class="post-image"> <a href="<?php echo esc_url(get_the_permalink()); ?> " title="<?php echo esc_attr(get_the_title()); ?> "> <?php barcelona_psum_overlay(); barcelona_thumbnail('barcelona-md'); ?> </a> </div> <div class="post-details"> <h2 class="post-title"> <a href="<?php echo esc_url(get_the_permalink()); ?> "><?php echo esc_html(get_the_title()); ?> </a> </h2> <ul class="post-meta no-sep"> <li class="post-date"> <span class="fa fa-clock-o"></span><?php echo esc_html(get_the_time(BARCELONA_DATE_FORMAT)); ?> </li> </ul> </div> </div><!-- .post-summary --> </div> <?php } ?> </div> <?php echo wp_kses_post($args['after_widget']); wp_reset_postdata(); } if (!$this->is_preview()) { $barcelona_cache[$args['widget_id']] = ob_get_flush(); wp_cache_set('barcelona-posts', $barcelona_cache, 'widget'); } else { ob_end_flush(); } }