WordPressを読む 35-3 /blog/wp-includes/general-template.php 3
2014/12/17
目次
/blog/wp-includes/general-template.php 3
関数 single_term_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 | /** * Display or retrieve page title for taxonomy term archive. * * Useful for taxonomy term template files for displaying the taxonomy term page title. * It has less overhead than {@link wp_title()}, because of its limited implementation. * * It does not support placing the separator after the title, but by leaving the * prefix parameter empty, you can set the title separator manually. The prefix * does not automatically place a space between the prefix, so if there should * be a space, the parameter value will need to have it at the end. * * @since 3.1.0 * * @param string $prefix Optional. What to display before the title. * @param bool $display Optional, default is true. Whether to display or retrieve title. * @return string|null Title when retrieving, null when displaying or failure. */ function single_term_title( $prefix = '', $display = true ) { $term = get_queried_object(); if ( !$term ) return; if ( is_category() ) { /** * Filter the category archive page title. * * @since 2.0.10 * * @param string $term_name Category name for archive being displayed. */ $term_name = apply_filters( 'single_cat_title', $term->name ); } elseif ( is_tag() ) { /** * Filter the tag archive page title. * * @since 2.3.0 * * @param string $term_name Tag name for archive being displayed. */ $term_name = apply_filters( 'single_tag_title', $term->name ); } elseif ( is_tax() ) { /** * Filter the custom taxonomy archive page title. * * @since 3.1.0 * * @param string $term_name Term name for archive being displayed. */ $term_name = apply_filters( 'single_term_title', $term->name ); } else { return; } if ( empty( $term_name ) ) return; if ( $display ) echo $prefix . $term_name; else return $prefix . $term_name; } |
関数 single_month_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 | /** * Display or retrieve page title for post archive based on date. * * Useful for when the template only needs to display the month and year, if * either are available. Optimized for just this purpose, so if it is all that * is needed, should be better than {@link wp_title()}. * * It does not support placing the separator after the title, but by leaving the * prefix parameter empty, you can set the title separator manually. The prefix * does not automatically place a space between the prefix, so if there should * be a space, the parameter value will need to have it at the end. * * @since 0.71 * * @param string $prefix Optional. What to display before the title. * @param bool $display Optional, default is true. Whether to display or retrieve title. * @return string|null Title when retrieving, null when displaying or failure. */ function single_month_title($prefix = '', $display = true ) { global $wp_locale; $m = get_query_var('m'); $year = get_query_var('year'); $monthnum = get_query_var('monthnum'); if ( !empty($monthnum) && !empty($year) ) { $my_year = $year; $my_month = $wp_locale->get_month($monthnum); } elseif ( !empty($m) ) { $my_year = substr($m, 0, 4); $my_month = $wp_locale->get_month(substr($m, 4, 2)); } if ( empty($my_month) ) return false; $result = $prefix . $my_month . $prefix . $my_year; if ( !$display ) return $result; echo $result; } |
関数 get_archives_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 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 | /** * Retrieve archive link content based on predefined or custom code. * * The format can be one of four styles. The 'link' for head element, 'option' * for use in the select element, 'html' for use in list (either ol or ul HTML * elements). Custom content is also supported using the before and after * parameters. * * The 'link' format uses the link HTML element with the <em>archives</em> * relationship. The before and after parameters are not used. The text * parameter is used to describe the link. * * The 'option' format uses the option HTML element for use in select element. * The value is the url parameter and the before and after parameters are used * between the text description. * * The 'html' format, which is the default, uses the li HTML element for use in * the list HTML elements. The before parameter is before the link and the after * parameter is after the closing link. * * The custom format uses the before parameter before the link ('a' HTML * element) and the after parameter after the closing link tag. If the above * three values for the format are not used, then custom format is assumed. * * @since 1.0.0 * * @param string $url URL to archive. * @param string $text Archive text description. * @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom. * @param string $before Optional. * @param string $after Optional. * @return string HTML link content for archive. */ function get_archives_link($url, $text, $format = 'html', $before = '', $after = '') { $text = wptexturize($text); $url = esc_url($url); if ('link' == $format) $link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n"; elseif ('option' == $format) $link_html = "\t<option value='$url'>$before $text $after</option>\n"; elseif ('html' == $format) $link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n"; else // custom $link_html = "\t$before<a href='$url'>$text</a>$after\n"; /** * Filter the archive link content. * * @since 2.6.0 * * @param string $link_html The archive HTML link content. */ $link_html = apply_filters( 'get_archives_link', $link_html ); return $link_html; } |
関数 wp_get_archives()
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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | /** * Display archive links based on type and format. * * @since 1.2.0 * * @see get_archives_link() * * @param string|array $args { * Default archive links arguments. Optional. * * @type string $type Type of archive to retrieve. Accepts 'daily', 'weekly', 'monthly', * 'yearly', 'postbypost', or 'alpha'. Both 'postbypost' and 'alpha' * display the same archive link list as well as post titles instead * of displaying dates. The difference between the two is that 'alpha' * will order by post title and 'postbypost' will order by post date. * Default 'monthly'. * @type string|int $limit Number of links to limit the query to. Default empty (no limit). * @type string $format Format each link should take using the $before and $after args. * Accepts 'link' (`<link>` tag), 'option' (`<option>` tag), 'html' * (`<li>` tag), or a custom format, which generates a link anchor * with $before preceding and $after succeeding. Default 'html'. * @type string $before Markup to prepend to the beginning of each link. Default empty. * @type string $after Markup to append to the end of each link. Default empty. * @type bool $show_post_count Whether to display the post count alongside the link. Default false. * @type bool $echo Whether to echo or return the links list. Default 1|true to echo. * @type string $order Whether to use ascending or descending order. Accepts 'ASC', or 'DESC'. * Default 'DESC'. * } * @return string|null String when retrieving, null when displaying. */ function wp_get_archives( $args = '' ) { global $wpdb, $wp_locale; $defaults = array( 'type' => 'monthly', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'order' => 'DESC', ); $r = wp_parse_args( $args, $defaults ); if ( '' == $r['type'] ) { $r['type'] = 'monthly'; } if ( ! empty( $r['limit'] ) ) { $r['limit'] = absint( $r['limit'] ); $r['limit'] = ' LIMIT ' . $r['limit']; } $order = strtoupper( $r['order'] ); if ( $order !== 'ASC' ) { $order = 'DESC'; } // this is what will separate dates on weekly archive links $archive_week_separator = '–'; // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride $archive_date_format_over_ride = 0; // options for daily archive (only if you over-ride the general date format) $archive_day_date_format = 'Y/m/d'; // options for weekly archive (only if you over-ride the general date format) $archive_week_start_date_format = 'Y/m/d'; $archive_week_end_date_format = 'Y/m/d'; if ( ! $archive_date_format_over_ride ) { $archive_day_date_format = get_option( 'date_format' ); $archive_week_start_date_format = get_option( 'date_format' ); $archive_week_end_date_format = get_option( 'date_format' ); } /** * Filter the SQL WHERE clause for retrieving archives. * * @since 2.2.0 * * @param string $sql_where Portion of SQL query containing the WHERE clause. * @param array $r An array of default arguments. */ $where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r ); /** * Filter the SQL JOIN clause for retrieving archives. * * @since 2.2.0 * * @param string $sql_join Portion of SQL query containing JOIN clause. * @param array $r An array of default arguments. */ $join = apply_filters( 'getarchives_join', '', $r ); $output = ''; $last_changed = wp_cache_get( 'last_changed', 'posts' ); if ( ! $last_changed ) { $last_changed = microtime(); wp_cache_set( 'last_changed', $last_changed, 'posts' ); } $limit = $r['limit']; if ( 'monthly' == $r['type'] ) { $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order $limit"; $key = md5( $query ); $key = "wp_get_archives:$key:$last_changed"; if ( ! $results = wp_cache_get( $key, 'posts' ) ) { $results = $wpdb->get_results( $query ); wp_cache_set( $key, $results, 'posts' ); } if ( $results ) { $after = $r['after']; foreach ( (array) $results as $result ) { $url = get_month_link( $result->year, $result->month ); /* translators: 1: month name, 2: 4-digit year */ $text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $result->month ), $result->year ); if ( $r['show_post_count'] ) { $r['after'] = ' (' . $result->posts . ')' . $after; } $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); } } } elseif ( 'yearly' == $r['type'] ) { $query = "SELECT YEAR(post_date) AS `year`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date) ORDER BY post_date $order $limit"; $key = md5( $query ); $key = "wp_get_archives:$key:$last_changed"; if ( ! $results = wp_cache_get( $key, 'posts' ) ) { $results = $wpdb->get_results( $query ); wp_cache_set( $key, $results, 'posts' ); } if ( $results ) { $after = $r['after']; foreach ( (array) $results as $result) { $url = get_year_link( $result->year ); $text = sprintf( '%d', $result->year ); if ( $r['show_post_count'] ) { $r['after'] = ' (' . $result->posts . ')' . $after; } $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); } } } elseif ( 'daily' == $r['type'] ) { $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, DAYOFMONTH(post_date) AS `dayofmonth`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date), DAYOFMONTH(post_date) ORDER BY post_date $order $limit"; $key = md5( $query ); $key = "wp_get_archives:$key:$last_changed"; if ( ! $results = wp_cache_get( $key, 'posts' ) ) { $results = $wpdb->get_results( $query ); $cache[ $key ] = $results; wp_cache_set( $key, $results, 'posts' ); } if ( $results ) { $after = $r['after']; foreach ( (array) $results as $result ) { $url = get_day_link( $result->year, $result->month, $result->dayofmonth ); $date = sprintf( '%1$d-%2$02d-%3$02d 00:00:00', $result->year, $result->month, $result->dayofmonth ); $text = mysql2date( $archive_day_date_format, $date ); if ( $r['show_post_count'] ) { $r['after'] = ' (' . $result->posts . ')' . $after; } $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); } } } elseif ( 'weekly' == $r['type'] ) { $week = _wp_mysql_week( '`post_date`' ); $query = "SELECT DISTINCT $week AS `week`, YEAR( `post_date` ) AS `yr`, DATE_FORMAT( `post_date`, '%Y-%m-%d' ) AS `yyyymmdd`, count( `ID` ) AS `posts` FROM `$wpdb->posts` $join $where GROUP BY $week, YEAR( `post_date` ) ORDER BY `post_date` $order $limit"; $key = md5( $query ); $key = "wp_get_archives:$key:$last_changed"; if ( ! $results = wp_cache_get( $key, 'posts' ) ) { $results = $wpdb->get_results( $query ); wp_cache_set( $key, $results, 'posts' ); } $arc_w_last = ''; if ( $results ) { $after = $r['after']; foreach ( (array) $results as $result ) { if ( $result->week != $arc_w_last ) { $arc_year = $result->yr; $arc_w_last = $result->week; $arc_week = get_weekstartend( $result->yyyymmdd, get_option( 'start_of_week' ) ); $arc_week_start = date_i18n( $archive_week_start_date_format, $arc_week['start'] ); $arc_week_end = date_i18n( $archive_week_end_date_format, $arc_week['end'] ); $url = sprintf( '%1$s/%2$s%3$sm%4$s%5$s%6$sw%7$s%8$d', home_url(), '', '?', '=', $arc_year, '&', '=', $result->week ); $text = $arc_week_start . $archive_week_separator . $arc_week_end; if ( $r['show_post_count'] ) { $r['after'] = ' (' . $result->posts . ')' . $after; } $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); } } } } elseif ( ( 'postbypost' == $r['type'] ) || ('alpha' == $r['type'] ) ) { $orderby = ( 'alpha' == $r['type'] ) ? 'post_title ASC ' : 'post_date DESC '; $query = "SELECT * FROM $wpdb->posts $join $where ORDER BY $orderby $limit"; $key = md5( $query ); $key = "wp_get_archives:$key:$last_changed"; if ( ! $results = wp_cache_get( $key, 'posts' ) ) { $results = $wpdb->get_results( $query ); wp_cache_set( $key, $results, 'posts' ); } if ( $results ) { foreach ( (array) $results as $result ) { if ( $result->post_date != '0000-00-00 00:00:00' ) { $url = get_permalink( $result ); if ( $result->post_title ) { /** This filter is documented in wp-includes/post-template.php */ $text = strip_tags( apply_filters( 'the_title', $result->post_title, $result->ID ) ); } else { $text = $result->ID; } $output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] ); } } } } if ( $r['echo'] ) { echo $output; } else { return $output; } } |
関数 calendar_week_mod()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Get number of days since the start of the week. * * @since 1.5.0 * * @param int $num Number of day. * @return int Days since the start of the week. */ function calendar_week_mod($num) { $base = 7; return ($num - $base*floor($num/$base)); } |