示例#1
0
文件: tags.php 项目: ecerta/webcomic
 /**
  * Return a relative term.
  * 
  * @param string $relative The relative term to retrieve; one of 'next', 'previous', 'first', 'last', or 'random'.
  * @param string $taxonomy The taxonomy the relative term must belong to.
  * @param array $args An array of arguments to pass to get_terms().
  * @return object
  */
 public static function get_relative_webcomic_term($relative = 'random', $taxonomy = '', $args = array())
 {
     global $post;
     $object = is_tax() ? get_queried_object() : false;
     if (!taxonomy_exists($taxonomy) and WebcomicTag::is_webcomic_tax()) {
         $taxonomy = $object->taxonomy;
     } elseif (('next' === $relative or 'previous' === $relative) and is_singular() and $terms = wp_get_object_terms($post->ID, $taxonomy, array_merge(array('hide_empty' => true, 'orderby' => is_taxonomy_hierarchical($taxonomy) ? 'term_group' : 'name'), (array) $args, array('cache_domain' => 'get_relative_webcomic_term'))) and !is_wp_error($terms)) {
         $object = 'next' === $relative ? array_pop($terms) : array_shift($terms);
     }
     $args = array_merge(array('hide_empty' => true, 'orderby' => is_taxonomy_hierarchical($taxonomy) ? 'term_group' : 'name'), (array) $args, array('cache_domain' => 'get_relative_webcomic_term'));
     if (taxonomy_exists($taxonomy) and ('previous' === $relative or 'next' === $relative) ? !empty($object) : true) {
         if ('first' === $relative and $terms = get_terms($taxonomy, array_merge($args, array('parent' => 0))) and !is_wp_error($terms)) {
             $object = $terms[0];
         } elseif ('random' === $relative and $terms = get_terms($taxonomy, $args) and !is_wp_error($terms)) {
             shuffle($terms);
             $object = $terms[0];
         } elseif ('last' === $relative and $terms = get_terms($taxonomy, array_merge($args, array('parent' => 0))) and !is_wp_error($terms)) {
             $last = array_pop($terms);
             while ($children = get_terms($last->taxonomy, array_merge($args, array('parent' => $last->term_id)))) {
                 $last = array_pop($children);
             }
             $object = $last;
         } elseif ('previous' === $relative) {
             if (!$object->term_group and $object->parent) {
                 $object = get_term($object->parent, $object->taxonomy);
             } elseif ($terms = get_terms($object->taxonomy, array_merge($args, array('parent' => $object->parent))) and !is_wp_error($terms) and false !== ($key = array_search($object, $terms)) and isset($terms[$key - 1])) {
                 $previous = $terms[$key - 1];
                 while ($children = get_terms($previous->taxonomy, array_merge($args, array('parent' => $previous->term_id)))) {
                     $previous = array_pop($children);
                 }
                 $object = $previous;
             }
         } elseif ('next' === $relative) {
             if ($children = get_terms($object->taxonomy, array_merge($args, array('parent' => $object->term_id))) and !is_wp_error($children)) {
                 $object = $children[0];
             } elseif ($terms = get_terms($object->taxonomy, array_merge($args, array('parent' => $object->parent, 'fields' => 'ids'))) and !is_wp_error($terms) and false !== ($key = array_search($object->term_id, $terms)) and isset($terms[$key + 1])) {
                 $object = get_term($terms[$key + 1], $taxonomy);
             } else {
                 $next = $object;
                 while ($next = get_term($next->parent, $next->taxonomy) and !is_wp_error($next)) {
                     if ($children = get_terms($next->taxonomy, array_merge($args, array('parent' => $next->parent))) and !is_wp_error($children) and false !== ($key = array_search($next, $children)) and isset($children[$key + 1])) {
                         $object = $children[$key + 1];
                         break;
                     }
                     if (!$next->parent) {
                         break;
                     }
                 }
             }
         }
         return $object;
     }
 }
示例#2
0
 /**
  * Automagically integrate basic webcomic functionality.
  * 
  * Reorders webcomics on post type and taxonomy archive pages so
  * that they appear in chronological order.
  * 
  * @param object $query WP_Query object.
  * @uses WebcomicTag::is_webcomic_archive()
  * @uses WebcomicTag::is_webcomic_tax()
  * @filter string webcomic_integrate_sort Filters the sort order of webcomics on post type and taxonomy archive pages.
  */
 public function integrate_sort_asc($query)
 {
     if (self::$integrate and $query->is_main_query() and (WebcomicTag::is_webcomic_archive() or WebcomicTag::is_webcomic_tax())) {
         $query->set('order', apply_filters('webcomic_integrate_sort', 'ASC'));
     }
 }