WordPressを読む 36-1 /blog/wp-includes/link-template.php 1
2015/01/14
目次
- 1 /blog/wp-includes/link-template.php
- 2 関数 the_permalink()
- 3 関数 user_trailingslashit()
- 4 関数 permalink_anchor()
- 5 関数 get_the_permalink()
- 6 関数 get_permalink()
- 7 関数 get_post_permalink()
- 8 関数 post_permalink()
- 9 関数 get_page_link()
- 10 関数 _get_page_link()
- 11 関数 get_attachment_link()
- 12 関数 get_year_link()
- 13 関数 get_month_link()
/blog/wp-includes/link-template.php
関数 the_permalink()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php /** * WordPress Link Template Functions * * @package WordPress * @subpackage Template */ /** * Display the permalink for the current post. * * @since 1.2.0 */ function the_permalink() { /** * Filter the display of the permalink for the current post. * * @since 1.5.0 * * @param string $permalink The permalink for the current post. */ echo esc_url( apply_filters( 'the_permalink', get_permalink() ) ); } |
関数 user_trailingslashit()
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 | /** * Retrieve trailing slash string, if blog set for adding trailing slashes. * * Conditionally adds a trailing slash if the permalink structure has a trailing * slash, strips the trailing slash if not. The string is passed through the * 'user_trailingslashit' filter. Will remove trailing slash from string, if * blog is not set to have them. * * @since 2.2.0 * @uses $wp_rewrite * * @param string $string URL with or without a trailing slash. * @param string $type_of_url The type of URL being considered (e.g. single, category, etc) for use in the filter. * @return string */ function user_trailingslashit($string, $type_of_url = '') { global $wp_rewrite; if ( $wp_rewrite->use_trailing_slashes ) $string = trailingslashit($string); else $string = untrailingslashit($string); /** * Filter the trailing slashed string, depending on whether the site is set * to use training slashes. * * @since 2.2.0 * * @param string $string URL with or without a trailing slash. * @param string $type_of_url The type of URL being considered. Accepts 'single', 'single_trackback', * 'single_feed', 'single_paged', 'feed', 'category', 'page', 'year', * 'month', 'day', 'paged', 'post_type_archive'. */ $string = apply_filters( 'user_trailingslashit', $string, $type_of_url ); return $string; } |
関数 permalink_anchor()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Display permalink anchor for current post. * * The permalink mode title will use the post title for the 'a' element 'id' * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute. * * @since 0.71 * * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'. */ function permalink_anchor( $mode = 'id' ) { $post = get_post(); switch ( strtolower( $mode ) ) { case 'title': $title = sanitize_title( $post->post_title ) . '-' . $post->ID; echo '<a id="'.$title.'"></a>'; break; case 'id': default: echo '<a id="post-' . $post->ID . '"></a>'; break; } } |
関数 get_the_permalink()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Retrieve full permalink for current post or post ID. * * This function is an alias for get_permalink(). * * @since 3.9.0 * * @see get_permalink() * * @param int|WP_Post $id Optional. Post ID or post object. Default is the current post. * @param bool $leavename Optional. Whether to keep post name or page name. Default false. * @return string|bool The permalink URL or false if post does not exist. */ function get_the_permalink( $id = 0, $leavename = false ) { return get_permalink( $id, $leavename ); } |
関数 get_permalink()
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 | /** * Retrieve full permalink for current post or post ID. * * @since 1.0.0 * * @param int|WP_Post $id Optional. Post ID or post object. Default current post. * @param bool $leavename Optional. Whether to keep post name or page name. Default false. * @return string|bool The permalink URL or false if post does not exist. */ function get_permalink( $id = 0, $leavename = false ) { $rewritecode = array( '%year%', '%monthnum%', '%day%', '%hour%', '%minute%', '%second%', $leavename? '' : '%postname%', '%post_id%', '%category%', '%author%', $leavename? '' : '%pagename%', ); if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) { $post = $id; $sample = true; } else { $post = get_post($id); $sample = false; } if ( empty($post->ID) ) return false; if ( $post->post_type == 'page' ) return get_page_link($post, $leavename, $sample); elseif ( $post->post_type == 'attachment' ) return get_attachment_link( $post, $leavename ); elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) ) return get_post_permalink($post, $leavename, $sample); $permalink = get_option('permalink_structure'); /** * Filter the permalink structure for a post before token replacement occurs. * * Only applies to posts with post_type of 'post'. * * @since 3.0.0 * * @param string $permalink The site's permalink structure. * @param WP_Post $post The post in question. * @param bool $leavename Whether to keep the post name. */ $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) { $unixtime = strtotime($post->post_date); $category = ''; if ( strpos($permalink, '%category%') !== false ) { $cats = get_the_category($post->ID); if ( $cats ) { usort($cats, '_usort_terms_by_ID'); // order by ID /** * Filter the category that gets used in the %category% permalink token. * * @since 3.5.0 * * @param stdClass $cat The category to use in the permalink. * @param array $cats Array of all categories associated with the post. * @param WP_Post $post The post in question. */ $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post ); $category_object = get_term( $category_object, 'category' ); $category = $category_object->slug; if ( $parent = $category_object->parent ) $category = get_category_parents($parent, false, '/', true) . $category; } // show default category in permalinks, without // having to assign it explicitly if ( empty($category) ) { $default_category = get_term( get_option( 'default_category' ), 'category' ); $category = is_wp_error( $default_category ) ? '' : $default_category->slug; } } $author = ''; if ( strpos($permalink, '%author%') !== false ) { $authordata = get_userdata($post->post_author); $author = $authordata->user_nicename; } $date = explode(" ",date('Y m d H i s', $unixtime)); $rewritereplace = array( $date[0], $date[1], $date[2], $date[3], $date[4], $date[5], $post->post_name, $post->ID, $category, $author, $post->post_name, ); $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) ); $permalink = user_trailingslashit($permalink, 'single'); } else { // if they're not using the fancy permalink option $permalink = home_url('?p=' . $post->ID); } /** * Filter the permalink for a post. * * Only applies to posts with post_type of 'post'. * * @since 1.5.0 * * @param string $permalink The post's permalink. * @param WP_Post $post The post in question. * @param bool $leavename Whether to keep the post name. */ return apply_filters( 'post_link', $permalink, $post, $leavename ); } |
関数 get_post_permalink()
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 | /** * Retrieve the permalink for a post with a custom post type. * * @since 3.0.0 * * @param int $id Optional. Post ID. * @param bool $leavename Optional, defaults to false. Whether to keep post name. * @param bool $sample Optional, defaults to false. Is it a sample permalink. * @return string */ function get_post_permalink( $id = 0, $leavename = false, $sample = false ) { global $wp_rewrite; $post = get_post($id); if ( is_wp_error( $post ) ) return $post; $post_link = $wp_rewrite->get_extra_permastruct($post->post_type); $slug = $post->post_name; $draft_or_pending = isset($post->post_status) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); $post_type = get_post_type_object($post->post_type); if ( !empty($post_link) && ( !$draft_or_pending || $sample ) ) { if ( ! $leavename ) { if ( $post_type->hierarchical ) $slug = get_page_uri($id); $post_link = str_replace("%$post->post_type%", $slug, $post_link); } $post_link = home_url( user_trailingslashit($post_link) ); } else { if ( $post_type->query_var && ( isset($post->post_status) && !$draft_or_pending ) ) $post_link = add_query_arg($post_type->query_var, $slug, ''); else $post_link = add_query_arg(array('post_type' => $post->post_type, 'p' => $post->ID), ''); $post_link = home_url($post_link); } /** * Filter the permalink for a post with a custom post type. * * @since 3.0.0 * * @param string $post_link The post's permalink. * @param WP_Post $post The post in question. * @param bool $leavename Whether to keep the post name. * @param bool $sample Is it a sample permalink. */ return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample ); } |
関数 post_permalink()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Retrieve permalink from post ID. * * @since 1.0.0 * * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post. * @param mixed $deprecated Not used. * @return string */ function post_permalink( $post_id = 0, $deprecated = '' ) { if ( !empty( $deprecated ) ) _deprecated_argument( __FUNCTION__, '1.3' ); return get_permalink($post_id); } |
関数 get_page_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 | /** * Retrieve the permalink for current page or page ID. * * Respects page_on_front. Use this one. * * @since 1.5.0 * * @param int|object $post Optional. Post ID or object. * @param bool $leavename Optional, defaults to false. Whether to keep page name. * @param bool $sample Optional, defaults to false. Is it a sample permalink. * @return string */ function get_page_link( $post = false, $leavename = false, $sample = false ) { $post = get_post( $post ); if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) ) $link = home_url('/'); else $link = _get_page_link( $post, $leavename, $sample ); /** * Filter the permalink for a page. * * @since 1.5.0 * * @param string $link The page's permalink. * @param int $post_id The ID of the page. * @param bool $sample Is it a sample permalink. */ return apply_filters( 'page_link', $link, $post->ID, $sample ); } |
関数 _get_page_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 | /** * Retrieve the page permalink. * * Ignores page_on_front. Internal use only. * * @since 2.1.0 * @access private * * @param int|object $post Optional. Post ID or object. * @param bool $leavename Optional. Leave name. * @param bool $sample Optional. Sample permalink. * @return string */ function _get_page_link( $post = false, $leavename = false, $sample = false ) { global $wp_rewrite; $post = get_post( $post ); $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); $link = $wp_rewrite->get_page_permastruct(); if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) { if ( ! $leavename ) { $link = str_replace('%pagename%', get_page_uri( $post ), $link); } $link = home_url($link); $link = user_trailingslashit($link, 'page'); } else { $link = home_url( '?page_id=' . $post->ID ); } /** * Filter the permalink for a non-page_on_front page. * * @since 2.1.0 * * @param string $link The page's permalink. * @param int $post_id The ID of the page. */ return apply_filters( '_get_page_link', $link, $post->ID ); } |
関数 get_attachment_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 | /** * Retrieve permalink for attachment. * * This can be used in the WordPress Loop or outside of it. * * @since 2.0.0 * * @param int|object $post Optional. Post ID or object. * @param bool $leavename Optional. Leave name. * @return string */ function get_attachment_link( $post = null, $leavename = false ) { global $wp_rewrite; $link = false; $post = get_post( $post ); $parent = ( $post->post_parent > 0 && $post->post_parent != $post->ID ) ? get_post( $post->post_parent ) : false; if ( $wp_rewrite->using_permalinks() && $parent ) { if ( 'page' == $parent->post_type ) $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front else $parentlink = get_permalink( $post->post_parent ); if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') ) $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker else $name = $post->post_name; if ( strpos($parentlink, '?') === false ) $link = user_trailingslashit( trailingslashit($parentlink) . '%postname%' ); if ( ! $leavename ) $link = str_replace( '%postname%', $name, $link ); } if ( ! $link ) $link = home_url( '/?attachment_id=' . $post->ID ); /** * Filter the permalink for an attachment. * * @since 2.0.0 * * @param string $link The attachment's permalink. * @param int $post_id Attachment ID. */ return apply_filters( 'attachment_link', $link, $post->ID ); } |
関数 get_year_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 | /** * Retrieve the permalink for the year archives. * * @since 1.5.0 * * @param int|bool $year False for current year or year for permalink. * @return string */ function get_year_link($year) { global $wp_rewrite; if ( !$year ) $year = gmdate('Y', current_time('timestamp')); $yearlink = $wp_rewrite->get_year_permastruct(); if ( !empty($yearlink) ) { $yearlink = str_replace('%year%', $year, $yearlink); $yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) ); } else { $yearlink = home_url( '?m=' . $year ); } /** * Filter the year archive permalink. * * @since 1.5.0 * * @param string $yearlink Permalink for the year archive. * @param int $year Year for the archive. */ return apply_filters( 'year_link', $yearlink, $year ); } |
関数 get_month_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 | /** * Retrieve the permalink for the month archives with year. * * @since 1.0.0 * * @param bool|int $year False for current year. Integer of year. * @param bool|int $month False for current month. Integer of month. * @return string */ function get_month_link($year, $month) { global $wp_rewrite; if ( !$year ) $year = gmdate('Y', current_time('timestamp')); if ( !$month ) $month = gmdate('m', current_time('timestamp')); $monthlink = $wp_rewrite->get_month_permastruct(); if ( !empty($monthlink) ) { $monthlink = str_replace('%year%', $year, $monthlink); $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink); $monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) ); } else { $monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) ); } /** * Filter the month archive permalink. * * @since 1.5.0 * * @param string $monthlink Permalink for the month archive. * @param int $year Year for the archive. * @param int $month The month for the archive. */ return apply_filters( 'month_link', $monthlink, $year, $month ); } |