WordPressを読む 36-4 /blog/wp-includes/link-template.php 4
2014/12/18
目次
- 1 /blog/wp-includes/link-template.php 4
- 2 関数 get_adjacent_post()
- 3 関数 get_adjacent_post_rel_link()
- 4 関数 adjacent_posts_rel_link()
- 5 関数 adjacent_posts_rel_link_wp_head()
- 6 関数 next_post_rel_link()
- 7 関数 prev_post_rel_link()
- 8 関数 get_boundary_post()
- 9 関数 get_previous_post_link()
- 10 関数 previous_post_link()
- 11 関数 get_next_post_link()
- 12 関数 next_post_link()
- 13 関数 get_adjacent_post_link()
/blog/wp-includes/link-template.php 4
関数 get_adjacent_post()
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 | /** * Retrieve adjacent post. * * Can either be next or previous post. * * @since 2.5.0 * * @param bool $in_same_term Optional. Whether post should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param bool $previous Optional. Whether to retrieve previous post. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. */ function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { global $wpdb; if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) ) return null; $current_post_date = $post->post_date; $join = ''; $where = ''; if ( $in_same_term || ! empty( $excluded_terms ) ) { $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; $where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) { // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and " if ( false !== strpos( $excluded_terms, ' and ' ) ) { _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) ); $excluded_terms = explode( ' and ', $excluded_terms ); } else { $excluded_terms = explode( ',', $excluded_terms ); } $excluded_terms = array_map( 'intval', $excluded_terms ); } if ( $in_same_term ) { if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) return ''; $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); // Remove any exclusions from the term array to include. $term_array = array_diff( $term_array, (array) $excluded_terms ); $term_array = array_map( 'intval', $term_array ); if ( ! $term_array || is_wp_error( $term_array ) ) return ''; $where .= " AND tt.term_id IN (" . implode( ',', $term_array ) . ")"; } if ( ! empty( $excluded_terms ) ) { $where .= " AND p.ID NOT IN ( SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id IN (" . implode( $excluded_terms, ',' ) . ') )'; } } $adjacent = $previous ? 'previous' : 'next'; $op = $previous ? '<' : '>'; $order = $previous ? 'DESC' : 'ASC'; /** * Filter the JOIN clause in the SQL for an adjacent post query. * * The dynamic portion of the hook name, $adjacent, refers to the type * of adjacency, 'next' or 'previous'. * * @since 2.5.0 * * @param string $join The JOIN clause in the SQL. * @param bool $in_same_term Whether post should be in a same taxonomy term. * @param array $excluded_terms Array of excluded term IDs. */ $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms ); /** * Filter the WHERE clause in the SQL for an adjacent post query. * * The dynamic portion of the hook name, $adjacent, refers to the type * of adjacency, 'next' or 'previous'. * * @since 2.5.0 * * @param string $where The WHERE clause in the SQL. * @param bool $in_same_term Whether post should be in a same taxonomy term. * @param array $excluded_terms Array of excluded term IDs. */ $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms ); /** * Filter the ORDER BY clause in the SQL for an adjacent post query. * * The dynamic portion of the hook name, $adjacent, refers to the type * of adjacency, 'next' or 'previous'. * * @since 2.5.0 * * @param string $order_by The ORDER BY clause in the SQL. */ $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort"; $query_key = 'adjacent_post_' . md5( $query ); $result = wp_cache_get( $query_key, 'counts' ); if ( false !== $result ) { if ( $result ) $result = get_post( $result ); return $result; } $result = $wpdb->get_var( $query ); if ( null === $result ) $result = ''; wp_cache_set( $query_key, $result, 'counts' ); if ( $result ) $result = get_post( $result ); return $result; } |
関数 get_adjacent_post_rel_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 | /** * Get adjacent post relational link. * * Can either be next or previous post relational link. * * @since 2.8.0 * * @param string $title Optional. Link title format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param bool $previous Optional. Whether to display link to previous or next post. Default true. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. * @return string */ function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { if ( $previous && is_attachment() && $post = get_post() ) $post = get_post( $post->post_parent ); else $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); if ( empty( $post ) ) return; $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) ); if ( empty( $post_title ) ) $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); $date = mysql2date( get_option( 'date_format' ), $post->post_date ); $title = str_replace( '%title', $post_title, $title ); $title = str_replace( '%date', $date, $title ); $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; $link .= esc_attr( $title ); $link .= "' href='" . get_permalink( $post ) . "' />\n"; $adjacent = $previous ? 'previous' : 'next'; /** * Filter the adjacent post relational link. * * The dynamic portion of the hook name, $adjacent, refers to the type * of adjacency, 'next' or 'previous'. * * @since 2.8.0 * * @param string $link The relational link. */ return apply_filters( "{$adjacent}_post_rel_link", $link ); } |
関数 adjacent_posts_rel_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Display relational links for the posts adjacent to the current post. * * @since 2.8.0 * * @param string $title Optional. Link title format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. */ function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy ); echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy ); } |
関数 adjacent_posts_rel_link_wp_head()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Display relational links for the posts adjacent to the current post for single post pages. * * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates. * @since 3.0.0 * */ function adjacent_posts_rel_link_wp_head() { if ( ! is_single() || is_attachment() ) { return; } adjacent_posts_rel_link(); } |
関数 next_post_rel_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Display relational link for the next post adjacent to the current post. * * @since 2.8.0 * * @param string $title Optional. Link title format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. */ function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy ); } |
関数 prev_post_rel_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Display relational link for the previous post adjacent to the current post. * * @since 2.8.0 * * @param string $title Optional. Link title format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. */ function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy ); } |
関数 get_boundary_post()
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 boundary post. * * Boundary being either the first or last post by publish date within the constraints specified * by $in_same_term or $excluded_terms. * * @since 2.8.0 * * @param bool $in_same_term Optional. Whether returned post should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param bool $start Optional. Whether to retrieve first or last post. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. * @return mixed Array containing the boundary post object if successful, null otherwise. */ function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) { $post = get_post(); if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) ) return null; $query_args = array( 'posts_per_page' => 1, 'order' => $start ? 'ASC' : 'DESC', 'update_post_term_cache' => false, 'update_post_meta_cache' => false ); $term_array = array(); if ( ! is_array( $excluded_terms ) ) { if ( ! empty( $excluded_terms ) ) $excluded_terms = explode( ',', $excluded_terms ); else $excluded_terms = array(); } if ( $in_same_term || ! empty( $excluded_terms ) ) { if ( $in_same_term ) $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); if ( ! empty( $excluded_terms ) ) { $excluded_terms = array_map( 'intval', $excluded_terms ); $excluded_terms = array_diff( $excluded_terms, $term_array ); $inverse_terms = array(); foreach ( $excluded_terms as $excluded_term ) $inverse_terms[] = $excluded_term * -1; $excluded_terms = $inverse_terms; } $query_args[ 'tax_query' ] = array( array( 'taxonomy' => $taxonomy, 'terms' => array_merge( $term_array, $excluded_terms ) ) ); } return get_posts( $query_args ); } |
関数 get_previous_post_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /* * Get previous post link that is adjacent to the current post. * * @since 3.7.0 * * @param string $format Optional. Link anchor format. * @param string $link Optional. Link permalink format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. * @return string */ function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); } |
関数 previous_post_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Display previous post link that is adjacent to the current post. * * @since 1.5.0 * @see get_previous_post_link() * * @param string $format Optional. Link anchor format. * @param string $link Optional. Link permalink format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. */ function previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy ); } |
関数 get_next_post_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Get next post link that is adjacent to the current post. * * @since 3.7.0 * * @param string $format Optional. Link anchor format. * @param string $link Optional. Link permalink format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. * @return string */ function get_next_post_link( $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, false, $taxonomy ); } |
関数 next_post_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Display next post link that is adjacent to the current post. * * @since 1.5.0 * @see get_next_post_link() * * @param string $format Optional. Link anchor format. * @param string $link Optional. Link permalink format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. */ function $format = '%link »', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy ); } |
関数 get_adjacent_post_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 58 59 60 | /** * Get adjacent post link. * * Can be either next post link or previous. * * @since 3.7.0 * * @param string $format Link anchor format. * @param string $link Link permalink format. * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term. * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs. * @param bool $previous Optional. Whether to display link to previous or next post. Default true. * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'. * @return string */ function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { if ( $previous && is_attachment() ) $post = get_post( get_post()->post_parent ); else $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); if ( ! $post ) { $output = ''; } else { $title = $post->post_title; if ( empty( $post->post_title ) ) $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); /** This filter is documented in wp-includes/post-template.php */ $title = apply_filters( 'the_title', $title, $post->ID ); $date = mysql2date( get_option( 'date_format' ), $post->post_date ); $rel = $previous ? 'prev' : 'next'; $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">'; $inlink = str_replace( '%title', $title, $link ); $inlink = str_replace( '%date', $date, $inlink ); $inlink = $string . $inlink . '</a>'; $output = str_replace( '%link', $inlink, $format ); } $adjacent = $previous ? 'previous' : 'next'; /** * Filter the adjacent post link. * * The dynamic portion of the hook name, $adjacent, refers to the type * of adjacency, 'next' or 'previous'. * * @since 2.6.0 * * @param string $output The adjacent post link. * @param string $format Link anchor format. * @param string $link Link permalink format. * @param WP_Post $post The adjacent post. */ return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post ); } |