WordPressを読む 39-1 /blog/wp-includes/post-template.php 1
2015/01/06
目次
- 1 /blog/wp-includes/post-template.php
- 2 関数 the_ID()
- 3 関数 get_the_ID()
- 4 関数 the_title()
- 5 関数 the_title_attribute()
- 6 関数 get_the_title()
- 7 関数 the_guid()
- 8 関数 get_the_guid()
- 9 関数 the_content()
- 10 関数 get_the_content()
- 11 関数 _convert_urlencoded_to_entities()
- 12 関数 the_excerpt()
- 13 関数 get_the_excerpt()
- 14 関数 has_excerpt()
- 15 関数 post_class()
- 16 関数 get_post_class()
- 17 関数 body_class()
/blog/wp-includes/post-template.php
関数 the_ID()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php /** * WordPress Post Template Functions. * * Gets content for the current post in the loop. * * @package WordPress * @subpackage Template */ /** * Display the ID of the current item in the WordPress Loop. * * @since 0.71 */ function the_ID() { echo get_the_ID(); } |
関数 get_the_ID()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Retrieve the ID of the current item in the WordPress Loop. * * @since 2.1.0 * @uses $post * * @return int|bool The ID of the current item in the WordPress Loop. False if $post is not set. */ function get_the_ID() { $post = get_post(); return ! empty( $post ) ? $post->ID : false; } |
関数 the_title()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Display or retrieve the current post title with optional content. * * @since 0.71 * * @param string $before Optional. Content to prepend to the title. * @param string $after Optional. Content to append to the title. * @param bool $echo Optional, default to true.Whether to display or return. * @return null|string Null on no title. String if $echo parameter is false. */ function the_title($before = '', $after = '', $echo = true) { $title = get_the_title(); if ( strlen($title) == 0 ) return; $title = $before . $title . $after; if ( $echo ) echo $title; else return $title; } |
関数 the_title_attribute()
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 | /** * Sanitize the current title when retrieving or displaying. * * Works like {@link the_title()}, except the parameters can be in a string or * an array. See the function for what can be override in the $args parameter. * * The title before it is displayed will have the tags stripped and {@link * esc_attr()} before it is passed to the user or displayed. The default * as with {@link the_title()}, is to display the title. * * @since 2.3.0 * * @param string|array $args { * Title attribute arguments. Optional. * * @type string $before Markup to prepend to the title. Default empty. * @type string $after Markup to append to the title. Default empty. * @type bool $echo Whether to echo or return the title. Default true for echo. * @type WP_Post $post Current post object to retrieve the title for. * } * @return string|null Null on failure or display. String when echo is false. */ function the_title_attribute( $args = '' ) { $defaults = array( 'before' => '', 'after' => '', 'echo' => true, 'post' => get_post() ); $r = wp_parse_args( $args, $defaults ); $title = get_the_title( $r['post'] ); if ( strlen( $title ) == 0 ) { return; } $title = $r['before'] . $title . $r['after']; $title = esc_attr( strip_tags( $title ) ); if ( $r['echo'] ) { echo $title; } else { return $title; } } |
関数 get_the_title()
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 | /** * Retrieve post title. * * If the post is protected and the visitor is not an admin, then "Protected" * will be displayed before the post title. If the post is private, then * "Private" will be located before the post title. * * @since 0.71 * * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. * @return string */ function get_the_title( $post = 0 ) { $post = get_post( $post ); $title = isset( $post->post_title ) ? $post->post_title : ''; $id = isset( $post->ID ) ? $post->ID : 0; if ( ! is_admin() ) { if ( ! empty( $post->post_password ) ) { /** * Filter the text prepended to the post title for protected posts. * * The filter is only applied on the front end. * * @since 2.8.0 * * @param string $prepend Text displayed before the post title. * Default 'Protected: %s'. * @param WP_Post $post Current post object. */ $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post ); $title = sprintf( $protected_title_format, $title ); } else if ( isset( $post->post_status ) && 'private' == $post->post_status ) { /** * Filter the text prepended to the post title of private posts. * * The filter is only applied on the front end. * * @since 2.8.0 * * @param string $prepend Text displayed before the post title. * Default 'Private: %s'. * @param WP_Post $post Current post object. */ $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post ); $title = sprintf( $private_title_format, $title ); } } /** * Filter the post title. * * @since 0.71 * * @param string $title The post title. * @param int $id The post ID. */ return apply_filters( 'the_title', $title, $id ); } |
関数 the_guid()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Display the Post Global Unique Identifier (guid). * * The guid will appear to be a link, but should not be used as an link to the * post. The reason you should not use it as a link, is because of moving the * blog across domains. * * Url is escaped to make it xml safe * * @since 1.5.0 * * @param int|WP_Post $id Optional. Post ID or post object. */ function the_guid( $id = 0 ) { echo esc_url( get_the_guid( $id ) ); } |
関数 get_the_guid()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Retrieve the Post Global Unique Identifier (guid). * * The guid will appear to be a link, but should not be used as an link to the * post. The reason you should not use it as a link, is because of moving the * blog across domains. * * @since 1.5.0 * * @param int|WP_Post $id Optional. Post ID or post object. * @return string */ function get_the_guid( $id = 0 ) { $post = get_post($id); /** * Filter the Global Unique Identifier (guid) of the post. * * @since 1.5.0 * * @param string $post_guid Global Unique Identifier (guid) of the post. */ return apply_filters( 'get_the_guid', $post->guid ); } |
関数 the_content()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Display the post content. * * @since 0.71 * * @param string $more_link_text Optional. Content for when there is more text. * @param bool $strip_teaser Optional. Strip teaser content before the more text. Default is false. */ function the_content( $more_link_text = null, $strip_teaser = false) { $content = get_the_content( $more_link_text, $strip_teaser ); /** * Filter the post content. * * @since 0.71 * * @param string $content Content of the current post. */ $content = apply_filters( 'the_content', $content ); $content = str_replace( ']]>', ']]>', $content ); echo $content; } |
関数 get_the_content()
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 | /** * Retrieve the post content. * * @since 0.71 * * @param string $more_link_text Optional. Content for when there is more text. * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false. * @return string */ function get_the_content( $more_link_text = null, $strip_teaser = false ) { global $page, $more, $preview, $pages, $multipage; $post = get_post(); if ( null === $more_link_text ) $more_link_text = __( '(more…)' ); $output = ''; $has_teaser = false; // If post password required and it doesn't match the cookie. if ( post_password_required( $post ) ) return get_the_password_form( $post ); if ( $page > count( $pages ) ) // if the requested page doesn't exist $page = count( $pages ); // give them the highest numbered page that DOES exist $content = $pages[$page - 1]; if ( preg_match( '/<!--more(.*?)?-->/', $content, $matches ) ) { $content = explode( $matches[0], $content, 2 ); if ( ! empty( $matches[1] ) && ! empty( $more_link_text ) ) $more_link_text = strip_tags( wp_kses_no_null( trim( $matches[1] ) ) ); $has_teaser = true; } else { $content = array( $content ); } if ( false !== strpos( $post->post_content, '<!--noteaser-->' ) && ( ! $multipage || $page == 1 ) ) $strip_teaser = true; $teaser = $content[0]; if ( $more && $strip_teaser && $has_teaser ) $teaser = ''; $output .= $teaser; if ( count( $content ) > 1 ) { if ( $more ) { $output .= '<span id="more-' . $post->ID . '"></span>' . $content[1]; } else { if ( ! empty( $more_link_text ) ) /** * Filter the Read More link text. * * @since 2.8.0 * * @param string $more_link_element Read More link element. * @param string $more_link_text Read More text. */ $output .= apply_filters( 'the_content_more_link', ' <a href="' . get_permalink() . "#more-{$post->ID}\" class=\"more-link\">$more_link_text</a>", $more_link_text ); $output = force_balance_tags( $output ); } } if ( $preview ) // preview fix for javascript bug with foreign languages $output = preg_replace_callback( '/\%u([0-9A-F]{4})/', '_convert_urlencoded_to_entities', $output ); return $output; } |
関数 _convert_urlencoded_to_entities()
1 2 3 4 5 6 7 8 9 10 11 | /** * Preview fix for javascript bug with foreign languages * * @since 3.1.0 * @access private * @param array $match Match array from preg_replace_callback * @return string */ function _convert_urlencoded_to_entities( $match ) { return '&#' . base_convert( $match[1], 16, 10 ) . ';'; } |
関数 the_excerpt()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Display the post excerpt. * * @since 0.71 */ function the_excerpt() { /** * Filter the displayed post excerpt. * * @since 0.71 * * @see get_the_excerpt() * * @param string $post_excerpt The post excerpt. */ echo apply_filters( 'the_excerpt', get_the_excerpt() ); } |
関数 get_the_excerpt()
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 | /** * Retrieve the post excerpt. * * @since 0.71 * * @param mixed $deprecated Not used. * @return string */ function get_the_excerpt( $deprecated = '' ) { if ( !empty( $deprecated ) ) _deprecated_argument( __FUNCTION__, '2.3' ); $post = get_post(); if ( empty( $post ) ) { return ''; } if ( post_password_required() ) { return __( 'There is no excerpt because this is a protected post.' ); } /** * Filter the retrieved post excerpt. * * @since 1.2.0 * * @param string $post_excerpt The post excerpt. */ return apply_filters( 'get_the_excerpt', $post->post_excerpt ); } |
関数 has_excerpt()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Whether post has excerpt. * * @since 2.3.0 * * @param int|WP_Post $id Optional. Post ID or post object. * @return bool */ function has_excerpt( $id = 0 ) { $post = get_post( $id ); return ( !empty( $post->post_excerpt ) ); } |
関数 post_class()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Display the classes for the post div. * * @since 2.7.0 * * @param string|array $class One or more classes to add to the class list. * @param int|WP_Post $post_id Optional. Post ID or post object. */ function post_class( $class = '', $post_id = null ) { // Separates classes with a single space, collates classes for post DIV echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"'; } |
関数 get_post_class()
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 | /** * Retrieve the classes for the post div as an array. * * The class names are many. If the post is a sticky, then the 'sticky' * class name. The class 'hentry' is always added to each post. If the post has a * post thumbnail, 'has-post-thumbnail' is added as a class. For each * category, the class will be added with 'category-' with category slug is * added. The tags are the same way as the categories with 'tag-' before the tag * slug. All classes are passed through the filter, 'post_class' with the list * of classes, followed by $class parameter value, with the post ID as the last * parameter. * * @since 2.7.0 * * @param string|array $class One or more classes to add to the class list. * @param int|WP_Post $post_id Optional. Post ID or post object. * @return array Array of classes. */ function get_post_class( $class = '', $post_id = null ) { $post = get_post($post_id); $classes = array(); if ( empty($post) ) return $classes; $classes[] = 'post-' . $post->ID; if ( ! is_admin() ) $classes[] = $post->post_type; $classes[] = 'type-' . $post->post_type; $classes[] = 'status-' . $post->post_status; // Post Format if ( post_type_supports( $post->post_type, 'post-formats' ) ) { $post_format = get_post_format( $post->ID ); if ( $post_format && !is_wp_error($post_format) ) $classes[] = 'format-' . sanitize_html_class( $post_format ); else $classes[] = 'format-standard'; } // Post requires password if ( post_password_required( $post->ID ) ) { $classes[] = 'post-password-required'; // Post thumbnails } elseif ( ! is_attachment( $post ) && current_theme_supports( 'post-thumbnails' ) && has_post_thumbnail( $post->ID ) ) { $classes[] = 'has-post-thumbnail'; } // sticky for Sticky Posts if ( is_sticky( $post->ID ) ) { if ( is_home() && ! is_paged() ) { $classes[] = 'sticky'; } elseif ( is_admin() ) { $classes[] = 'status-sticky'; } } // hentry for hAtom compliance $classes[] = 'hentry'; // Categories if ( is_object_in_taxonomy( $post->post_type, 'category' ) ) { foreach ( (array) get_the_category($post->ID) as $cat ) { if ( empty($cat->slug ) ) continue; $classes[] = 'category-' . sanitize_html_class($cat->slug, $cat->term_id); } } // Tags if ( is_object_in_taxonomy( $post->post_type, 'post_tag' ) ) { foreach ( (array) get_the_tags($post->ID) as $tag ) { if ( empty($tag->slug ) ) continue; $classes[] = 'tag-' . sanitize_html_class($tag->slug, $tag->term_id); } } if ( !empty($class) ) { if ( !is_array( $class ) ) $class = preg_split('#\s+#', $class); $classes = array_merge($classes, $class); } $classes = array_map('esc_attr', $classes); /** * Filter the list of CSS classes for the current post. * * @since 2.7.0 * * @param array $classes An array of post classes. * @param string $class A comma-separated list of additional classes added to the post. * @param int $post_id The post ID. */ $classes = apply_filters( 'post_class', $classes, $class, $post->ID ); return array_unique( $classes ); } |
関数 body_class()
1 2 3 4 5 6 7 8 9 10 11 | /** * Display the classes for the body element. * * @since 2.8.0 * * @param string|array $class One or more classes to add to the class list. */ function body_class( $class = '' ) { // Separates classes with a single space, collates classes for body element echo 'class="' . join( ' ', get_body_class( $class ) ) . '"'; } |