if (is_array($terms) || is_object($terms)) { ?> <ul class="card__tags"> <?php foreach ($terms as $term) { $icon_url = listable_get_term_icon_url($term->term_id); $attachment_id = listable_get_term_icon_id($term->term_id); if (empty($icon_url)) { continue; } ?> <li> <div class="card__tag"> <div class="pin__icon"> <?php listable_display_image($icon_url, '', true, $attachment_id); ?> </div> </div> </li> <?php } ?> </ul> <?php } ?> <div class="address card__address" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress"> <div itemprop="streetAddress"> <span class="address__street"><?php echo trim(get_post_meta($post->ID, 'geolocation_street', true), '');
function listable_display_frontpage_listing_categories($default_count = 7) { $term_list = array(); //first let's do only one query and get all the terms - we will reuse this info to avoid multiple queries $query_args = array('orderby' => 'count', 'order' => 'DESC', 'hide_empty' => false, 'hierarchical' => true, 'pad_counts' => true); $all_terms = get_terms('job_listing_category', $query_args); //bail if there was an error if (is_wp_error($all_terms)) { return; } //now create an array with the category slug as key so we can reference/search easier $all_categories = array(); foreach ($all_terms as $key => $term) { $all_categories[$term->slug] = $term; } $categories = get_post_meta(get_the_ID(), 'frontpage_listing_categories', true); $custom_category_labels = array(); //if we have received a list of categories to display (their slugs and optional label), use that if (!empty($categories)) { $categories = explode(',', $categories); foreach ($categories as $key => $category) { if (strpos($category, '(') !== false) { $category = explode('(', $category); $term_slug = trim($category[0]); if (substr($category[1], -1, 1) == ')') { $custom_category_labels[$term_slug] = trim(substr($category[1], 0, -1)); } if (array_key_exists($term_slug, $all_categories)) { $term_list[] = $all_categories[$term_slug]; } } else { $term_slug = trim($category); if (array_key_exists($term_slug, $all_categories)) { $term_list[] = $all_categories[$term_slug]; } } } } else { //it seems we will have to figure out ourselves what categories to display $term_list = array_slice($all_categories, 0, $default_count); } if ($term_list) { echo '<span class="cta-text">' . esc_html__('Or browse the highlights', 'listable') . '</span>'; } foreach ($term_list as $key => $term) { if (!$term || is_array($term) && isset($term['invalid_taxonomy'])) { continue; } ?> <a href="<?php echo esc_url(get_term_link($term)); ?> "> <?php $url = listable_get_term_icon_url($term->term_id); $attachment_id = listable_get_term_icon_id($term->term_id); if (!empty($url)) { ?> <span class="cat__icon"><?php listable_display_image($url, '', true, $attachment_id); ?> </span> <?php } ?> <span class="cat__text"><?php echo isset($custom_category_labels[$term->slug]) ? $custom_category_labels[$term->slug] : $term->name; ?> </span> </a> <?php } }
public function widget($args, $instance) { global $post; $placeholders = $this->get_placeholder_strings(); //only put in the default title if the user hasn't saved anything in the database e.g. $instance is empty (as a whole) $title = apply_filters('widget_title', empty($instance) ? $placeholders['title'] : $instance['title'], $instance, $this->id_base); $subtitle = empty($instance) ? $placeholders['subtitle'] : $instance['subtitle']; $number_of_items = empty($instance['number_of_items']) ? $this->defaults['number_of_items'] : $instance['number_of_items']; $orderby = empty($instance['orderby']) ? $this->defaults['orderby'] : $instance['orderby']; $regions_slug = empty($instance['regions_slug']) ? $this->defaults['regions_slug'] : $instance['regions_slug']; $term_list = array(); $custom_region_labels = array(); //first let's do only one query and get all the terms - we will reuse this info to avoid multiple queries $query_args = array('order' => 'DESC', 'hide_empty' => false, 'hierarchical' => true, 'pad_counts' => true); if (!empty($orderby) && is_string($orderby)) { $query_args['orderby'] = $orderby; } $all_terms = get_terms('job_listing_region', $query_args); //bail if there was an error if (is_wp_error($all_terms)) { return; } //now create an array with the category slug as key so we can reference/search easier $all_regions = array(); foreach ($all_terms as $key => $term) { $all_regions[$term->slug] = $term; } echo $args['before_widget']; //if we have received a list of regions to display (their slugs and optional label), use that if (!empty($regions_slug) && is_string($regions_slug)) { $regions = explode(',', $regions_slug); foreach ($regions as $key => $region) { if (strpos($region, '(') !== false) { $region = explode('(', $region); $term_slug = trim($region[0]); if (substr($region[1], -1, 1) == ')') { $custom_region_labels[$term_slug] = trim(substr($region[1], 0, -1)); } if (array_key_exists($term_slug, $all_regions)) { $term_list[] = $all_regions[$term_slug]; } } else { $term_slug = trim($region); if (array_key_exists($term_slug, $all_regions)) { $term_list[] = $all_regions[$term_slug]; } } } //now if the user has chosen to sort these according to the number of posts, we should do that // since we will, by default, respect the order of the regions he has used if ('count' == $orderby) { // Define the custom sort function function sort_by_post_count($a, $b) { return $a->count < $b->count; } // Sort the multidimensional array usort($term_list, "sort_by_post_count"); } elseif ('rand' == $orderby) { //randomize things a bit if this is what the user ordered shuffle($term_list); } } else { //it seems we will have to figure out ourselves what categories to display if (!($number_of_items = intval($number_of_items))) { $number_of_items = 4; } $term_list = array_slice($all_regions, 0, $number_of_items); } if (!empty($term_list)) { ?> <h3 class="widget_title widget_title--frontpage"> <?php echo $title; if (!empty($subtitle)) { ?> <span class="widget_subtitle widget_subtitle--frontpage"> <?php echo $subtitle; ?> </span> <?php } ?> </h3> <div class="categories-wrap--widget"> <ul class="categories--widget"> <?php foreach ($term_list as $key => $term) { if (!$term) { continue; } $icon_url = listable_get_term_icon_url($term->term_id); $image_url = listable_get_term_image_url($term->term_id, 'listable-card-image'); $attachment_id = listable_get_term_icon_id($term->term_id); $image_src = ''; if (!empty($image_url)) { $image_src = $image_url; } else { $thumbargs = array('posts_per_page' => 1, 'post_type' => 'job_listing', 'orderby' => 'rand', 'meta_key' => 'main_image', 'tax_query' => array(array('taxonomy' => 'job_listing_region', 'field' => 'name', 'terms' => $term->name))); $latest_thumb = new WP_Query($thumbargs); if ($latest_thumb->have_posts()) { //get the first image in the listing's gallery or the featured image, if present $image_ID = listable_get_post_image_id($latest_thumb->post->ID); $image_src = ''; if (!empty($image_ID)) { $image = wp_get_attachment_image_src($image_ID, 'medium'); $image_src = $image[0]; } } } ?> <li <?php echo empty($icon_url) ? 'class="no-icon"' : ''; ?> > <div class="category-cover" style="background-image: url(<?php echo $image_src; ?> )"> <a href="<?php echo esc_url(get_term_link($term)); ?> "> <?php if (!empty($icon_url)) { ?> <div class="category-icon"> <?php listable_display_image($icon_url, '', true, $attachment_id); ?> <span class="category-count"> <?php echo $term->count; ?> </span> </div> <?php } ?> <span class="category-text"><?php echo isset($custom_region_labels[$term->slug]) ? $custom_region_labels[$term->slug] : $term->name; ?> </span> </a> </div> </li> <?php } ?> </ul><!-- .categories --> </div><!-- .categories-wrap --> <?php } echo $args['after_widget']; }