Make WordPress Core

Changeset 2542


Ignore:
Timestamp:
04/18/2005 09:47:35 PM (17 years ago)
Author:
ryan
Message:

In same category support for next and previous post navigation. http://mosquito.wordpress.org/view.php?id=1252 Props: skippy

File:
1 edited

Legend:

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

    r2538 r2542  
    199199
    200200function get_previous_post($in_same_cat = false, $excluded_categories = '') {
    201     global $post, $wpdb;
    202 
    203     if(! is_single()) {
    204       return null;
    205     }
     201    global $post, $wpdb;
     202
     203    if(! is_single()) {
     204        return null;
     205    }
    206206   
    207     $current_post_date = $post->post_date;
    208     $current_category = $post->post_category;
     207    $current_post_date = $post->post_date;
    209208   
    210     $sqlcat = '';
    211     if ($in_same_cat) {
    212       $sqlcat = " AND post_category = '$current_category' ";
    213     }
    214 
    215     $sql_exclude_cats = '';
    216     if (!empty($excluded_categories)) {
    217       $blah = explode('and', $excluded_categories);
    218       foreach($blah as $category) {
    219     $category = intval($category);
    220     $sql_exclude_cats .= " AND post_category != $category";
    221       }
    222     }
    223 
    224     return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
     209    $join = '';
     210    if ($in_same_cat) {
     211        $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
     212        $cat_array = get_the_category($post->ID);
     213        $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
     214        for ($i = 1; $i < (count($cat_array)); $i++) {
     215            $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
     216        }
     217        $join .= ')';
     218    }
     219
     220    $sql_exclude_cats = '';
     221    if (!empty($excluded_categories)) {
     222        $blah = explode('and', $excluded_categories);
     223        foreach($blah as $category) {
     224            $category = intval($category);
     225            $sql_exclude_cats .= " AND post_category != $category";
     226        }
     227    }
     228
     229    return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
    225230}
    226231
    227232function get_next_post($in_same_cat = false, $excluded_categories = '') {
    228     global $post, $wpdb;
    229 
    230     if(! is_single()) {
    231       return null;
    232     }
    233 
    234     $current_post_date = $post->post_date;
    235     $current_category = $post->post_category;
     233    global $post, $wpdb;
     234
     235    if(! is_single()) {
     236        return null;
     237    }
     238
     239    $current_post_date = $post->post_date;
    236240   
    237     $sqlcat = '';
    238     if ($in_same_cat) {
    239       $sqlcat = " AND post_category = '$current_category' ";
    240     }
    241 
    242     $sql_exclude_cats = '';
    243     if (!empty($excluded_categories)) {
    244       $blah = explode('and', $excluded_categories);
    245       foreach($blah as $category) {
    246     $category = intval($category);
    247     $sql_exclude_cats .= " AND post_category != $category";
    248       }
    249     }
    250 
    251     $now = current_time('mysql');
     241    $join = '';
     242    if ($in_same_cat) {
     243        $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
     244        $cat_array = get_the_category($post->ID);
     245        $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
     246        for ($i = 1; $i < (count($cat_array)); $i++) {
     247            $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
     248        }
     249        $join .= ')';
     250    }
     251
     252    $sql_exclude_cats = '';
     253    if (!empty($excluded_categories)) {
     254        $blah = explode('and', $excluded_categories);
     255        foreach($blah as $category) {
     256            $category = intval($category);
     257            $sql_exclude_cats .= " AND post_category != $category";
     258        }
     259    }
     260
     261    $now = current_time('mysql');
    252262   
    253     return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
     263    return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts $join WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
    254264}
    255265
     
    296306// Deprecated.  Use previous_post_link().
    297307function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
    298     global $id, $post, $wpdb;
    299     global $posts, $posts_per_page, $s;
    300 
    301     if(($posts_per_page == 1) || is_single()) {
    302 
    303         $current_post_date = $post->post_date;
    304         $current_category = $post->post_category;
    305 
    306         $sqlcat = '';
    307         if ($in_same_cat != 'no') {
    308             $sqlcat = " AND post_category = '$current_category' ";
    309         }
    310 
    311         $sql_exclude_cats = '';
    312         if (!empty($excluded_categories)) {
    313             $blah = explode('and', $excluded_categories);
    314             foreach($blah as $category) {
    315                 $category = intval($category);
    316                 $sql_exclude_cats .= " AND post_category != $category";
    317             }
    318         }
    319 
    320         $limitprev--;
    321         $lastpost = @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT $limitprev, 1");
    322         if ($lastpost) {
    323             $string = '<a href="'.get_permalink($lastpost->ID).'">'.$previous;
    324             if ($title == 'yes') {
    325                             $string .= apply_filters('the_title', $lastpost->post_title, $lastpost);
    326             }
    327             $string .= '</a>';
    328             $format = str_replace('%', $string, $format);
    329             echo $format;
    330         }
    331     }
     308
     309    if ( empty($in_same_cat) || 'no' == $in_same_cat )
     310        $in_same_cat = false;
     311    else
     312        $in_same_cat = true;
     313
     314  $post = get_previous_post($in_same_cat, $excluded_categories);
     315
     316  if(! $post) {
     317    return;
     318  }
     319
     320    $string = '<a href="'.get_permalink($post->ID).'">'.$previous;
     321    if ($title == 'yes') {
     322        $string .= apply_filters('the_title', $post->post_title, $post);
     323    }
     324    $string .= '</a>';
     325    $format = str_replace('%', $string, $format);
     326    echo $format;
    332327}
    333328
    334329// Deprecated.  Use next_post_link().
    335330function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
    336     global $posts_per_page, $post, $wpdb;
    337     if(1 == $posts_per_page || is_single()) {
    338 
    339         $current_post_date = $post->post_date;
    340         $current_category = $post->post_category;
    341 
    342         $sqlcat = '';
    343         if ($in_same_cat != 'no') {
    344             $sqlcat = " AND post_category='$current_category' ";
    345         }
    346 
    347         $sql_exclude_cats = '';
    348         if (!empty($excluded_categories)) {
    349             $blah = explode('and', $excluded_categories);
    350             foreach($blah as $category) {
    351                 $category = intval($category);
    352                 $sql_exclude_cats .= " AND post_category != $category";
    353             }
    354         }
    355 
    356         $now = current_time('mysql', 1);
    357 
    358         $limitnext--;
    359 
    360         $nextpost = @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts WHERE post_date > '$current_post_date' AND post_date_gmt < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT $limitnext,1");
    361         if ($nextpost) {
    362             $string = '<a href="'.get_permalink($nextpost->ID).'">'.$next;
    363             if ($title=='yes') {
    364                             $string .= apply_filters('the_title', $nextpost->post_title, $nextpost);
    365             }
    366             $string .= '</a>';
    367             $format = str_replace('%', $string, $format);
    368             echo $format;
    369         }
    370     }
     331   
     332    if ( empty($in_same_cat) || 'no' == $in_same_cat )
     333        $in_same_cat = false;
     334    else
     335        $in_same_cat = true;
     336
     337  $post = get_next_post($in_same_cat, $excluded_categories);
     338
     339  if(! $post) {
     340    return;
     341  }
     342
     343    $string = '<a href="'.get_permalink($post->ID).'">'.$next;
     344    if ($title=='yes') {
     345        $string .= apply_filters('the_title', $post->post_title, $nextpost);
     346    }
     347    $string .= '</a>';
     348    $format = str_replace('%', $string, $format);
     349    echo $format;
    371350}
    372351
Note: See TracChangeset for help on using the changeset viewer.