WordPressを読む 44-2 /blog/wp-includes/category-template.php 2
2015/01/10
目次
/blog/wp-includes/category-template.php 2
関数 wp_list_categories()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | /** * Display or retrieve the HTML list of categories. * * The list of arguments is below: * 'show_option_all' (string) - Text to display for showing all categories. * 'orderby' (string) default is 'ID' - What column to use for ordering the * categories. * 'order' (string) default is 'ASC' - What direction to order categories. * 'show_count' (bool|int) default is 0 - Whether to show how many posts are * in the category. * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that * don't have any posts attached to them. * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the * description instead of the category title. * 'feed' - See {@link get_categories()}. * 'feed_type' - See {@link get_categories()}. * 'feed_image' - See {@link get_categories()}. * 'child_of' (int) default is 0 - See {@link get_categories()}. * 'exclude' (string) - See {@link get_categories()}. * 'exclude_tree' (string) - See {@link get_categories()}. * 'echo' (bool|int) default is 1 - Whether to display or retrieve content. * 'current_category' (int) - See {@link get_categories()}. * 'hierarchical' (bool) - See {@link get_categories()}. * 'title_li' (string) - See {@link get_categories()}. * 'depth' (int) - The max depth. * * @since 2.1.0 * * @param string|array $args Optional. Override default arguments. * @return string HTML content only if 'echo' argument is 0. */ function wp_list_categories( $args = '' ) { $defaults = array( 'show_option_all' => '', 'show_option_none' => __('No categories'), '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_category' => 0, 'hierarchical' => true, 'title_li' => __( 'Categories' ), 'echo' => 1, 'depth' => 0, 'taxonomy' => 'category' ); $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'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy']; extract( $r ); if ( !taxonomy_exists($taxonomy) ) return false; $categories = get_categories( $r ); $output = ''; if ( $title_li && 'list' == $style ) $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>'; if ( empty( $categories ) ) { if ( ! empty( $show_option_none ) ) { if ( 'list' == $style ) $output .= '<li class="cat-item-none">' . $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 class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>"; else $output .= "<a href='$posts_page'>$show_option_all</a>"; } if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) { $current_term_object = get_queried_object(); if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) $r['current_category'] = get_queried_object_id(); } if ( $hierarchical ) $depth = $r['depth']; else $depth = -1; // Flat. $output .= walk_category_tree( $categories, $depth, $r ); } if ( $title_li && 'list' == $style ) $output .= '</ul></li>'; /** * Filter the HTML output of a taxonomy list. * * @since 2.1.0 * * @param string $output HTML output. * @param array $args An array of taxonomy-listing arguments. */ $output = apply_filters( 'wp_list_categories', $output, $args ); if ( $echo ) echo $output; else return $output; } |
関数 wp_tag_cloud()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /** * Display tag cloud. * * The text size is set by the 'smallest' and 'largest' arguments, which will * use the 'unit' argument value for the CSS text size unit. The 'format' * argument can be 'flat' (default), 'list', or 'array'. The flat value for the * 'format' argument will separate tags with spaces. The list value for the * 'format' argument will format the tags in a UL HTML list. The array value for * the 'format' argument will return in PHP array type format. * * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'. * * The 'number' argument is how many tags to return. By default, the limit will * be to return the top 45 tags in the tag cloud list. * * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the * text for the tooltip of the tag link. * * The 'topic_count_text_callback' argument is a function, which given the count * of the posts with that tag returns a text for the tooltip of the tag link. * * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type * passed to edit.php for the popular tags edit links. * * The 'exclude' and 'include' arguments are used for the {@link get_tags()} * function. Only one should be used, because only one will be used and the * other ignored, if they are both set. * * @since 2.3.0 * * @param array|string $args Optional. Override default arguments. * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. */ function wp_tag_cloud( $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true ); $args = wp_parse_args( $args, $defaults ); $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags if ( empty( $tags ) || is_wp_error( $tags ) ) return; foreach ( $tags as $key => $tag ) { if ( 'edit' == $args['link'] ) $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] ); else $link = get_term_link( intval($tag->term_id), $tag->taxonomy ); if ( is_wp_error( $link ) ) return false; $tags[ $key ]->link = $link; $tags[ $key ]->id = $tag->term_id; } $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args /** * Filter the tag cloud output. * * @since 2.3.0 * * @param string $return HTML output of the tag cloud. * @param array $args An array of tag cloud arguments. */ $return = apply_filters( 'wp_tag_cloud', $return, $args ); if ( 'array' == $args['format'] || empty($args['echo']) ) return $return; echo $return; } |
関数 default_topic_count_scale()
1 2 3 4 5 6 7 8 9 | /** * Default topic count scaling for tag links * * @param integer $count number of posts with that tag * @return integer scaled count */ function default_topic_count_scale( $count ) { return round(log10($count + 1) * 100); } |
関数 wp_generate_tag_cloud()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | /** * Generates a tag cloud (heatmap) from provided data. * * The text size is set by the 'smallest' and 'largest' arguments, which will * use the 'unit' argument value for the CSS text size unit. The 'format' * argument can be 'flat' (default), 'list', or 'array'. The flat value for the * 'format' argument will separate tags with spaces. The list value for the * 'format' argument will format the tags in a UL HTML list. The array value for * the 'format' argument will return in PHP array type format. * * The 'tag_cloud_sort' filter allows you to override the sorting. * Passed to the filter: $tags array and $args array, has to return the $tags array * after sorting it. * * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or * 'RAND'. * * The 'number' argument is how many tags to return. By default, the limit will * be to return the entire tag cloud list. * * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the * text for the tooltip of the tag link. * * The 'topic_count_text_callback' argument is a function, which given the count * of the posts with that tag returns a text for the tooltip of the tag link. * * @todo Complete functionality. * @since 2.3.0 * * @param array $tags List of tags. * @param string|array $args Optional, override default arguments. * @return string|array Tag cloud as a string or an array, depending on 'format' argument. */ function wp_generate_tag_cloud( $tags, $args = '' ) { $defaults = array( 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', 'topic_count_text' => null, 'topic_count_text_callback' => null, 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, ); $args = wp_parse_args( $args, $defaults ); extract( $args, EXTR_SKIP ); $return = ( 'array' === $format ) ? array() : ''; if ( empty( $tags ) ) { return $return; } // Juggle topic count tooltips: if ( isset( $args['topic_count_text'] ) ) { // First look for nooped plural support via topic_count_text. $translate_nooped_plural = $args['topic_count_text']; } elseif ( ! empty( $args['topic_count_text_callback'] ) ) { // Look for the alternative callback style. Ignore the previous default. if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) { $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); } else { $translate_nooped_plural = false; } } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { // If no callback exists, look for the old-style single_text and multiple_text arguments. $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); } else { // This is the default for when no callback, plural, or argument is passed in. $translate_nooped_plural = _n_noop( '%s topic', '%s topics' ); } /** * Filter how the items in a tag cloud are sorted. * * @since 2.8.0 * * @param array $tags Ordered array of terms. * @param array $args An array of tag cloud arguments. */ $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args ); if ( empty( $tags_sorted ) ) { return $return; } if ( $tags_sorted !== $tags ) { $tags = $tags_sorted; unset( $tags_sorted ); } else { if ( 'RAND' === $order ) { shuffle( $tags ); } else { // SQL cannot save you; this is a second (potentially different) sort on a subset of data. if ( 'name' === $orderby ) { uasort( $tags, '_wp_object_name_sort_cb' ); } else { uasort( $tags, '_wp_object_count_sort_cb' ); } if ( 'DESC' === $order ) { $tags = array_reverse( $tags, true ); } } } if ( $number > 0 ) $tags = array_slice($tags, 0, $number); $counts = array(); $real_counts = array(); // For the alt tag foreach ( (array) $tags as $key => $tag ) { $real_counts[ $key ] = $tag->count; $counts[ $key ] = $topic_count_scale_callback($tag->count); } $min_count = min( $counts ); $spread = max( $counts ) - $min_count; if ( $spread <= 0 ) $spread = 1; $font_spread = $largest - $smallest; if ( $font_spread < 0 ) $font_spread = 1; $font_step = $font_spread / $spread; $a = array(); foreach ( $tags as $key => $tag ) { $count = $counts[ $key ]; $real_count = $real_counts[ $key ]; $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#'; $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key; $tag_name = $tags[ $key ]->name; if ( $translate_nooped_plural ) { $title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); } else { $title_attribute = call_user_func( $topic_count_text_callback, $real_count, $tag, $args ); } $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $title_attribute ) . "' style='font-size: " . str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) ) . "$unit;'>$tag_name</a>"; } switch ( $format ) : case 'array' : $return =& $a; break; case 'list' : $return = "<ul class='wp-tag-cloud'>\n\t<li>"; $return .= join( "</li>\n\t<li>", $a ); $return .= "</li>\n</ul>\n"; break; default : $return = join( $separator, $a ); break; endswitch; if ( $filter ) { /** * Filter the generated output of a tag cloud. * * The filter is only evaluated if a true value is passed * to the $filter argument in wp_generate_tag_cloud(). * * @since 2.3.0 * * @see wp_generate_tag_cloud() * * @param array|string $return String containing the generated HTML tag cloud output * or an array of tag links if the 'format' argument * equals 'array'. * @param array $tags An array of terms used in the tag cloud. * @param array $args An array of wp_generate_tag_cloud() arguments. */ return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); } else return $return; } |
関数 _wp_object_name_sort_cb()
1 2 3 4 5 6 7 8 9 | /** * Callback for comparing objects based on name * * @since 3.1.0 * @access private */ function _wp_object_name_sort_cb( $a, $b ) { return strnatcasecmp( $a->name, $b->name ); } |
関数 _wp_object_count_sort_cb()
1 2 3 4 5 6 7 8 9 | /** * Callback for comparing objects based on count * * @since 3.1.0 * @access private */ function _wp_object_count_sort_cb( $a, $b ) { return ( $a->count > $b->count ); } |