WordPressを読む 44-3 /blog/wp-includes/category-template.php 3
2015/01/10
目次
- 1 /blog/wp-includes/category-template.php 3
- 2 関数 walk_category_tree()
- 3 関数 walk_category_dropdown_tree()
- 4 Walker_Category::クラス変数
- 5 Walker_Category::start_lvl()
- 6 Walker_Category::end_lvl()
- 7 Walker_Category::start_el()
- 8 Walker_Category::end_el()
- 9 Walker_CategoryDropdown ::クラス変数
- 10 Walker_CategoryDropdown::start_el()
- 11 関数 get_tag_link()
- 12 関数 get_the_tags()
- 13 関数 get_the_tag_list()
- 14 関数 the_tags()
- 15 関数 tag_description()
- 16 関数 term_description()
- 17 関数 get_the_terms()
- 18 関数 get_the_term_list()
- 19 関数 the_terms()
- 20 関数 has_category()
- 21 関数 has_tag()
- 22 関数 has_term()
/blog/wp-includes/category-template.php 3
関数 walk_category_tree()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | // // Helper functions // /** * Retrieve HTML list content for category list. * * @uses Walker_Category to create HTML list content. * @since 2.1.0 * @see Walker_Category::walk() for parameters and return description. */ function walk_category_tree() { $args = func_get_args(); // the user's options are the third parameter if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') ) $walker = new Walker_Category; else $walker = $args[2]['walker']; return call_user_func_array(array( &$walker, 'walk' ), $args ); } |
関数 walk_category_dropdown_tree()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Retrieve HTML dropdown (select) content for category list. * * @uses Walker_CategoryDropdown to create HTML dropdown content. * @since 2.1.0 * @see Walker_CategoryDropdown::walk() for parameters and return description. */ function walk_category_dropdown_tree() { $args = func_get_args(); // the user's options are the third parameter if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') ) $walker = new Walker_CategoryDropdown; else $walker = $args[2]['walker']; return call_user_func_array(array( &$walker, 'walk' ), $args ); } |
Walker_Category::クラス変数
継承元::Walker
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 | /** * Create HTML list of categories. * * @package WordPress * @since 2.1.0 * @uses Walker */ class Walker_Category extends Walker { /** * What the class handles. * * @see Walker::$tree_type * @since 2.1.0 * @var string */ var $tree_type = 'category'; /** * Database fields to use. * * @see Walker::$db_fields * @since 2.1.0 * @todo Decouple this * @var array */ var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); |
Walker_Category::start_lvl()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Starts the list before the elements are added. * * @see Walker::start_lvl() * * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of category. Used for tab indentation. * @param array $args An array of arguments. Will only append content if style argument value is 'list'. * @see wp_list_categories() */ function start_lvl( &$output, $depth = 0, $args = array() ) { if ( 'list' != $args['style'] ) return; $indent = str_repeat("\t", $depth); $output .= "$indent<ul class='children'>\n"; } |
Walker_Category::end_lvl()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Ends the list of after the elements are added. * * @see Walker::end_lvl() * * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of category. Used for tab indentation. * @param array $args An array of arguments. Will only append content if style argument value is 'list'. * @wsee wp_list_categories() */ function end_lvl( &$output, $depth = 0, $args = array() ) { if ( 'list' != $args['style'] ) return; $indent = str_repeat("\t", $depth); $output .= "$indent</ul>\n"; } |
Walker_Category::start_el()
継承元::Walker
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 | /** * Start the element output. * * @see Walker::start_el() * * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $category Category data object. * @param int $depth Depth of category in reference to parents. Default 0. * @param array $args An array of arguments. @see wp_list_categories() * @param int $id ID of the current category. */ function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { extract($args); $cat_name = esc_attr( $category->name ); /** This filter is documented in wp-includes/category-template.php */ $cat_name = apply_filters( 'list_cats', $cat_name, $category ); $link = '<a href="' . esc_url( get_term_link($category) ) . '" '; if ( $use_desc_for_title == 0 || empty($category->description) ) { $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"'; } else { /** * Filter the category description for display. * * @since 1.2.0 * * @param string $description Category description. * @param object $category Category object. */ $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"'; } $link .= '>'; $link .= $cat_name . '</a>'; if ( !empty($feed_image) || !empty($feed) ) { $link .= ' '; if ( empty($feed_image) ) $link .= '('; $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"'; if ( empty($feed) ) { $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"'; } else { $title = ' title="' . $feed . '"'; $alt = ' alt="' . $feed . '"'; $name = $feed; $link .= $title; } $link .= '>'; if ( empty($feed_image) ) $link .= $name; else $link .= "<img src='$feed_image'$alt$title" . ' />'; $link .= '</a>'; if ( empty($feed_image) ) $link .= ')'; } if ( !empty($show_count) ) $link .= ' (' . number_format_i18n( $category->count ) . ')'; if ( 'list' == $args['style'] ) { $output .= "\t<li"; $class = 'cat-item cat-item-' . $category->term_id; if ( !empty($current_category) ) { $_current_category = get_term( $current_category, $category->taxonomy ); if ( $category->term_id == $current_category ) $class .= ' current-cat'; elseif ( $category->term_id == $_current_category->parent ) $class .= ' current-cat-parent'; } $output .= ' class="' . $class . '"'; $output .= ">$link\n"; } else { $output .= "\t$link<br />\n"; } } |
Walker_Category::end_el()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Ends the element output, if needed. * * @see Walker::end_el() * * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $page Not used. * @param int $depth Depth of category. Not used. * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories() */ function end_el( &$output, $page, $depth = 0, $args = array() ) { if ( 'list' != $args['style'] ) return; $output .= "</li>\n"; } } |
Walker_Category::クラス定義終了
Walker_CategoryDropdown ::クラス変数
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Create HTML dropdown list of Categories. * * @package WordPress * @since 2.1.0 * @uses Walker */ class Walker_CategoryDropdown extends Walker { /** * @see Walker::$tree_type * @since 2.1.0 * @var string */ var $tree_type = 'category'; /** * @see Walker::$db_fields * @since 2.1.0 * @todo Decouple this * @var array */ var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); |
Walker_CategoryDropdown::start_el()
継承元::Walker
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 | /** * Start the element output. * * @see Walker::start_el() * @since 2.1.0 * * @param string $output Passed by reference. Used to append additional content. * @param object $category Category data object. * @param int $depth Depth of category. Used for padding. * @param array $args Uses 'selected' and 'show_count' keys, if they exist. @see wp_dropdown_categories() */ function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { $pad = str_repeat(' ', $depth * 3); /** This filter is documented in wp-includes/category-template.php */ $cat_name = apply_filters( 'list_cats', $category->name, $category ); $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\""; if ( $category->term_id == $args['selected'] ) $output .= ' selected="selected"'; $output .= '>'; $output .= $pad.$cat_name; if ( $args['show_count'] ) $output .= ' ('. number_format_i18n( $category->count ) .')'; $output .= "</option>\n"; } } |
Walker_CategoryDropdown::クラス定義終了
関数 get_tag_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // // Tags // /** * Retrieve the link to the tag. * * @since 2.3.0 * @see get_term_link() * * @param int|object $tag Tag ID or object. * @return string Link on success, empty string if tag does not exist. */ function get_tag_link( $tag ) { if ( ! is_object( $tag ) ) $tag = (int) $tag; $tag = get_term_link( $tag, 'post_tag' ); if ( is_wp_error( $tag ) ) return ''; return $tag; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Retrieve the tags for a post. * * @since 2.3.0 * * @param int $id Post ID. * @return array|bool Array of tag objects on success, false on failure. */ function get_the_tags( $id = 0 ) { /** * Filter the array of tags for the given post. * * @since 2.3.0 * * @see get_the_terms() * * @param array $terms An array of tags for the given post. */ return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) ); } |
関数 get_the_tag_list()
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 | /** * Retrieve the tags for a post formatted as a string. * * @since 2.3.0 * * @param string $before Optional. Before tags. * @param string $sep Optional. Between tags. * @param string $after Optional. After tags. * @param int $id Optional. Post ID. Defaults to the current post. * @return string|bool|WP_Error A list of tags on success, false or WP_Error on failure. */ function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) { /** * Filter the tags list for a given post. * * @since 2.3.0 * * @param string $tag_list List of tags. * @param string $before String to use before tags. * @param string $sep String to use between the tags. * @param string $after String to use after tags. * @param int $id Post ID. */ return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Retrieve the tags for a post. * * @since 2.3.0 * * @param string $before Optional. Before list. * @param string $sep Optional. Separate items using this. * @param string $after Optional. After list. */ function the_tags( $before = null, $sep = ', ', $after = '' ) { if ( null === $before ) $before = __('Tags: '); echo get_the_tag_list($before, $sep, $after); } |
関数 tag_description()
1 2 3 4 5 6 7 8 9 10 11 | /** * Retrieve tag description. * * @since 2.8.0 * * @param int $tag Optional. Tag ID. Will use global tag ID by default. * @return string Tag description, available. */ function tag_description( $tag = 0 ) { return term_description( $tag ); } |
関数 term_description()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Retrieve term description. * * @since 2.8.0 * * @param int $term Optional. Term ID. Will use global term ID by default. * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'. * @return string Term description, available. */ function term_description( $term = 0, $taxonomy = 'post_tag' ) { if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) { $term = get_queried_object(); if ( $term ) { $taxonomy = $term->taxonomy; $term = $term->term_id; } } $description = get_term_field( 'description', $term, $taxonomy ); return is_wp_error( $description ) ? '' : $description; } |
関数 get_the_terms()
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 | /** * Retrieve the terms of the taxonomy that are attached to the post. * * @since 2.5.0 * * @param int|object $post Post ID or object. * @param string $taxonomy Taxonomy name. * @return array|bool|WP_Error Array of term objects on success, false or WP_Error on failure. */ function get_the_terms( $post, $taxonomy ) { if ( ! $post = get_post( $post ) ) return false; $terms = get_object_term_cache( $post->ID, $taxonomy ); if ( false === $terms ) { $terms = wp_get_object_terms( $post->ID, $taxonomy ); wp_cache_add($post->ID, $terms, $taxonomy . '_relationships'); } /** * Filter the list of terms attached to the given post. * * @since 3.1.0 * * @param array $terms List of attached terms. * @param int $post_id Post ID. * @param string $taxonomy Name of the taxonomy. */ $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); if ( empty( $terms ) ) return false; return $terms; } |
関数 get_the_term_list()
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 | /** * Retrieve a post's terms as a list with specified format. * * @since 2.5.0 * * @param int $id Post ID. * @param string $taxonomy Taxonomy name. * @param string $before Optional. Before list. * @param string $sep Optional. Separate items using this. * @param string $after Optional. After list. * @return string|bool|WP_Error A list of terms on success, false or WP_Error on failure. */ function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { $terms = get_the_terms( $id, $taxonomy ); if ( is_wp_error( $terms ) ) return $terms; if ( empty( $terms ) ) return false; foreach ( $terms as $term ) { $link = get_term_link( $term, $taxonomy ); if ( is_wp_error( $link ) ) return $link; $term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>'; } /** * Filter the term links for a given taxonomy. * * The dynamic portion of the filter name, $taxonomy, refers * to the taxonomy slug. * * @since 2.5.0 * * @param array $term_links An array of term links. */ $term_links = apply_filters( "term_links-$taxonomy", $term_links ); return $before . join( $sep, $term_links ) . $after; } |
関数 the_terms()
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 | /** * Display the terms in a list. * * @since 2.5.0 * * @param int $id Post ID. * @param string $taxonomy Taxonomy name. * @param string $before Optional. Before list. * @param string $sep Optional. Separate items using this. * @param string $after Optional. After list. * @return null|bool False on WordPress error. Returns null when displaying. */ function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after ); if ( is_wp_error( $term_list ) ) return false; /** * Filter the list of terms to display. * * @since 2.9.0 * * @param array $term_list List of terms to display. * @param string $taxonomy The taxonomy name. * @param string $before String to use before the terms. * @param string $sep String to use between the terms. * @param string $after String to use after the terms. */ echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after ); } |
関数 has_category()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Check if the current post has any of given category. * * @since 3.1.0 * * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for. * @param int|object $post Optional. Post to check instead of the current post. * @return bool True if the current post has any of the given categories (or any category, if no category specified). */ function has_category( $category = '', $post = null ) { return has_term( $category, 'category', $post ); } |
関数 has_tag()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Check if the current post has any of given tags. * * The given tags are checked against the post's tags' term_ids, names and slugs. * Tags given as integers will only be checked against the post's tags' term_ids. * If no tags are given, determines if post has any tags. * * Prior to v2.7 of WordPress, tags given as integers would also be checked against the post's tags' names and slugs (in addition to term_ids) * Prior to v2.7, this function could only be used in the WordPress Loop. * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. * * @since 2.6.0 * * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). */ function has_tag( $tag = '', $post = null ) { return has_term( $tag, 'post_tag', $post ); } |
関数 has_term()
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 | /** * Check if the current post has any of given terms. * * The given terms are checked against the post's terms' term_ids, names and slugs. * Terms given as integers will only be checked against the post's terms' term_ids. * If no terms are given, determines if post has any terms. * * @since 3.1.0 * * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for. * @param string $taxonomy Taxonomy name * @param int|object $post Optional. Post to check instead of the current post. * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). */ function has_term( $term = '', $taxonomy = '', $post = null ) { $post = get_post($post); if ( !$post ) return false; $r = is_object_in_term( $post->ID, $taxonomy, $term ); if ( is_wp_error( $r ) ) return false; return $r; } |