WordPressを読む 39-2 /blog/wp-includes/post-template.php 2
2015/01/06
目次
/blog/wp-includes/post-template.php 2
関数 get_body_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 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 180 181 182 | /** * Retrieve the classes for the body element as an array. * * @since 2.8.0 * * @param string|array $class One or more classes to add to the class list. * @return array Array of classes. */ function get_body_class( $class = '' ) { global $wp_query, $wpdb; $classes = array(); if ( is_rtl() ) $classes[] = 'rtl'; if ( is_front_page() ) $classes[] = 'home'; if ( is_home() ) $classes[] = 'blog'; if ( is_archive() ) $classes[] = 'archive'; if ( is_date() ) $classes[] = 'date'; if ( is_search() ) { $classes[] = 'search'; $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results'; } if ( is_paged() ) $classes[] = 'paged'; if ( is_attachment() ) $classes[] = 'attachment'; if ( is_404() ) $classes[] = 'error404'; if ( is_single() ) { $post_id = $wp_query->get_queried_object_id(); $post = $wp_query->get_queried_object(); $classes[] = 'single'; if ( isset( $post->post_type ) ) { $classes[] = 'single-' . sanitize_html_class($post->post_type, $post_id); $classes[] = 'postid-' . $post_id; // 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[] = 'single-format-' . sanitize_html_class( $post_format ); else $classes[] = 'single-format-standard'; } } if ( is_attachment() ) { $mime_type = get_post_mime_type($post_id); $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' ); $classes[] = 'attachmentid-' . $post_id; $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type ); } } elseif ( is_archive() ) { if ( is_post_type_archive() ) { $classes[] = 'post-type-archive'; $post_type = get_query_var( 'post_type' ); if ( is_array( $post_type ) ) $post_type = reset( $post_type ); $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type ); } else if ( is_author() ) { $author = $wp_query->get_queried_object(); $classes[] = 'author'; if ( isset( $author->user_nicename ) ) { $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID ); $classes[] = 'author-' . $author->ID; } } elseif ( is_category() ) { $cat = $wp_query->get_queried_object(); $classes[] = 'category'; if ( isset( $cat->term_id ) ) { $classes[] = 'category-' . sanitize_html_class( $cat->slug, $cat->term_id ); $classes[] = 'category-' . $cat->term_id; } } elseif ( is_tag() ) { $tags = $wp_query->get_queried_object(); $classes[] = 'tag'; if ( isset( $tags->term_id ) ) { $classes[] = 'tag-' . sanitize_html_class( $tags->slug, $tags->term_id ); $classes[] = 'tag-' . $tags->term_id; } } elseif ( is_tax() ) { $term = $wp_query->get_queried_object(); if ( isset( $term->term_id ) ) { $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy ); $classes[] = 'term-' . sanitize_html_class( $term->slug, $term->term_id ); $classes[] = 'term-' . $term->term_id; } } } elseif ( is_page() ) { $classes[] = 'page'; $page_id = $wp_query->get_queried_object_id(); $post = get_post($page_id); $classes[] = 'page-id-' . $page_id; if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) { $classes[] = 'page-parent'; } if ( $post->post_parent ) { $classes[] = 'page-child'; $classes[] = 'parent-pageid-' . $post->post_parent; } if ( is_page_template() ) { $classes[] = 'page-template'; $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) ); } else { $classes[] = 'page-template-default'; } } if ( is_user_logged_in() ) $classes[] = 'logged-in'; if ( is_admin_bar_showing() ) { $classes[] = 'admin-bar'; $classes[] = 'no-customize-support'; } if ( get_theme_mod( 'background_color' ) || get_background_image() ) $classes[] = 'custom-background'; $page = $wp_query->get( 'page' ); if ( ! $page || $page < 2 ) $page = $wp_query->get( 'paged' ); if ( $page && $page > 1 && ! is_404() ) { $classes[] = 'paged-' . $page; if ( is_single() ) $classes[] = 'single-paged-' . $page; elseif ( is_page() ) $classes[] = 'page-paged-' . $page; elseif ( is_category() ) $classes[] = 'category-paged-' . $page; elseif ( is_tag() ) $classes[] = 'tag-paged-' . $page; elseif ( is_date() ) $classes[] = 'date-paged-' . $page; elseif ( is_author() ) $classes[] = 'author-paged-' . $page; elseif ( is_search() ) $classes[] = 'search-paged-' . $page; elseif ( is_post_type_archive() ) $classes[] = 'post-type-paged-' . $page; } if ( ! empty( $class ) ) { if ( !is_array( $class ) ) $class = preg_split( '#\s+#', $class ); $classes = array_merge( $classes, $class ); } else { // Ensure that we always coerce class to being an array. $class = array(); } $classes = array_map( 'esc_attr', $classes ); /** * Filter the list of CSS body classes for the current post or page. * * @since 2.8.0 * * @param array $classes An array of body classes. * @param string $class A comma-separated list of additional classes added to the body. */ $classes = apply_filters( 'body_class', $classes, $class ); return array_unique( $classes ); } |
関数 post_password_required()
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 | /** * Whether post requires password and correct password has been provided. * * @since 2.7.0 * * @param int|WP_Post $post An optional post. Global $post used if not provided. * @return bool false if a password is not required or the correct password cookie is present, true otherwise. */ function post_password_required( $post = null ) { $post = get_post($post); if ( empty( $post->post_password ) ) return false; if ( ! isset( $_COOKIE['wp-postpass_' . COOKIEHASH] ) ) return true; require_once ABSPATH . WPINC . '/class-phpass.php'; $hasher = new PasswordHash( 8, true ); $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ); if ( 0 !== strpos( $hash, '$P$B' ) ) return true; return ! $hasher->CheckPassword( $post->post_password, $hash ); } |
関数 wp_link_pages()
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 | // // Page Template Functions for usage in Themes // /** * The formatted output of a list of pages. * * Displays page links for paginated posts (i.e. includes the <!--nextpage-->. * Quicktag one or more times). This tag must be within The Loop. * * @since 1.2.0 * * @param string|array $args { * Optional. Array or string of default arguments. * * @type string $before HTML or text to prepend to each link. Default is '<p> Pages:'. * @type string $after HTML or text to append to each link. Default is '</p>'. * @type string $link_before HTML or text to prepend to each link, inside the <a> tag. * Also prepended to the current item, which is not linked. Default empty. * @type string $link_after HTML or text to append to each Pages link inside the <a> tag. * Also appended to the current item, which is not linked. Default empty. * @type string $next_or_number Indicates whether page numbers should be used. Valid values are number * and next. Default is 'number'. * @type string $separator Text between pagination links. Default is ' '. * @type string $nextpagelink Link text for the next page link, if available. Default is 'Next Page'. * @type string $previouspagelink Link text for the previous page link, if available. Default is 'Previous Page'. * @type string $pagelink Format string for page numbers. The % in the parameter string will be * replaced with the page number, so 'Page %' generates "Page 1", "Page 2", etc. * Defaults to '%', just the page number. * @type int|bool $echo Whether to echo or not. Accepts 1|true or 0|false. Default 1|true. * } * @return string Formatted output in HTML. */ function wp_link_pages( $args = '' ) { $defaults = array( 'before' => '<p>' . __( 'Pages:' ), 'after' => '</p>', 'link_before' => '', 'link_after' => '', 'next_or_number' => 'number', 'separator' => ' ', 'nextpagelink' => __( 'Next page' ), 'previouspagelink' => __( 'Previous page' ), 'pagelink' => '%', 'echo' => 1 ); $params = wp_parse_args( $args, $defaults ); /** * Filter the arguments used in retrieving page links for paginated posts. * * @since 3.0.0 * * @param array $params An array of arguments for page links for paginated posts. */ $r = apply_filters( 'wp_link_pages_args', $params ); global $page, $numpages, $multipage, $more; $output = ''; if ( $multipage ) { if ( 'number' == $r['next_or_number'] ) { $output .= $r['before']; for ( $i = 1; $i <= $numpages; $i++ ) { $link = $r['link_before'] . str_replace( '%', $i, $r['pagelink'] ) . $r['link_after']; if ( $i != $page || ! $more && 1 == $page ) { $link = _wp_link_page( $i ) . $link . '</a>'; } /** * Filter the HTML output of individual page number links. * * @since 3.6.0 * * @param string $link The page number HTML output. * @param int $i Page number for paginated posts' page links. */ $link = apply_filters( 'wp_link_pages_link', $link, $i ); $output .= $r['separator'] . $link; } $output .= $r['after']; } elseif ( $more ) { $output .= $r['before']; $prev = $page - 1; if ( $prev ) { $link = _wp_link_page( $prev ) . $r['link_before'] . $r['previouspagelink'] . $r['link_after'] . '</a>'; /** This filter is documented in wp-includes/post-template.php */ $link = apply_filters( 'wp_link_pages_link', $link, $prev ); $output .= $r['separator'] . $link; } $next = $page + 1; if ( $next <= $numpages ) { $link = _wp_link_page( $next ) . $r['link_before'] . $r['nextpagelink'] . $r['link_after'] . '</a>'; /** This filter is documented in wp-includes/post-template.php */ $link = apply_filters( 'wp_link_pages_link', $link, $next ); $output .= $r['separator'] . $link; } $output .= $r['after']; } } /** * Filter the HTML output of page links for paginated posts. * * @since 3.6.0 * * @param string $output HTML output of paginated posts' page links. * @param array $args An array of arguments. */ $html = apply_filters( 'wp_link_pages', $output, $args ); if ( $r['echo'] ) { echo $html; } return $html; } |
関数 _wp_link_page()
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 | /** * Helper function for wp_link_pages(). * * @since 3.1.0 * @access private * * @param int $i Page number. * @return string Link. */ function _wp_link_page( $i ) { global $wp_rewrite; $post = get_post(); if ( 1 == $i ) { $url = get_permalink(); } else { if ( '' == get_option('permalink_structure') || in_array($post->post_status, array('draft', 'pending')) ) $url = add_query_arg( 'page', $i, get_permalink() ); elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') == $post->ID ) $url = trailingslashit(get_permalink()) . user_trailingslashit("$wp_rewrite->pagination_base/" . $i, 'single_paged'); else $url = trailingslashit(get_permalink()) . user_trailingslashit($i, 'single_paged'); } if ( is_preview() ) { $url = add_query_arg( array( 'preview' => 'true' ), $url ); if ( ( 'draft' !== $post->post_status ) && isset( $_GET['preview_id'], $_GET['preview_nonce'] ) ) { $url = add_query_arg( array( 'preview_id' => wp_unslash( $_GET['preview_id'] ), 'preview_nonce' => wp_unslash( $_GET['preview_nonce'] ) ), $url ); } } return '<a href="' . esc_url( $url ) . '">'; } |
関数 post_custom()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // // Post-meta: Custom per-post fields. // /** * Retrieve post custom meta data field. * * @since 1.5.0 * * @param string $key Meta data key name. * @return bool|string|array Array of values or single value, if only one element exists. False will be returned if key does not exist. */ function post_custom( $key = '' ) { $custom = get_post_custom(); if ( !isset( $custom[$key] ) ) return false; elseif ( 1 == count($custom[$key]) ) return $custom[$key][0]; else return $custom[$key]; } |
関数 the_meta()
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 list of post custom fields. * * @internal This will probably change at some point... * @since 1.2.0 * @uses apply_filters() Calls 'the_meta_key' on list item HTML content, with key and value as separate parameters. */ function the_meta() { if ( $keys = get_post_custom_keys() ) { echo "<ul class='post-meta'>\n"; foreach ( (array) $keys as $key ) { $keyt = trim($key); if ( is_protected_meta( $keyt, 'post' ) ) continue; $values = array_map('trim', get_post_custom_values($key)); $value = implode($values,', '); /** * Filter the HTML output of the li element in the post custom fields list. * * @since 2.2.0 * * @param string $html The HTML output for the li element. * @param string $key Meta key. * @param string $value Meta value. */ echo apply_filters( 'the_meta_key', "<li><span class='post-meta-key'>$key:</span> $value</li>\n", $key, $value ); } echo "</ul>\n"; } } |
関数 wp_dropdown_pages()
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 | // // Pages // /** * Retrieve or display list of pages as a dropdown (select list). * * @since 2.1.0 * * @param array|string $args Optional. Override default arguments. * @return string HTML content, if not displaying. */ function wp_dropdown_pages( $args = '' ) { $defaults = array( 'depth' => 0, 'child_of' => 0, 'selected' => 0, 'echo' => 1, 'name' => 'page_id', 'id' => '', 'show_option_none' => '', 'show_option_no_change' => '', 'option_none_value' => '' ); $r = wp_parse_args( $args, $defaults ); $pages = get_pages( $r ); $output = ''; // Back-compat with old system where both id and name were based on $name argument if ( empty( $r['id'] ) ) { $r['id'] = $r['name']; } if ( ! empty( $pages ) ) { $output = "<select name='" . esc_attr( $r['name'] ) . "' id='" . esc_attr( $r['id'] ) . "'>\n"; if ( $r['show_option_no_change'] ) { $output .= "\t<option value=\"-1\">" . $r['show_option_no_change'] . "</option>\n"; } if ( $r['show_option_none'] ) { $output .= "\t<option value=\"" . esc_attr( $r['option_none_value'] ) . '">' . $r['show_option_none'] . "</option>\n"; } $output .= walk_page_dropdown_tree( $pages, $r['depth'], $r ); $output .= "</select>\n"; } /** * Filter the HTML output of a list of pages as a drop down. * * @since 2.1.0 * * @param string $output HTML output for drop down list of pages. */ $html = apply_filters( 'wp_dropdown_pages', $output ); if ( $r['echo'] ) { echo $html; } return $html; } |