Make WordPress Core


Ignore:
Timestamp:
03/18/2009 09:56:03 PM (15 years ago)
Author:
westi
Message:

Add relational links to next/prev/parent posts. See #8703 props sivel.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/link-template.php

    r10810 r10815  
    910910
    911911/**
     912 * Get adjacent post relational link.
     913 *
     914 * Can either be next or previous post relational link.
     915 *
     916 * @since 2.8.0
     917 *
     918 * @param string $title Optional. Link title format.
     919 * @param bool $in_same_cat Optional. Whether link should be in same category.
     920 * @param string $excluded_categories Optional. Excluded categories IDs.
     921 * @param bool $previous Optional, default is true. Whether display link to previous post.
     922 * @return string
     923 */
     924function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
     925    if ( $previous && is_attachment() )
     926        $post = & get_post($GLOBALS['post']->post_parent);
     927    else
     928        $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous);
     929
     930    if ( empty($post) )
     931        return;
     932
     933    if ( empty($post->post_title) )
     934        $post->post_title = $previous ? __('Previous Post') : __('Next Post');
     935
     936    $date = mysql2date(get_option('date_format'), $post->post_date);
     937   
     938    $title = str_replace('%title', $post->post_title, $title);
     939    $title = str_replace('%date', $date, $title);
     940    $title = apply_filters('the_title', $title, $post);
     941
     942    $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
     943    $link .= $title;
     944    $link .= "' href='" . get_permalink($post) . "' />\n";
     945
     946        $adjacent = $previous ? 'previous' : 'next';
     947        return apply_filters( "{$adjacent}_post_rel_link", $link );
     948}
     949
     950/**
     951 * Display relational links for the posts adjacent to the current post.
     952 *
     953 * @since 2.8.0
     954 *
     955 * @param string $title Optional. Link title format.
     956 * @param bool $in_same_cat Optional. Whether link should be in same category.
     957 * @param string $excluded_categories Optional. Excluded categories IDs.
     958 */
     959function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
     960    echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
     961    echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     962}
     963
     964/**
     965 * Display relational link for the next post adjacent to the current post.
     966 *
     967 * @since 2.8.0
     968 *
     969 * @param string $title Optional. Link title format.
     970 * @param bool $in_same_cat Optional. Whether link should be in same category.
     971 * @param string $excluded_categories Optional. Excluded categories IDs.
     972 */
     973function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
     974    echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     975}
     976
     977/**
     978 * Display relational link for the previous post adjacent to the current post.
     979 *
     980 * @since 2.8.0
     981 *
     982 * @param string $title Optional. Link title format.
     983 * @param bool $in_same_cat Optional. Whether link should be in same category.
     984 * @param string $excluded_categories Optional. Excluded categories IDs.
     985 */
     986function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
     987    echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
     988}
     989
     990/**
     991 * Retrieve boundary post.
     992 *
     993 * Boundary being either the first or last post by publish date within the contraitns specified
     994 * by in same category or excluded categories.
     995 *
     996 * @since 2.8.0
     997 *
     998 * @param bool $in_same_cat Optional. Whether returned post should be in same category.
     999 * @param string $excluded_categories Optional. Excluded categories IDs.
     1000 * @param bool $previous Optional. Whether to retrieve first post.
     1001 * @return object
     1002 */
     1003function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
     1004        global $post, $wpdb;
     1005
     1006        if( empty($post) || !is_single() || is_attachment() )
     1007                return null;
     1008
     1009    $cat_array = array();
     1010    $excluded_categories = array();
     1011        if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
     1012                if ( !empty($in_same_cat) ) {
     1013                        $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
     1014                }
     1015
     1016                if ( !empty($excluded_categories) ) {
     1017                        $excluded_categories = array_map('intval', explode(',', $excluded_categories));
     1018
     1019                        if ( !empty($cat_array) ) {
     1020                                $excluded_categories = array_diff($excluded_categories, $cat_array);
     1021                        }
     1022
     1023                        $inverse_cats = array();
     1024                        foreach ( $excluded_categories as $excluded_category) {
     1025                                $inverse_cats[] = $excluded_category * -1;
     1026                        }
     1027                        $excluded_categories = $inverse_cats;
     1028                }
     1029        }
     1030
     1031    $categories = array_merge($cat_array, $excluded_categories);
     1032
     1033        $order = $start ? 'ASC' : 'DESC';
     1034
     1035    return get_posts("numberposts=1&order=$order&orderby=ID&category=$categories");
     1036}
     1037
     1038/**
     1039 * Get boundary post relational link.
     1040 *
     1041 * Can either be start or end post relational link.
     1042 *
     1043 * @since 2.8.0
     1044 *
     1045 * @param string $title Optional. Link title format.
     1046 * @param bool $in_same_cat Optional. Whether link should be in same category.
     1047 * @param string $excluded_categories Optional. Excluded categories IDs.
     1048 * @param bool $start Optional, default is true. Whether display link to first post.
     1049 * @return string
     1050 */
     1051function get_boundary_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $start = true) {
     1052        $posts = get_boundary_post($in_same_cat,$excluded_categories,$start);
     1053    // Even though we limited get_posts to return only 1 item it still returns an array of objects. 
     1054    $post = $posts[0]; 
     1055
     1056        if ( empty($post) )
     1057                return;
     1058
     1059        if ( empty($post->post_title) )
     1060                $post->post_title = $start ? __('First Post') : __('Last Post');
     1061
     1062        $date = mysql2date(get_option('date_format'), $post->post_date);
     1063
     1064        $title = str_replace('%title', $post->post_title, $title);
     1065        $title = str_replace('%date', $date, $title);
     1066        $title = apply_filters('the_title', $title, $post);
     1067
     1068        $link = $start ? "<link rel='start' title='" : "<link rel='end' title='";
     1069        $link .= $title;
     1070        $link .= "' href='" . get_permalink($post) . "' />\n";
     1071
     1072        $boundary = $start ? 'start' : 'end';
     1073        return apply_filters( "{$boundary}_post_rel_link", $link );
     1074}
     1075
     1076/**
     1077 * Display relational link for the first post.
     1078 *
     1079 * @since 2.8.0
     1080 *
     1081 * @param string $title Optional. Link title format.
     1082 * @param bool $in_same_cat Optional. Whether link should be in same category.
     1083 * @param string $excluded_categories Optional. Excluded categories IDs.
     1084 */
     1085function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
     1086    echo get_boundary_post_rel_link($title, $in_same_cat, $excluded_categories, true);
     1087}
     1088
     1089/**
     1090 * Get site index relational link.
     1091 *
     1092 * @since 2.8.0
     1093 *
     1094 * @return string
     1095 */
     1096function get_index_rel_link() {
     1097    $link = "<link rel='index' title='" . get_bloginfo('name') . "' href='" . get_bloginfo('siteurl') . "' />\n";
     1098    return apply_filters( "index_rel_link", $link );
     1099}
     1100
     1101/**
     1102 * Display relational link for the site index.
     1103 *
     1104 * @since 2.8.0
     1105 */
     1106function index_rel_link() {
     1107    echo get_index_rel_link();
     1108}
     1109
     1110/**
     1111 * Get parent post relational link.
     1112 *
     1113 * @since 2.8.0
     1114 *
     1115 * @param string $title Optional. Link title format.
     1116 * @return string
     1117 */
     1118function get_parent_post_rel_link($title = '%title') {
     1119    if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )
     1120        $post = & get_post($GLOBALS['post']->post_parent);
     1121
     1122    if ( empty($post) )
     1123        return;
     1124
     1125        $date = mysql2date(get_option('date_format'), $post->post_date);
     1126
     1127        $title = str_replace('%title', $post->post_title, $title);
     1128        $title = str_replace('%date', $date, $title);
     1129        $title = apply_filters('the_title', $title, $post);
     1130
     1131        $link = "<link rel='up' title='";
     1132        $link .= $title;
     1133        $link .= "' href='" . get_permalink($post) . "' />\n";
     1134
     1135        return apply_filters( "parent_post_rel_link", $link );
     1136}
     1137
     1138/**
     1139 * Display relational link for parent item
     1140 *
     1141 * @since 2.8.0
     1142 */
     1143function parent_post_rel_link($title = '%title') {
     1144    echo get_parent_post_rel_link($title);
     1145}
     1146
     1147/**
    9121148 * Display previous post link that is adjacent to the current post.
    9131149 *
Note: See TracChangeset for help on using the changeset viewer.