function setUp() { global $wp_rewrite; parent::setUp(); set_current_screen('front'); $GLOBALS['wp_the_query'] = new WP_Query(); $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; $wp_rewrite->init(); $wp_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/'); create_initial_taxonomies(); register_taxonomy('testtax', 'post', array('public' => true)); $wp_rewrite->flush_rules(); $this->tag_id = $this->factory->tag->create(array('slug' => 'tag-slug')); $this->cat_id = $this->factory->category->create(array('slug' => 'cat-slug')); $this->tax_id = $this->factory->term->create(array('taxonomy' => 'testtax', 'slug' => 'tax-slug')); $this->tax_id2 = $this->factory->term->create(array('taxonomy' => 'testtax', 'slug' => 'tax-slug2')); $this->post_id = $this->factory->post->create(); wp_set_object_terms($this->post_id, $this->cat_id, 'category'); wp_set_object_terms($this->post_id, array($this->tax_id, $this->tax_id2), 'testtax'); $this->cat = get_term($this->cat_id, 'category'); _make_cat_compat($this->cat); $this->tag = get_term($this->tag_id, 'post_tag'); $this->uncat = get_term_by('slug', 'uncategorized', 'category'); _make_cat_compat($this->uncat); add_action('pre_get_posts', array($this, 'pre_get_posts_tax_category_tax_query')); }
function get_category_by_slug( $slug ) { $category = get_term_by('slug', $slug, 'category'); if ( $category ) _make_cat_compat($category); return $category; }
/** * Validate _make_cat_compat function */ function test__make_cat_compat() { // create Test Categories and Array Representations $testcat_array = array('slug' => 'testmcc', 'name' => 'Test MCC', 'description' => 'Category Test'); $testcat = self::factory()->category->create_and_get($testcat_array); $testcat_array['term_id'] = $testcat->term_id; $testcat2_array = array('slug' => 'testmcc', 'name' => 'Test MCC', 'description' => 'Category Test', 'parent' => $testcat->term_id); $testcat2 = self::factory()->category->create_and_get($testcat2_array); $testcat2_array['term_id'] = $testcat2->term_id; // unset properties to enable validation of object unset($testcat->cat_ID); unset($testcat->category_count); unset($testcat->category_description); unset($testcat->cat_name); unset($testcat->category_nicename); unset($testcat->category_parent); unset($testcat2->cat_ID); unset($testcat2->category_count); unset($testcat2->category_description); unset($testcat2->cat_name); unset($testcat2->category_nicename); unset($testcat2->category_parent); // make Compatible _make_cat_compat($testcat); _make_cat_compat($testcat2); _make_cat_compat($testcat_array); _make_cat_compat($testcat2_array); // Validate Compatibility Object $this->assertEquals($testcat->cat_ID, $testcat->term_id); $this->assertEquals($testcat->category_count, $testcat->count); $this->assertEquals($testcat->category_description, $testcat->description); $this->assertEquals($testcat->cat_name, $testcat->name); $this->assertEquals($testcat->category_nicename, $testcat->slug); $this->assertEquals($testcat->category_parent, $testcat->parent); // Validate Compatibility Object with Parent $this->assertEquals($testcat->cat_ID, $testcat->term_id); $this->assertEquals($testcat->category_count, $testcat->count); $this->assertEquals($testcat->category_description, $testcat->description); $this->assertEquals($testcat->cat_name, $testcat->name); $this->assertEquals($testcat->category_nicename, $testcat->slug); $this->assertEquals($testcat->category_parent, $testcat->parent); // Validate Compatibility Array $this->assertEquals($testcat_array['cat_ID'], $testcat_array['term_id']); $this->assertEquals($testcat_array['category_count'], $testcat_array['count']); $this->assertEquals($testcat_array['category_description'], $testcat_array['description']); $this->assertEquals($testcat_array['cat_name'], $testcat_array['name']); $this->assertEquals($testcat_array['category_nicename'], $testcat_array['slug']); $this->assertEquals($testcat_array['category_parent'], $testcat_array['parent']); // Validate Compatibility Array with Parent $this->assertEquals($testcat_array['cat_ID'], $testcat_array['term_id']); $this->assertEquals($testcat_array['category_count'], $testcat_array['count']); $this->assertEquals($testcat_array['category_description'], $testcat_array['description']); $this->assertEquals($testcat_array['cat_name'], $testcat_array['name']); $this->assertEquals($testcat_array['category_nicename'], $testcat_array['slug']); $this->assertEquals($testcat_array['category_parent'], $testcat_array['parent']); }
function widget($args, $instance) { extract($args); $title = apply_filters('xt_widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); global $xt; if ($xt->is_daogou) { $cats = get_the_terms(null, 'daogou_category'); if (!$cats || is_wp_error($cats)) { $cats = array(); } $cats = array_values($cats); foreach (array_keys($cats) as $key) { _make_cat_compat($cats[$key]); } $cids = array(); if (!empty($cats)) { foreach ($cats as $cat) { $cids[] = $cat->cat_ID; } } $cids = implode(',', $cids); $show = intval($instance['show']); $count = intval($instance['count']); $sortOrder = $instance['sortOrder']; $isDaogouList = $layout == 'span9' && $show == 2; if ($isDaogouList) { echo str_replace('xt-widget-blog', 'xt-widget-daogou', $before_widget); } else { echo $before_widget; } echo '<div class="hd"><h4 class="xt-bd-l" style="' . (empty($title) ? 'display:none' : '') . '"><span>' . $title . '</span></h4></div><div class="bd">'; $_params = array('post_type' => 'daogou', 'posts_per_page' => $count, 'orderby' => $sortOrder); if (!empty($cids)) { $_params['tax_query'] = array(array("taxonomy" => "daogou_category", "field" => "id", "terms" => $cids)); } query_posts($_params); if ($isDaogouList) { _xt_widget_blog_daogou($layout); } else { if ($show == 0) { echo '<ul class="xt-bd-list unstyled clearfix">'; } else { echo '<ul class="media-list clearfix">'; } $_post_count = 0; while (have_posts()) { the_post(); _xt_widget_blog_li($show, $_post_count); $_post_count++; } echo '</ul>'; } echo '</div>'; echo $after_widget; } }
/** * Retrieve post categories. * * @since 0.71 * @uses $post * * @param int $id Optional, default to current post ID. The post ID. * @return array */ function get_the_category($id = false) { $categories = get_the_terms($id, 'category'); if (!$categories) { $categories = array(); } $categories = array_values($categories); foreach (array_keys($categories) as $key) { _make_cat_compat($categories[$key]); } // Filter name is plural because we return alot of categories (possibly more than #13237) not just one return apply_filters('get_the_categories', $categories); }
/** * Retrieve list of category objects. * * If you change the type to 'link' in the arguments, then the link categories * will be returned instead. Also all categories will be updated to be backwards * compatible with pre-2.3 plugins and themes. * * @since 2.1.0 * @see get_terms() Type of arguments that can be changed. * @link http://codex.wordpress.org/Function_Reference/get_categories * * @param string|array $args Optional. Change the defaults retrieving categories. * @return array List of categories. */ function get_categories($args = '') { $defaults = array('taxonomy' => 'category'); $args = wp_parse_args($args, $defaults); $taxonomy = apply_filters('get_categories_taxonomy', $args['taxonomy'], $args); // Back compat if (isset($args['type']) && 'link' == $args['type']) { _deprecated_argument(__FUNCTION__, '3.0', ''); $taxonomy = $args['taxonomy'] = 'link_category'; } $categories = (array) get_terms($taxonomy, $args); foreach (array_keys($categories) as $k) { _make_cat_compat($categories[$k]); } return $categories; }
/** * Retrieve post categories. * * @since 0.71 * * @param int $id Optional, default to current post ID. The post ID. * @return array */ function get_the_category($id = false) { $categories = get_the_terms($id, 'category'); if (!$categories || is_wp_error($categories)) { $categories = array(); } $categories = array_values($categories); foreach (array_keys($categories) as $key) { _make_cat_compat($categories[$key]); } /** * Filter the array of categories to return for a post. * * @since 3.1.0 * * @param array $categories An array of categories to return for the post. */ return apply_filters('get_the_categories', $categories); }
function get_the_category($id = false) { global $post, $term_cache, $blog_id; $id = (int) $id; if (!$id) { $id = (int) $post->ID; } $categories = get_object_term_cache($id, 'category'); if (false === $categories) { $categories = wp_get_object_terms($id, 'category'); } if (!empty($categories)) { usort($categories, '_usort_terms_by_name'); } else { $categories = array(); } foreach (array_keys($categories) as $key) { _make_cat_compat($categories[$key]); } return $categories; }
/** * Retrieve queried object. * * If queried object is not set, then the queried object will be set from * the category, tag, taxonomy, posts page, single post, page, or author * query variable. After it is set up, it will be returned. * * @since 1.5.0 * @access public * * @return object */ public function get_queried_object() { if (isset($this->queried_object)) { return $this->queried_object; } $this->queried_object = null; $this->queried_object_id = null; if ($this->is_category || $this->is_tag || $this->is_tax) { if ($this->is_category) { if ($this->get('cat')) { $term = get_term($this->get('cat'), 'category'); } elseif ($this->get('category_name')) { $term = get_term_by('slug', $this->get('category_name'), 'category'); } } elseif ($this->is_tag) { if ($this->get('tag_id')) { $term = get_term($this->get('tag_id'), 'post_tag'); } elseif ($this->get('tag')) { $term = get_term_by('slug', $this->get('tag'), 'post_tag'); } } else { // For other tax queries, grab the first term from the first clause. $tax_query_in_and = wp_list_filter($this->tax_query->queried_terms, array('operator' => 'NOT IN'), 'NOT'); if (!empty($tax_query_in_and)) { $queried_taxonomies = array_keys($tax_query_in_and); $matched_taxonomy = reset($queried_taxonomies); $query = $tax_query_in_and[$matched_taxonomy]; if ($query['terms']) { if ('term_id' == $query['field']) { $term = get_term(reset($query['terms']), $matched_taxonomy); } else { $term = get_term_by($query['field'], reset($query['terms']), $matched_taxonomy); } } } } if (!empty($term) && !is_wp_error($term)) { $this->queried_object = $term; $this->queried_object_id = (int) $term->term_id; if ($this->is_category && 'category' === $this->queried_object->taxonomy) { _make_cat_compat($this->queried_object); } } } elseif ($this->is_post_type_archive) { $post_type = $this->get('post_type'); if (is_array($post_type)) { $post_type = reset($post_type); } $this->queried_object = get_post_type_object($post_type); } elseif ($this->is_posts_page) { $page_for_posts = get_option('page_for_posts'); $this->queried_object = get_post($page_for_posts); $this->queried_object_id = (int) $this->queried_object->ID; } elseif ($this->is_singular && !empty($this->post)) { $this->queried_object = $this->post; $this->queried_object_id = (int) $this->post->ID; } elseif ($this->is_author) { $this->queried_object_id = (int) $this->get('author'); $this->queried_object = get_userdata($this->queried_object_id); } return $this->queried_object; }
/** * Aliases wp_insert_category() with minimal args. * * If you want to update only some fields of an existing category, call this * function with only the new values set inside $catarr. * * @since 2.0.0 * * @param array $catarr The 'cat_ID' value is required. All other keys are optional. * @return int|bool The ID number of the new or updated Category on success. Zero or FALSE on failure. */ function wp_update_category($catarr) { $cat_ID = (int) $catarr['cat_ID']; if (isset($catarr['category_parent']) && $cat_ID == $catarr['category_parent']) { return false; } // First, get all of the original fields $category = get_term($cat_ID, 'category', ARRAY_A); _make_cat_compat($category); // Escape data pulled from DB. $category = wp_slash($category); // Merge old and new fields with new fields overwriting old ones. $catarr = array_merge($category, $catarr); return wp_insert_category($catarr); }
/** * Display or retrieve the HTML list of brands. * * @return string HTML content only if 'echo' argument is 0. */ function dhwc_list_brands($args = '') { $defaults = array('show_option_all' => '', 'show_option_none' => __('No brands'), 'orderby' => 'name', 'order' => 'ASC', 'style' => 'list', 'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1, 'child_of' => 0, 'feed' => '', 'feed_type' => '', 'feed_image' => '', 'exclude' => '', 'exclude_tree' => '', 'current_brand' => 0, 'hierarchical' => true, 'title_li' => __('Brands'), 'echo' => 1, 'depth' => 0, 'taxonomy' => 'product_brand'); $r = wp_parse_args($args, $defaults); if (!isset($r['pad_counts']) && $r['show_count'] && $r['hierarchical']) { $r['pad_counts'] = true; } if (true == $r['hierarchical']) { $r['exclude_tree'] = $r['exclude']; $r['exclude'] = ''; } if (!isset($r['class'])) { $r['class'] = 'brand' == $r['taxonomy'] ? 'brands' : $r['taxonomy']; } extract($r); if (!taxonomy_exists($taxonomy)) { return false; } $brans = (array) get_terms($taxonomy, $args); foreach (array_keys($brans) as $k) { _make_cat_compat($brans[$k]); } $output = ''; if ($title_li && 'list' == $style) { $output = '<li class="' . esc_attr($class) . '">' . $title_li . '<ul>'; } if (empty($brans)) { if (!empty($show_option_none)) { if ('list' == $style) { $output .= '<li>' . $show_option_none . '</li>'; } else { $output .= $show_option_none; } } } else { if (!empty($show_option_all)) { $posts_page = 'page' == get_option('show_on_front') && get_option('page_for_posts') ? get_permalink(get_option('page_for_posts')) : home_url('/'); $posts_page = esc_url($posts_page); if ('list' == $style) { $output .= "<li><a href='{$posts_page}'>{$show_option_all}</a></li>"; } else { $output .= "<a href='{$posts_page}'>{$show_option_all}</a>"; } } if (empty($r['current_brand']) && is_tax()) { $current_term_object = get_queried_object(); if ($current_term_object && $r['taxonomy'] === $current_term_object->taxonomy) { $r['current_brand'] = get_queried_object_id(); } } if ($hierarchical) { $depth = $r['depth']; } else { $depth = -1; } // Flat. $output .= walk_category_tree($brans, $depth, $r); } if ($title_li && 'list' == $style) { $output .= '</ul></li>'; } $output = apply_filters('dhwc_list_brands', $output, $args); if ($echo) { echo $output; } else { return $output; } }
/** * Retrieve queried object. * * If queried object is not set, then the queried object will be set from * the category, tag, taxonomy, posts page, single post, page, or author * query variable. After it is set up, it will be returned. * * @since 1.5.0 * @access public * * @return object */ function get_queried_object() { if (isset($this->queried_object)) { return $this->queried_object; } $this->queried_object = null; $this->queried_object_id = 0; if ($this->is_category || $this->is_tag || $this->is_tax) { $tax_query_in_and = wp_list_filter($this->tax_query->queries, array('operator' => 'NOT IN'), 'NOT'); $query = reset($tax_query_in_and); if ('term_id' == $query['field']) { $term = get_term(reset($query['terms']), $query['taxonomy']); } else { $term = get_term_by($query['field'], reset($query['terms']), $query['taxonomy']); } if ($term && !is_wp_error($term)) { $this->queried_object = $term; $this->queried_object_id = (int) $term->term_id; if ($this->is_category) { _make_cat_compat($this->queried_object); } } } elseif ($this->is_post_type_archive) { $this->queried_object = get_post_type_object($this->get('post_type')); } elseif ($this->is_posts_page) { $page_for_posts = get_option('page_for_posts'); $this->queried_object =& get_page($page_for_posts); $this->queried_object_id = (int) $this->queried_object->ID; } elseif ($this->is_singular && !is_null($this->post)) { $this->queried_object = $this->post; $this->queried_object_id = (int) $this->post->ID; } elseif ($this->is_author) { $this->queried_object_id = (int) $this->get('author'); $this->queried_object = get_userdata($this->queried_object_id); } return $this->queried_object; }
function TS_VCSC_GetTheCategoryByTax($id = false, $tcat = 'category') { $categories = get_the_terms($id, $tcat); if (!$categories || is_wp_error($categories)) { $categories = array(); } $categories = array_values($categories); foreach (array_keys($categories) as $key) { _make_cat_compat($categories[$key]); } return apply_filters('get_the_categories', $categories); }
/** * Filter the array of categories to return for a post. * * @since 4.4.0 Added `$id` parameter. * @param bool|int $id ID of the post. * @return mixed|void */ function get_post_category($id = false) { $categories = get_the_terms($id, 'category'); if (!$categories || is_wp_error($categories)) { $categories = array(); } $categories = array_values($categories); foreach (array_keys($categories) as $key) { _make_cat_compat($categories[$key]); } $category_print = ''; if ($categories) { $category_print = _e('in', 'clean-blog'); foreach ($categories as $category) { $category_link = get_category_link($category->cat_ID); // Get the URL of this category $category_name = $category->name; $category_print .= ' <a href="' . $category_link . '" title="' . $category_name . '">' . $category_name . '</a>,'; } $category_print = rtrim($category_print, ','); // remove last comma from string } return $category_print; }
public static function get_category($category, $output = OBJECT, $filter = 'raw') { static $caches = []; $cache_id = md5(json_encode(func_get_args())); if (isset($caches[$cache_id])) { return $caches[$cache_id]; } $category = get_term($category, 'category', $output, $filter); if (is_wp_error($category)) { $caches[$cache_id] = $category; return $category; } _make_cat_compat($category); $caches[$cache_id] = $category; return $category; }
/** * Insert a new category. * @param array $catarr The new category to insert. * @return int The ID of the new category, or 0 if there was an error. */ function wp_insert_category($catarr) { global $wp_test_expectations; if (is_object($catarr)) { $catarr = (array) $catarr; } if (is_array($catarr)) { if (empty($catarr['cat_ID'])) { $max_id = 1; foreach ($wp_test_expectations['categories'] as $category) { if (isset($category->cat_ID)) { $max_id = max($max_id, $category->cat_ID + 1); } } add_category($max_id, (object) _make_cat_compat($catarr)); return $max_id; } } return 0; }
function yoast_get_term_parents($id, $term_type = 'category', $link = FALSE, $separator = '/', $nicename = FALSE) { $chain = ''; $category = get_term($id, $term_type, $output, $filter); if (is_wp_error($category)) { return $category; } _make_cat_compat($category); $parent = $category; if (is_wp_error($parent)) { return $parent; } if ($nicename) { $name = $parent->slug; } else { $name = $parent->cat_name; } $term_child = get_term_children($id, $term_type); if ($parent->parent && $parent->parent != $parent->term_id) { $chain .= '<a href="' . get_term_link($parent->parent, $term_type) . '">' . yoast_get_term_parents($parent->parent, $term_type, true, $separator, $nicename) . '</a>' . $separator; } $chain .= bold_or_not($name); return $chain; }
/** * Retrieve product categories. Copied from the corresponding wordpress function * * @since 3.8.0 * * @param int $id Mandatory, the product ID * @return array */ function get_the_product_category($id) { $id = (int) $id; $categories = wpsc_get_product_terms($id, 'wpsc_product_category'); if (!empty($categories)) { usort($categories, '_usort_terms_by_name'); } else { $categories = array(); } foreach ((array) array_keys($categories) as $key) { _make_cat_compat($categories[$key]); } return $categories; }
/** * Retrieve galleryCategory object by galleryCategory slug. * * @since 2.3.0 * * @param string $slug The galleryCategory slug. * @return object GalleryCategory data object */ function get_gallery_category_by_slug($slug) { $category = get_term_by('slug', $slug, 'ds-gallery-category'); if ($category) { _make_cat_compat($category); } return $category; }
/** * Retrieve product categories. Copied from the corresponding wordpress function * * @since 3.8.0 * * @param int $id Mandatory, the product ID * @return array */ function get_the_product_category($id) { $id = (int) $id; $categories = get_object_term_cache($id, 'wpsc_product_category'); if (false === $categories) { $categories = wp_get_object_terms($id, 'wpsc_product_category'); wp_cache_add($id, $categories, 'product_category_relationships'); } if (!empty($categories)) { usort($categories, '_usort_terms_by_name'); } else { $categories = array(); } foreach ((array) array_keys($categories) as $key) { _make_cat_compat($categories[$key]); } return $categories; }
/** * Retrieve testimonial_category object by category slug. * * @param string $slug The category slug. * @return object Category data object */ function get_testimonial_category_by_slug($slug) { $category = get_term_by('slug', $slug, 'testimonial_category'); if ($category) { _make_cat_compat($category); } return $category; }
/** * Retrieve post categories. * * @since 0.71 * @uses $post * * @param int $id Optional, default to current post ID. The post ID. * @return array */ function get_the_category( $id = false ) { global $post; $id = (int) $id; if ( !$id ) $id = (int) $post->ID; $categories = get_object_term_cache( $id, 'category' ); if ( false === $categories ) { $categories = wp_get_object_terms( $id, 'category' ); wp_cache_add($id, $categories, 'category_relationships'); } if ( !empty( $categories ) ) usort( $categories, '_usort_terms_by_name' ); else $categories = array(); foreach ( (array) array_keys( $categories ) as $key ) { _make_cat_compat( $categories[$key] ); } return $categories; }
/** * @see get_the_category() */ function hmp_get_the_category($post = null) { if ($post === null) { global $post; } if (!is_numeric($post)) { $post = $post->ID; } $categories = get_object_term_cache($post, 'hmp-entry-category'); if (false === $categories) { $categories = wp_get_object_terms($post, 'hmp-entry-category'); wp_cache_add($post, $categories, 'hmp-entry-category_relationships'); } if (!empty($categories)) { usort($categories, '_usort_terms_by_name'); } else { $categories = array(); } foreach ((array) array_keys($categories) as $key) { _make_cat_compat($categories[$key]); } return $categories; }