4 function the_permalink() {
5 echo apply_filters('the_permalink', get_permalink());
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
17 function user_trailingslashit($string, $type_of_url = '') {
19 if ( $wp_rewrite->use_trailing_slashes )
20 $string = trailingslashit($string);
22 $string = untrailingslashit($string);
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);
31 function permalink_anchor($mode = 'id') {
33 switch ( strtolower($mode) ) {
35 $title = sanitize_title($post->post_title) . '-' . $id;
36 echo '<a id="'.$title.'"></a>';
40 echo '<a id="post-' . $post->ID . '"></a>';
46 function get_permalink($id = 0) {
61 $post = &get_post($id);
63 if ( empty($post->ID) ) return FALSE;
65 if ( $post->post_type == 'page' )
66 return get_page_link($post->ID);
67 elseif ($post->post_type == 'attachment')
68 return get_attachment_link($post->ID);
70 $permalink = get_option('permalink_structure');
72 if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
73 $unixtime = strtotime($post->post_date);
76 if (strpos($permalink, '%category%') !== false) {
77 $cats = get_the_category($post->ID);
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;
85 $authordata = get_userdata($post->post_author);
86 $author = $authordata->user_nicename;
87 $date = explode(" ",date('Y m d H i s', $unixtime));
102 $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
103 $permalink = user_trailingslashit($permalink, 'single');
104 return apply_filters('post_link', $permalink, $post);
105 } else { // if they're not using the fancy permalink option
106 $permalink = get_option('home') . '/?p=' . $post->ID;
107 return apply_filters('post_link', $permalink, $post);
111 // get permalink from post ID
112 function post_permalink($post_id = 0, $deprecated = '') {
113 return get_permalink($post_id);
116 // Respects page_on_front. Use this one.
117 function get_page_link($id = false) {
122 $id = (int) $post->ID;
124 if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
125 $link = get_option('home');
127 $link = _get_page_link( $id );
129 return apply_filters('page_link', $link, $id);
132 // Ignores page_on_front. Internal use only.
133 function _get_page_link( $id = false ) {
134 global $post, $wp_rewrite;
137 $id = (int) $post->ID;
139 $pagestruct = $wp_rewrite->get_page_permastruct();
141 if ( '' != $pagestruct && 'draft' != $post->post_status ) {
142 $link = get_page_uri($id);
143 $link = str_replace('%pagename%', $link, $pagestruct);
144 $link = get_option('home') . "/$link";
145 $link = user_trailingslashit($link, 'page');
147 $link = get_option('home') . "/?page_id=$id";
150 return apply_filters( '_get_page_link', $link, $id );
153 function get_attachment_link($id = false) {
154 global $post, $wp_rewrite;
159 $id = (int) $post->ID;
162 $object = get_post($id);
163 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
164 $parent = get_post($object->post_parent);
165 if ( 'page' == $parent->post_type )
166 $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
168 $parentlink = get_permalink( $object->post_parent );
169 if (strpos($parentlink, '?') === false)
170 $link = trailingslashit($parentlink) . $object->post_name . '/';
174 $link = get_bloginfo('url') . "/?attachment_id=$id";
177 return apply_filters('attachment_link', $link, $id);
180 function get_year_link($year) {
183 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
184 $yearlink = $wp_rewrite->get_year_permastruct();
185 if ( !empty($yearlink) ) {
186 $yearlink = str_replace('%year%', $year, $yearlink);
187 return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year);
189 return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
193 function get_month_link($year, $month) {
196 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
198 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
199 $monthlink = $wp_rewrite->get_month_permastruct();
200 if ( !empty($monthlink) ) {
201 $monthlink = str_replace('%year%', $year, $monthlink);
202 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
203 return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
205 return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
209 function get_day_link($year, $month, $day) {
212 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
214 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
216 $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
218 $daylink = $wp_rewrite->get_day_permastruct();
219 if ( !empty($daylink) ) {
220 $daylink = str_replace('%year%', $year, $daylink);
221 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
222 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
223 return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day);
225 return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
229 function get_feed_link($feed='rss2') {
232 $feed_url = get_option('siteurl');
233 $comment_feed_url = $feed_url;
235 $permalink = $wp_rewrite->get_feed_permastruct();
236 if ( '' != $permalink ) {
237 if ( false !== strpos($feed, 'comments_') ) {
238 $feed = str_replace('comments_', '', $feed);
239 $permalink = $wp_rewrite->get_comment_feed_permastruct();
242 if ( 'rss2' == $feed )
245 $permalink = str_replace('%feed%', $feed, $permalink);
246 $permalink = preg_replace('#/+#', '/', "/$permalink");
247 $output = get_option('home') . user_trailingslashit($permalink, 'feed');
249 if ( false !== strpos($feed, 'comments_') )
250 $feed = str_replace('comments_', 'comments-', $feed);
252 $output = get_option('home') . "/?feed={$feed}";
255 return apply_filters('feed_link', $output, $feed);
258 function get_post_comments_feed_link($post_id = '', $feed = 'rss2') {
261 if ( empty($post_id) )
262 $post_id = (int) $id;
264 if ( '' != get_option('permalink_structure') ) {
265 $url = trailingslashit( get_permalink($post_id) ) . 'feed';
266 if ( 'rss2' != $feed )
268 $url = user_trailingslashit($url, 'single_feed');
270 $type = get_post_field('post_type', $post_id);
271 if ( 'page' == $type )
272 $url = get_option('home') . "/?feed=$feed&page_id=$post_id";
274 $url = get_option('home') . "/?feed=$feed&p=$post_id";
277 return apply_filters('post_comments_feed_link', $url);
280 function get_edit_post_link( $id = 0 ) {
281 $post = &get_post( $id );
283 if ( $post->post_type == 'attachment' ) {
285 } elseif ( $post->post_type == 'page' ) {
286 if ( !current_user_can( 'edit_page', $post->ID ) )
291 if ( !current_user_can( 'edit_post', $post->ID ) )
297 return apply_filters( 'get_edit_post_link', get_bloginfo( 'wpurl' ) . '/wp-admin/' . $file . '.php?action=edit&post=' . $post->ID, $post->ID );
300 function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
303 if ( $post->post_type == 'attachment' ) {
305 } elseif ( $post->post_type == 'page' ) {
306 if ( !current_user_can( 'edit_page', $post->ID ) )
311 if ( !current_user_can( 'edit_post', $post->ID ) )
317 $link = '<a href="' . get_edit_post_link( $post->ID ) . '" title="' . __( 'Edit post' ) . '">' . $link . '</a>';
318 echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
321 function get_edit_comment_link( $comment_id = 0 ) {
322 $comment = &get_comment( $comment_id );
323 $post = &get_post( $comment->comment_post_ID );
325 if ( $post->post_type == 'attachment' ) {
327 } elseif ( $post->post_type == 'page' ) {
328 if ( !current_user_can( 'edit_page', $post->ID ) )
331 if ( !current_user_can( 'edit_post', $post->ID ) )
335 $location = get_bloginfo( 'wpurl' ) . '/wp-admin/comment.php?action=editcomment&c=' . $comment->comment_ID;
336 return apply_filters( 'get_edit_comment_link', $location );
339 function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
340 global $comment, $post;
342 if ( $post->post_type == 'attachment' ) {
344 } elseif ( $post->post_type == 'page' ) {
345 if ( !current_user_can( 'edit_page', $post->ID ) )
348 if ( !current_user_can( 'edit_post', $post->ID ) )
352 $link = '<a href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
353 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
358 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
361 if( empty($post) || !is_single() || is_attachment() )
364 $current_post_date = $post->post_date;
367 if ( $in_same_cat ) {
368 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id ";
369 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids');
370 $join .= ' AND (tr.term_taxonomy_id = ' . intval($cat_array[0]);
371 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
372 $join .= ' OR tr.term_taxonomy_id = ' . intval($cat_array[$i]);
377 $sql_exclude_cats = '';
378 if ( !empty($excluded_categories) ) {
379 $blah = explode(' and ', $excluded_categories);
380 $posts_in_ex_cats = get_objects_in_term($blah, 'category');
381 $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
384 $join = apply_filters( 'get_previous_post_join', $join, $in_same_cat, $excluded_categories );
385 $where = apply_filters( 'get_previous_post_where', "WHERE p.post_date < '$current_post_date' AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $in_same_cat, $excluded_categories );
386 $sort = apply_filters( 'get_previous_post_sort', 'ORDER BY p.post_date DESC LIMIT 1' );
388 return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort");
391 function get_next_post($in_same_cat = false, $excluded_categories = '') {
394 if( empty($post) || !is_single() || is_attachment() )
397 $current_post_date = $post->post_date;
400 if ( $in_same_cat ) {
401 $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id ";
402 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids');
403 $join .= ' AND (tr.term_taxonomy_id = ' . intval($cat_array[0]);
404 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
405 $join .= ' OR tr.term_taxonomy_id = ' . intval($cat_array[$i]);
410 $sql_exclude_cats = '';
411 if ( !empty($excluded_categories) ) {
412 $blah = explode(' and ', $excluded_categories);
413 $posts_in_ex_cats = get_objects_in_term($blah, 'category');
414 $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')';
417 $join = apply_filters( 'get_next_post_join', $join, $in_same_cat, $excluded_categories );
418 $where = apply_filters( 'get_next_post_where', "WHERE p.post_date > '$current_post_date' AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql AND p.ID != $post->ID", $in_same_cat, $excluded_categories );
419 $sort = apply_filters( 'get_next_post_sort', 'ORDER BY p.post_date ASC LIMIT 1' );
421 return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort");
425 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
427 if ( is_attachment() )
428 $post = & get_post($GLOBALS['post']->post_parent);
430 $post = get_previous_post($in_same_cat, $excluded_categories);
435 $title = $post->post_title;
437 if ( empty($post->post_title) )
438 $title = __('Previous Post');
440 $title = apply_filters('the_title', $title, $post);
441 $string = '<a href="'.get_permalink($post->ID).'">';
442 $link = str_replace('%title', $title, $link);
443 $link = $pre . $string . $link . '</a>';
445 $format = str_replace('%link', $link, $format);
450 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
451 $post = get_next_post($in_same_cat, $excluded_categories);
456 $title = $post->post_title;
458 if ( empty($post->post_title) )
459 $title = __('Next Post');
461 $title = apply_filters('the_title', $title, $post);
462 $string = '<a href="'.get_permalink($post->ID).'">';
463 $link = str_replace('%title', $title, $link);
464 $link = $string . $link . '</a>';
465 $format = str_replace('%link', $link, $format);
470 function get_pagenum_link($pagenum = 1) {
473 $pagenum = (int) $pagenum;
475 $request = remove_query_arg( 'paged' );
477 $home_root = parse_url(get_option('home'));
478 $home_root = $home_root['path'];
479 $home_root = preg_quote( trailingslashit( $home_root ), '|' );
481 $request = preg_replace('|^'. $home_root . '|', '', $request);
482 $request = preg_replace('|^/+|', '', $request);
484 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
485 $base = trailingslashit( get_bloginfo( 'home' ) );
487 if ( $pagenum > 1 ) {
488 $result = add_query_arg( 'paged', $pagenum, $base . $request );
490 $result = $base . $request;
493 $qs_regex = '|\?.*?$|';
494 preg_match( $qs_regex, $request, $qs_match );
496 if ( $qs_match[0] ) {
497 $query_string = $qs_match[0];
498 $request = preg_replace( $qs_regex, '', $request );
503 $request = preg_replace( '|page/(.+)/?$|', '', $request);
504 $request = preg_replace( '|^index\.php|', '', $request);
505 $request = ltrim($request, '/');
507 $base = trailingslashit( get_bloginfo( 'url' ) );
509 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
510 $base .= 'index.php/';
512 if ( $pagenum > 1 ) {
513 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
516 $result = $base . $request . $query_string;
522 function get_next_posts_page_link($max_page = 0) {
523 global $paged, $pagenow;
525 if ( !is_single() ) {
528 $nextpage = intval($paged) + 1;
529 if ( !$max_page || $max_page >= $nextpage )
530 return get_pagenum_link($nextpage);
534 function next_posts($max_page = 0) {
535 echo clean_url(get_next_posts_page_link($max_page));
538 function next_posts_link($label='Next Page »', $max_page=0) {
539 global $paged, $wpdb, $wp_query;
541 $max_page = $wp_query->max_num_pages;
545 $nextpage = intval($paged) + 1;
546 if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
548 next_posts($max_page);
549 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
553 function get_previous_posts_page_link() {
554 global $paged, $pagenow;
556 if ( !is_single() ) {
557 $nextpage = intval($paged) - 1;
560 return get_pagenum_link($nextpage);
564 function previous_posts() {
565 echo clean_url(get_previous_posts_page_link());
568 function previous_posts_link($label='« Previous Page') {
570 if ( (!is_single()) && ($paged > 1) ) {
573 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
577 function posts_nav_link($sep=' — ', $prelabel='« Previous Page', $nxtlabel='Next Page »') {
579 if ( !is_singular() ) {
580 $max_num_pages = $wp_query->max_num_pages;
581 $paged = get_query_var('paged');
583 //only have sep if there's both prev and next results
584 if ($paged < 2 || $paged >= $max_num_pages) {
588 if ( $max_num_pages > 1 ) {
589 previous_posts_link($prelabel);
590 echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
591 next_posts_link($nxtlabel);