Make WordPress Core

Changeset 1557


Ignore:
Timestamp:
08/24/2004 01:35:57 AM (20 years ago)
Author:
rboren
Message:

Add previous_post_link() and next_post_link().

File:
1 edited

Legend:

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

    r1555 r1557  
    243243// Navigation links
    244244
     245function get_previous_post($in_same_cat = false, $excluded_categories = '') {
     246    global $post, $wpdb;
     247
     248    if(! is_single()) {
     249      return null;
     250    }
     251   
     252    $current_post_date = $post->post_date;
     253    $current_category = $post->post_category;
     254   
     255    $sqlcat = '';
     256    if ($in_same_cat) {
     257      $sqlcat = " AND post_category = '$current_category' ";
     258    }
     259
     260    $sql_exclude_cats = '';
     261    if (!empty($excluded_categories)) {
     262      $blah = explode('and', $excluded_categories);
     263      foreach($blah as $category) {
     264    $category = intval($category);
     265    $sql_exclude_cats .= " AND post_category != $category";
     266      }
     267    }
     268
     269    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");
     270}
     271
     272function get_next_post($in_same_cat = false, $excluded_categories = '') {
     273    global $post, $wpdb;
     274
     275    if(! is_single()) {
     276      return null;
     277    }
     278
     279    $current_post_date = $post->post_date;
     280    $current_category = $post->post_category;
     281   
     282    $sqlcat = '';
     283    if ($in_same_cat) {
     284      $sqlcat = " AND post_category = '$current_category' ";
     285    }
     286
     287    $sql_exclude_cats = '';
     288    if (!empty($excluded_categories)) {
     289      $blah = explode('and', $excluded_categories);
     290      foreach($blah as $category) {
     291    $category = intval($category);
     292    $sql_exclude_cats .= " AND post_category != $category";
     293      }
     294    }
     295
     296    $now = current_time('mysql');
     297   
     298    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");
     299}
     300
     301function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
     302  $post = get_previous_post($in_same_cat, $excluded_categories);
     303
     304  if(! $post) {
     305    return;
     306  }
     307
     308  $title = apply_filters('the_title', $post->post_title);
     309
     310  $string = '<a href="'.get_permalink($post->ID).'">';
     311
     312  $link = str_replace('%title', $title, $link);
     313
     314  $link = $string . $link . '</a>';
     315
     316  $format = str_replace('%link', $link, $format);
     317
     318  echo $format;     
     319}
     320
     321function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
     322  $post = get_next_post($in_same_cat, $excluded_categories);
     323
     324  if(! $post) {
     325    return;
     326  }
     327
     328  $title = apply_filters('the_title', $post->post_title);
     329
     330  $string = '<a href="'.get_permalink($post->ID).'">';
     331
     332  $link = str_replace('%title', $title, $link);
     333
     334  $link = $string . $link . '</a>';
     335
     336  $format = str_replace('%link', $link, $format);
     337
     338  echo $format;     
     339}
     340
    245341function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
    246342    global $id, $post, $wpdb;
Note: See TracChangeset for help on using the changeset viewer.