Changeset 10159 for trunk/wp-includes/category-template.php
- Timestamp:
- 12/09/2008 11:31:11 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/category-template.php
r10150 r10159 266 266 } 267 267 268 /** 269 * Checks whether the current post is within a particular category. 270 * 271 * This function checks to see if the post is within the supplied category. The 272 * category can be specified by number or name and will be checked as a name 273 * first to allow for categories with numeric names. Note: Prior to v2.5 of 274 * WordPress category names were not supported. 268 269 /** 270 * Check if the current post in within any of the given categories. 271 * 272 * The given categories are checked against the post's categories' term_ids, names and slugs. 273 * Categories given as integers will only be checked against the post's categories' term_ids. 274 * 275 * Prior to v2.5 of WordPress, category names were not supported. 276 * Prior to v2.7, category slugs were not supported. 277 * Prior to v2.7, only one category could be compared: in_category( $single_category ). 278 * Prior to v2.7, this function could only be used in the WordPress Loop. 279 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. 275 280 * 276 281 * @since 1.2.0 277 282 * 278 * @param int|string $category Category ID or category name. 279 * @return bool True, if the post is in the supplied category. 280 */ 281 function in_category( $category ) { 282 global $post; 283 283 * @uses is_object_in_term() 284 * 285 * @param int|string|array $category. Category ID, name or slug, or array of said. 286 * @param int|post object Optional. Post to check instead of the current post. @since 2.7.0 287 * @return bool True if the current post is in any of the given categories. 288 */ 289 function in_category( $category, $_post = null ) { 284 290 if ( empty( $category ) ) 285 291 return false; 286 292 287 // If category is not an int, check to see if it's a name 288 if ( ! is_int( $category ) ) { 289 $cat_ID = get_cat_ID( $category ); 290 if ( $cat_ID ) 291 $category = $cat_ID; 292 } 293 294 $categories = get_object_term_cache( $post->ID, 'category' ); 295 if ( false !== $categories ) { 296 if ( array_key_exists( $category, $categories ) ) 297 return true; 298 else 299 return false; 300 } 301 302 $categories = wp_get_object_terms( $post->ID, 'category', 'fields=ids' ); 303 if ( is_array($categories) && in_array($category, $categories) ) 304 return true; 305 else 293 if ( $_post ) { 294 $_post = get_post( $_post ); 295 } else { 296 $_post =& $GLOBALS['post']; 297 } 298 299 if ( !$_post ) 306 300 return false; 307 301 302 $r = is_object_in_term( $_post->ID, 'category', $category ); 303 if ( is_wp_error( $r ) ) 304 return false; 305 return $r; 308 306 } 309 307 … … 901 899 902 900 /** 903 * Check if the current post has the given tag. 904 * 905 * This function is only for use within the WordPress Loop. 901 * Check if the current post has any of given tags. 902 * 903 * The given tags are checked against the post's tags' term_ids, names and slugs. 904 * Tags given as integers will only be checked against the post's tags' term_ids. 905 * If no tags are given, determines if post has any tags. 906 * 907 * 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) 908 * Prior to v2.7, this function could only be used in the WordPress Loop. 909 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. 906 910 * 907 911 * @since 2.6.0 908 912 * 909 * @uses wp_get_object_terms() Gets the tags. 910 * 911 * @param string|int|array $tag Optional. The tag name/id/slug or array of them to check for. 912 * @return bool True if the current post has the given tag, or any tag, if no tag specified. 913 */ 914 function has_tag( $tag = '' ) { 915 global $post; 916 $taxonomy = 'post_tag'; 917 918 if ( !in_the_loop() ) return false; // in-the-loop function 919 920 $post_id = (int) $post->ID; 921 922 $terms = get_object_term_cache( $post_id, $taxonomy ); 923 if ( empty( $terms ) ) 924 $terms = wp_get_object_terms( $post_id, $taxonomy ); 925 if ( empty( $terms ) ) return false; 926 927 if ( empty( $tag ) ) return ( !empty( $terms ) ); 928 929 $tag = (array) $tag; 930 931 foreach ( $terms as $term ) { 932 if ( in_array( $term->term_id, $tag ) ) return true; 933 if ( in_array( $term->name, $tag ) ) return true; 934 if ( in_array( $term->slug, $tag ) ) return true; 935 } 936 937 return false; 913 * @uses is_object_in_term() 914 * 915 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. 916 * @param int|post object Optional. Post to check instead of the current post. @since 2.7.0 917 * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified). 918 */ 919 function has_tag( $tag = '', $_post = null ) { 920 if ( $_post ) { 921 $_post = get_post( $_post ); 922 } else { 923 $_post =& $GLOBALS['post']; 924 } 925 926 if ( !$_post ) 927 return false; 928 929 $r = is_object_in_term( $_post->ID, 'post_tag', $tag ); 930 if ( is_wp_error( $r ) ) 931 return false; 932 return $r; 938 933 } 939 934
Note: See TracChangeset
for help on using the changeset viewer.