]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/link-template.php
Wordpress 2.5.1-scripts
[autoinstalls/wordpress.git] / wp-includes / link-template.php
1 <?php
2
3
4 function the_permalink() {
5         echo apply_filters('the_permalink', get_permalink());
6 }
7
8
9 /**
10  * Conditionally adds a trailing slash if the permalink structure
11  * has a trailing slash, strips the trailing slash if not
12  * @global object Uses $wp_rewrite
13  * @param $string string a URL with or without a trailing slash
14  * @param $type_of_url string the type of URL being considered (e.g. single, category, etc) for use in the filter
15  * @return string
16  */
17 function user_trailingslashit($string, $type_of_url = '') {
18         global $wp_rewrite;
19         if ( $wp_rewrite->use_trailing_slashes )
20                 $string = trailingslashit($string);
21         else
22                 $string = untrailingslashit($string);
23
24         // Note that $type_of_url can be one of following:
25         // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged
26         $string = apply_filters('user_trailingslashit', $string, $type_of_url);
27         return $string;
28 }
29
30
31 function permalink_anchor($mode = 'id') {
32         global $post;
33         switch ( strtolower($mode) ) {
34                 case 'title':
35                         $title = sanitize_title($post->post_title) . '-' . $post->ID;
36                         echo '<a id="'.$title.'"></a>';
37                         break;
38                 case 'id':
39                 default:
40                         echo '<a id="post-' . $post->ID . '"></a>';
41                         break;
42         }
43 }
44
45
46 function get_permalink($id = 0, $leavename=false) {
47         $rewritecode = array(
48                 '%year%',
49                 '%monthnum%',
50                 '%day%',
51                 '%hour%',
52                 '%minute%',
53                 '%second%',
54                 $leavename? '' : '%postname%',
55                 '%post_id%',
56                 '%category%',
57                 '%author%',
58                 $leavename? '' : '%pagename%',
59         );
60
61         $post = &get_post($id);
62
63         if ( empty($post->ID) ) return FALSE;
64
65         if ( $post->post_type == 'page' )
66                 return get_page_link($post->ID, $leavename);
67         elseif ($post->post_type == 'attachment')
68                 return get_attachment_link($post->ID);
69
70         $permalink = get_option('permalink_structure');
71
72         if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
73                 $unixtime = strtotime($post->post_date);
74
75                 $category = '';
76                 if ( strpos($permalink, '%category%') !== false ) {
77                         $cats = get_the_category($post->ID);
78                         if ( $cats )
79                                 usort($cats, '_usort_terms_by_ID'); // order by ID
80                         $category = $cats[0]->slug;
81                         if ( $parent=$cats[0]->parent )
82                                 $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
83
84                         // show default category in permalinks, without
85                         // having to assign it explicitly
86                         if ( empty($category) ) {
87                                 $default_category = get_category( get_option( 'default_category' ) );
88                                 $category = is_wp_error( $default_category ) ? '' : $default_category->slug; 
89                         }
90                 }
91
92                 $author = '';
93                 if ( strpos($permalink, '%author%') !== false ) {
94                         $authordata = get_userdata($post->post_author);
95                         $author = $authordata->user_nicename;
96                 }
97
98                 $date = explode(" ",date('Y m d H i s', $unixtime));
99                 $rewritereplace =
100                 array(
101                         $date[0],
102                         $date[1],
103                         $date[2],
104                         $date[3],
105                         $date[4],
106                         $date[5],
107                         $post->post_name,
108                         $post->ID,
109                         $category,
110                         $author,
111                         $post->post_name,
112                 );
113                 $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
114                 $permalink = user_trailingslashit($permalink, 'single');
115                 return apply_filters('post_link', $permalink, $post);
116         } else { // if they're not using the fancy permalink option
117                 $permalink = get_option('home') . '/?p=' . $post->ID;
118                 return apply_filters('post_link', $permalink, $post);
119         }
120 }
121
122 // get permalink from post ID
123 function post_permalink($post_id = 0, $deprecated = '') {
124         return get_permalink($post_id);
125 }
126
127 // Respects page_on_front.  Use this one.
128 function get_page_link($id = false, $leavename = false) {
129         global $post;
130
131         $id = (int) $id;
132         if ( !$id )
133                 $id = (int) $post->ID;
134
135         if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
136                 $link = get_option('home');
137         else
138                 $link = _get_page_link( $id , $leavename );
139
140         return apply_filters('page_link', $link, $id);
141 }
142
143 // Ignores page_on_front.  Internal use only.
144 function _get_page_link( $id = false, $leavename = false ) {
145         global $post, $wp_rewrite;
146
147         if ( !$id )
148                 $id = (int) $post->ID;
149         else
150                 $post = &get_post($id);
151
152         $pagestruct = $wp_rewrite->get_page_permastruct();
153
154         if ( '' != $pagestruct && isset($post->post_status) && 'draft' != $post->post_status ) {
155                 $link = get_page_uri($id);
156                 $link = ( $leavename ) ? $pagestruct : str_replace('%pagename%', $link, $pagestruct);
157                 $link = get_option('home') . "/$link";
158                 $link = user_trailingslashit($link, 'page');
159         } else {
160                 $link = get_option('home') . "/?page_id=$id";
161         }
162
163         return apply_filters( '_get_page_link', $link, $id );
164 }
165
166 function get_attachment_link($id = false) {
167         global $post, $wp_rewrite;
168
169         $link = false;
170
171         if (! $id) {
172                 $id = (int) $post->ID;
173         }
174
175         $object = get_post($id);
176         if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
177                 $parent = get_post($object->post_parent);
178                 if ( 'page' == $parent->post_type )
179                         $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
180                 else
181                         $parentlink = get_permalink( $object->post_parent );
182                 if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
183                         $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
184                 else
185                         $name = $object->post_name;
186                 if (strpos($parentlink, '?') === false)
187                         $link = trailingslashit($parentlink) . $name . '/';
188         }
189
190         if (! $link ) {
191                 $link = get_bloginfo('url') . "/?attachment_id=$id";
192         }
193
194         return apply_filters('attachment_link', $link, $id);
195 }
196
197 function get_year_link($year) {
198         global $wp_rewrite;
199         if ( !$year )
200                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
201         $yearlink = $wp_rewrite->get_year_permastruct();
202         if ( !empty($yearlink) ) {
203                 $yearlink = str_replace('%year%', $year, $yearlink);
204                 return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year);
205         } else {
206                 return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
207         }
208 }
209
210 function get_month_link($year, $month) {
211         global $wp_rewrite;
212         if ( !$year )
213                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
214         if ( !$month )
215                 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
216         $monthlink = $wp_rewrite->get_month_permastruct();
217         if ( !empty($monthlink) ) {
218                 $monthlink = str_replace('%year%', $year, $monthlink);
219                 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
220                 return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
221         } else {
222                 return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
223         }
224 }
225
226 function get_day_link($year, $month, $day) {
227         global $wp_rewrite;
228         if ( !$year )
229                 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
230         if ( !$month )
231                 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
232         if ( !$day )
233                 $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
234
235         $daylink = $wp_rewrite->get_day_permastruct();
236         if ( !empty($daylink) ) {
237                 $daylink = str_replace('%year%', $year, $daylink);
238                 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
239                 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
240                 return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day);
241         } else {
242                 return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
243         }
244 }
245
246 function get_feed_link($feed = '') {
247         global $wp_rewrite;
248
249         $permalink = $wp_rewrite->get_feed_permastruct();
250         if ( '' != $permalink ) {
251                 if ( false !== strpos($feed, 'comments_') ) {
252                         $feed = str_replace('comments_', '', $feed);
253                         $permalink = $wp_rewrite->get_comment_feed_permastruct();
254                 }
255
256                 if ( get_default_feed() == $feed )
257                         $feed = '';
258
259                 $permalink = str_replace('%feed%', $feed, $permalink);
260                 $permalink = preg_replace('#/+#', '/', "/$permalink");
261                 $output =  get_option('home') . user_trailingslashit($permalink, 'feed');
262         } else {
263                 if ( empty($feed) )
264                         $feed = get_default_feed();
265
266                 if ( false !== strpos($feed, 'comments_') )
267                         $feed = str_replace('comments_', 'comments-', $feed);
268
269                 $output = get_option('home') . "/?feed={$feed}";
270         }
271
272         return apply_filters('feed_link', $output, $feed);
273 }
274
275 function get_post_comments_feed_link($post_id = '', $feed = '') {
276         global $id;
277
278         if ( empty($post_id) )
279                 $post_id = (int) $id;
280
281         if ( empty($feed) )
282                 $feed = get_default_feed();
283
284         if ( '' != get_option('permalink_structure') ) {
285                 $url = trailingslashit( get_permalink($post_id) ) . 'feed';
286                 if ( $feed != get_default_feed() )
287                         $url .= "/$feed";
288                 $url = user_trailingslashit($url, 'single_feed');
289         } else {
290                 $type = get_post_field('post_type', $post_id);
291                 if ( 'page' == $type )
292                         $url = get_option('home') . "/?feed=$feed&amp;page_id=$post_id";
293                 else
294                         $url = get_option('home') . "/?feed=$feed&amp;p=$post_id";
295         }
296
297         return apply_filters('post_comments_feed_link', $url);
298 }
299
300 /** post_comments_feed_link() - Output the comment feed link for a post.
301  *
302  * Prints out the comment feed link for a post.  Link text is placed in the
303  * anchor.  If no link text is specified, default text is used.  If no post ID
304  * is specified, the current post is used.
305  *
306  * @package WordPress
307  * @subpackage Feed
308  * @since 2.5
309  *
310  * @param string Descriptive text
311  * @param int Optional post ID.  Default to current post.
312  * @return string Link to the comment feed for the current post
313 */
314 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
315         $url = get_post_comments_feed_link($post_id, $feed);
316         if ( empty($link_text) )
317                 $link_text = __('Comments Feed');
318
319         echo "<a href='$url'>$link_text</a>";
320 }
321
322 function get_author_feed_link( $author_id, $feed = '' ) {
323         $author_id = (int) $author_id;
324         $permalink_structure = get_option('permalink_structure');
325
326         if ( empty($feed) )
327                 $feed = get_default_feed();
328
329         if ( '' == $permalink_structure ) {
330                 $link = get_option('home') . '?feed=rss2&amp;author=' . $author_id;
331         } else {
332                 $link = get_author_posts_url($author_id);
333                 $link = trailingslashit($link) . user_trailingslashit('feed', 'feed');
334         }
335
336         $link = apply_filters('author_feed_link', $link);
337
338         return $link;
339 }
340
341 /** get_category_feed_link() - Get the feed link for a given category
342  *
343  * Returns a link to the feed for all post in a given category.  A specific feed can be requested
344  * or left blank to get the default feed.
345  *
346  * @package WordPress
347  * @subpackage Feed
348  * @since 2.5
349  *
350  * @param int $cat_id ID of a category
351  * @param string $feed Feed type
352  * @return string Link to the feed for the category specified by $cat_id
353 */
354 function get_category_feed_link($cat_id, $feed = '') {
355         $cat_id = (int) $cat_id;
356
357         $category = get_category($cat_id);
358
359         if ( empty($category) || is_wp_error($category) )
360                 return false;
361
362         if ( empty($feed) )
363                 $feed = get_default_feed();
364
365         $permalink_structure = get_option('permalink_structure');
366
367         if ( '' == $permalink_structure ) {
368                 $link = get_option('home') . "?feed=$feed&amp;cat=" . $cat_id;
369         } else {
370                 $link = get_category_link($cat_id);
371                 if( $feed == get_default_feed() )
372                         $feed_link = 'feed';
373                 else
374                         $feed_link = "feed/$feed";
375
376                 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
377         }
378
379         $link = apply_filters('category_feed_link', $link, $feed);
380
381         return $link;
382 }
383
384 function get_tag_feed_link($tag_id, $feed = '') {
385         $tag_id = (int) $tag_id;
386
387         $tag = get_tag($tag_id);
388
389         if ( empty($tag) || is_wp_error($tag) )
390                 return false;
391
392         $permalink_structure = get_option('permalink_structure');
393
394         if ( empty($feed) )
395                 $feed = get_default_feed();
396
397         if ( '' == $permalink_structure ) {
398                 $link = get_option('home') . "?feed=$feed&amp;tag=" . $tag->slug;
399         } else {
400                 $link = get_tag_link($tag->term_id);
401                 if ( $feed == get_default_feed() )
402                         $feed_link = 'feed';
403                 else
404                         $feed_link = "feed/$feed";
405                 $link = $link . user_trailingslashit($feed_link, 'feed');
406         }
407
408         $link = apply_filters('tag_feed_link', $link, $feed);
409
410         return $link;
411 }
412
413 function get_search_feed_link($search_query = '', $feed = '') {
414         if ( empty($search_query) )
415                 $search = attribute_escape(get_search_query());
416         else
417                 $search = attribute_escape(stripslashes($search_query));
418
419         if ( empty($feed) )
420                 $feed = get_default_feed();
421
422         $link = get_option('home') . "?s=$search&amp;feed=$feed";
423
424         $link = apply_filters('search_feed_link', $link);
425
426         return $link;
427 }
428
429 function get_search_comments_feed_link($search_query = '', $feed = '') {
430         if ( empty($search_query) )
431                 $search = attribute_escape(get_search_query());
432         else
433                 $search = attribute_escape(stripslashes($search_query));
434
435         if ( empty($feed) )
436                 $feed = get_default_feed();
437
438         $link = get_option('home') . "?s=$search&amp;feed=comments-$feed";
439
440         $link = apply_filters('search_feed_link', $link);
441
442         return $link;
443 }
444
445 function get_edit_post_link( $id = 0 ) {
446         if ( !$post = &get_post( $id ) )
447                 return;
448
449         switch ( $post->post_type ) :
450         case 'page' :
451                 if ( !current_user_can( 'edit_page', $post->ID ) )
452                         return;
453                 $file = 'page';
454                 $var  = 'post';
455                 break;
456         case 'attachment' :
457                 if ( !current_user_can( 'edit_post', $post->ID ) )
458                         return;
459                 $file = 'media';
460                 $var  = 'attachment_id';
461                 break;
462         default :
463                 if ( !current_user_can( 'edit_post', $post->ID ) )
464                         return;
465                 $file = 'post';
466                 $var  = 'post';
467                 break;
468         endswitch;
469         
470         return apply_filters( 'get_edit_post_link', get_bloginfo( 'wpurl' ) . "/wp-admin/$file.php?action=edit&amp;$var=$post->ID", $post->ID );
471 }
472
473 function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
474         global $post;
475
476         if ( $post->post_type == 'page' ) {
477                 if ( !current_user_can( 'edit_page', $post->ID ) )
478                         return;
479         } else {
480                 if ( !current_user_can( 'edit_post', $post->ID ) )
481                         return;
482         }
483
484         $link = '<a href="' . get_edit_post_link( $post->ID ) . '" title="' . __( 'Edit post' ) . '">' . $link . '</a>';
485         echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
486 }
487
488 function get_edit_comment_link( $comment_id = 0 ) {
489         $comment = &get_comment( $comment_id );
490         $post = &get_post( $comment->comment_post_ID );
491
492         if ( $post->post_type == 'page' ) {
493                 if ( !current_user_can( 'edit_page', $post->ID ) )
494                         return;
495         } else {
496                 if ( !current_user_can( 'edit_post', $post->ID ) )
497                         return;
498         }
499
500         $location = get_bloginfo( 'wpurl' ) . '/wp-admin/comment.php?action=editcomment&amp;c=' . $comment->comment_ID;
501         return apply_filters( 'get_edit_comment_link', $location );
502 }
503
504 function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
505         global $comment, $post;
506
507         if ( $post->post_type == 'attachment' ) {
508         } elseif ( $post->post_type == 'page' ) {
509                 if ( !current_user_can( 'edit_page', $post->ID ) )
510                         return;
511         } else {
512                 if ( !current_user_can( 'edit_post', $post->ID ) )
513                         return;
514         }
515
516         $link = '<a href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
517         echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
518 }
519
520 // Navigation links
521
522 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
523         return get_adjacent_post($in_same_cat, $excluded_categories);
524 }
525
526 function get_next_post($in_same_cat = false, $excluded_categories = '') {
527         return get_adjacent_post($in_same_cat, $excluded_categories, false);
528 }
529
530 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
531         global $post, $wpdb;
532
533         if( empty($post) || !is_single() || is_attachment() )
534                 return null;
535
536         $current_post_date = $post->post_date;
537
538         $join = '';
539         $posts_in_ex_cats_sql = '';
540         if ( $in_same_cat || !empty($excluded_categories) ) {
541                 $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";
542
543                 if ( $in_same_cat ) {
544                         $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
545                         $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode($cat_array, ',') . ')';
546                 }
547
548                 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
549                 if ( !empty($excluded_categories) ) {
550                         $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
551                         if ( !empty($cat_array) ) {
552                                 $excluded_categories = array_diff($excluded_categories, $cat_array);
553                                 $posts_in_ex_cats_sql = '';
554                         }
555
556                         if ( !empty($excluded_categories) ) {
557                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
558                         }
559                 }
560         }
561
562         $adjacent = $previous ? 'previous' : 'next';
563         $op = $previous ? '<' : '>';
564         $order = $previous ? 'DESC' : 'ASC';
565
566         $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
567         $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories );
568         $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
569
570         return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
571 }
572
573 function previous_post_link($format='&laquo; %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
574         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
575 }
576
577 function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat = false, $excluded_categories = '') {
578         adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
579 }
580
581 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
582         if ( $previous && is_attachment() )
583                 $post = & get_post($GLOBALS['post']->post_parent);
584         else
585                 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
586
587         if ( !$post )
588                 return;
589
590         $title = $post->post_title;
591
592         if ( empty($post->post_title) )
593                 $title = $previous ? __('Previous Post') : __('Next Post');
594
595         $title = apply_filters('the_title', $title, $post);
596         $string = '<a href="'.get_permalink($post).'">';
597         $link = str_replace('%title', $title, $link);
598         $link = $string . $link . '</a>';
599
600         $format = str_replace('%link', $link, $format);
601
602         echo $format;
603 }
604
605 function get_pagenum_link($pagenum = 1) {
606         global $wp_rewrite;
607
608         $pagenum = (int) $pagenum;
609
610         $request = remove_query_arg( 'paged' );
611
612         $home_root = parse_url(get_option('home'));
613         $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
614         $home_root = preg_quote( trailingslashit( $home_root ), '|' );
615
616         $request = preg_replace('|^'. $home_root . '|', '', $request);
617         $request = preg_replace('|^/+|', '', $request);
618
619         if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
620                 $base = trailingslashit( get_bloginfo( 'home' ) );
621
622                 if ( $pagenum > 1 ) {
623                         $result = add_query_arg( 'paged', $pagenum, $base . $request );
624                 } else {
625                         $result = $base . $request;
626                 }
627         } else {
628                 $qs_regex = '|\?.*?$|';
629                 preg_match( $qs_regex, $request, $qs_match );
630
631                 if ( !empty( $qs_match[0] ) ) {
632                         $query_string = $qs_match[0];
633                         $request = preg_replace( $qs_regex, '', $request );
634                 } else {
635                         $query_string = '';
636                 }
637
638                 $request = preg_replace( '|page/\d+/?$|', '', $request);
639                 $request = preg_replace( '|^index\.php|', '', $request);
640                 $request = ltrim($request, '/');
641
642                 $base = trailingslashit( get_bloginfo( 'url' ) );
643
644                 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
645                         $base .= 'index.php/';
646
647                 if ( $pagenum > 1 ) {
648                         $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
649                 }
650
651                 $result = $base . $request . $query_string;
652         }
653
654         $result = apply_filters('get_pagenum_link', $result);
655
656         return $result;
657 }
658
659 function get_next_posts_page_link($max_page = 0) {
660         global $paged;
661
662         if ( !is_single() ) {
663                 if ( !$paged )
664                         $paged = 1;
665                 $nextpage = intval($paged) + 1;
666                 if ( !$max_page || $max_page >= $nextpage )
667                         return get_pagenum_link($nextpage);
668         }
669 }
670
671 function next_posts($max_page = 0) {
672         echo clean_url(get_next_posts_page_link($max_page));
673 }
674
675 function next_posts_link($label='Next Page &raquo;', $max_page=0) {
676         global $paged, $wp_query;
677         if ( !$max_page ) {
678                 $max_page = $wp_query->max_num_pages;
679         }
680         if ( !$paged )
681                 $paged = 1;
682         $nextpage = intval($paged) + 1;
683         if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
684                 echo '<a href="';
685                 next_posts($max_page);
686                 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
687         }
688 }
689
690 function get_previous_posts_page_link() {
691         global $paged;
692
693         if ( !is_single() ) {
694                 $nextpage = intval($paged) - 1;
695                 if ( $nextpage < 1 )
696                         $nextpage = 1;
697                 return get_pagenum_link($nextpage);
698         }
699 }
700
701 function previous_posts() {
702         echo clean_url(get_previous_posts_page_link());
703 }
704
705 function previous_posts_link($label='&laquo; Previous Page') {
706         global $paged;
707         if ( (!is_single())     && ($paged > 1) ) {
708                 echo '<a href="';
709                 previous_posts();
710                 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $label) .'</a>';
711         }
712 }
713
714 function posts_nav_link($sep=' &#8212; ', $prelabel='&laquo; Previous Page', $nxtlabel='Next Page &raquo;') {
715         global $wp_query;
716         if ( !is_singular() ) {
717                 $max_num_pages = $wp_query->max_num_pages;
718                 $paged = get_query_var('paged');
719
720                 //only have sep if there's both prev and next results
721                 if ($paged < 2 || $paged >= $max_num_pages) {
722                         $sep = '';
723                 }
724
725                 if ( $max_num_pages > 1 ) {
726                         previous_posts_link($prelabel);
727                         echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&#038;$1', $sep);
728                         next_posts_link($nxtlabel);
729                 }
730         }
731 }
732
733 ?>