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) . '-' . $post->ID;
36 echo '<a id="'.$title.'"></a>';
40 echo '<a id="post-' . $post->ID . '"></a>';
46 function get_permalink($id = 0, $leavename=false) {
54 $leavename? '' : '%postname%',
58 $leavename? '' : '%pagename%',
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, $leavename);
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;
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;
93 if ( strpos($permalink, '%author%') !== false ) {
94 $authordata = get_userdata($post->post_author);
95 $author = $authordata->user_nicename;
98 $date = explode(" ",date('Y m d H i s', $unixtime));
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);
122 // get permalink from post ID
123 function post_permalink($post_id = 0, $deprecated = '') {
124 return get_permalink($post_id);
127 // Respects page_on_front. Use this one.
128 function get_page_link($id = false, $leavename = false) {
133 $id = (int) $post->ID;
135 if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
136 $link = get_option('home');
138 $link = _get_page_link( $id , $leavename );
140 return apply_filters('page_link', $link, $id);
143 // Ignores page_on_front. Internal use only.
144 function _get_page_link( $id = false, $leavename = false ) {
145 global $post, $wp_rewrite;
148 $id = (int) $post->ID;
150 $post = &get_post($id);
152 $pagestruct = $wp_rewrite->get_page_permastruct();
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');
160 $link = get_option('home') . "/?page_id=$id";
163 return apply_filters( '_get_page_link', $link, $id );
166 function get_attachment_link($id = false) {
167 global $post, $wp_rewrite;
172 $id = (int) $post->ID;
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
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
185 $name = $object->post_name;
186 if (strpos($parentlink, '?') === false)
187 $link = trailingslashit($parentlink) . $name . '/';
191 $link = get_bloginfo('url') . "/?attachment_id=$id";
194 return apply_filters('attachment_link', $link, $id);
197 function get_year_link($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);
206 return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
210 function get_month_link($year, $month) {
213 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
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);
222 return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
226 function get_day_link($year, $month, $day) {
229 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
231 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
233 $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
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);
242 return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
246 function get_feed_link($feed = '') {
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();
256 if ( get_default_feed() == $feed )
259 $permalink = str_replace('%feed%', $feed, $permalink);
260 $permalink = preg_replace('#/+#', '/', "/$permalink");
261 $output = get_option('home') . user_trailingslashit($permalink, 'feed');
264 $feed = get_default_feed();
266 if ( false !== strpos($feed, 'comments_') )
267 $feed = str_replace('comments_', 'comments-', $feed);
269 $output = get_option('home') . "/?feed={$feed}";
272 return apply_filters('feed_link', $output, $feed);
275 function get_post_comments_feed_link($post_id = '', $feed = '') {
278 if ( empty($post_id) )
279 $post_id = (int) $id;
282 $feed = get_default_feed();
284 if ( '' != get_option('permalink_structure') ) {
285 $url = trailingslashit( get_permalink($post_id) ) . 'feed';
286 if ( $feed != get_default_feed() )
288 $url = user_trailingslashit($url, 'single_feed');
290 $type = get_post_field('post_type', $post_id);
291 if ( 'page' == $type )
292 $url = get_option('home') . "/?feed=$feed&page_id=$post_id";
294 $url = get_option('home') . "/?feed=$feed&p=$post_id";
297 return apply_filters('post_comments_feed_link', $url);
300 /** post_comments_feed_link() - Output the comment feed link for a post.
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.
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
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');
319 echo "<a href='$url'>$link_text</a>";
322 function get_author_feed_link( $author_id, $feed = '' ) {
323 $author_id = (int) $author_id;
324 $permalink_structure = get_option('permalink_structure');
327 $feed = get_default_feed();
329 if ( '' == $permalink_structure ) {
330 $link = get_option('home') . '?feed=rss2&author=' . $author_id;
332 $link = get_author_posts_url($author_id);
333 $link = trailingslashit($link) . user_trailingslashit('feed', 'feed');
336 $link = apply_filters('author_feed_link', $link);
341 /** get_category_feed_link() - Get the feed link for a given category
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.
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
354 function get_category_feed_link($cat_id, $feed = '') {
355 $cat_id = (int) $cat_id;
357 $category = get_category($cat_id);
359 if ( empty($category) || is_wp_error($category) )
363 $feed = get_default_feed();
365 $permalink_structure = get_option('permalink_structure');
367 if ( '' == $permalink_structure ) {
368 $link = get_option('home') . "?feed=$feed&cat=" . $cat_id;
370 $link = get_category_link($cat_id);
371 if( $feed == get_default_feed() )
374 $feed_link = "feed/$feed";
376 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
379 $link = apply_filters('category_feed_link', $link, $feed);
384 function get_tag_feed_link($tag_id, $feed = '') {
385 $tag_id = (int) $tag_id;
387 $tag = get_tag($tag_id);
389 if ( empty($tag) || is_wp_error($tag) )
392 $permalink_structure = get_option('permalink_structure');
395 $feed = get_default_feed();
397 if ( '' == $permalink_structure ) {
398 $link = get_option('home') . "?feed=$feed&tag=" . $tag->slug;
400 $link = get_tag_link($tag->term_id);
401 if ( $feed == get_default_feed() )
404 $feed_link = "feed/$feed";
405 $link = $link . user_trailingslashit($feed_link, 'feed');
408 $link = apply_filters('tag_feed_link', $link, $feed);
413 function get_search_feed_link($search_query = '', $feed = '') {
414 if ( empty($search_query) )
415 $search = attribute_escape(get_search_query());
417 $search = attribute_escape(stripslashes($search_query));
420 $feed = get_default_feed();
422 $link = get_option('home') . "?s=$search&feed=$feed";
424 $link = apply_filters('search_feed_link', $link);
429 function get_search_comments_feed_link($search_query = '', $feed = '') {
430 if ( empty($search_query) )
431 $search = attribute_escape(get_search_query());
433 $search = attribute_escape(stripslashes($search_query));
436 $feed = get_default_feed();
438 $link = get_option('home') . "?s=$search&feed=comments-$feed";
440 $link = apply_filters('search_feed_link', $link);
445 function get_edit_post_link( $id = 0 ) {
446 if ( !$post = &get_post( $id ) )
449 switch ( $post->post_type ) :
451 if ( !current_user_can( 'edit_page', $post->ID ) )
457 if ( !current_user_can( 'edit_post', $post->ID ) )
460 $var = 'attachment_id';
463 if ( !current_user_can( 'edit_post', $post->ID ) )
470 return apply_filters( 'get_edit_post_link', get_bloginfo( 'wpurl' ) . "/wp-admin/$file.php?action=edit&$var=$post->ID", $post->ID );
473 function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
476 if ( $post->post_type == 'page' ) {
477 if ( !current_user_can( 'edit_page', $post->ID ) )
480 if ( !current_user_can( 'edit_post', $post->ID ) )
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;
488 function get_edit_comment_link( $comment_id = 0 ) {
489 $comment = &get_comment( $comment_id );
490 $post = &get_post( $comment->comment_post_ID );
492 if ( $post->post_type == 'page' ) {
493 if ( !current_user_can( 'edit_page', $post->ID ) )
496 if ( !current_user_can( 'edit_post', $post->ID ) )
500 $location = get_bloginfo( 'wpurl' ) . '/wp-admin/comment.php?action=editcomment&c=' . $comment->comment_ID;
501 return apply_filters( 'get_edit_comment_link', $location );
504 function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
505 global $comment, $post;
507 if ( $post->post_type == 'attachment' ) {
508 } elseif ( $post->post_type == 'page' ) {
509 if ( !current_user_can( 'edit_page', $post->ID ) )
512 if ( !current_user_can( 'edit_post', $post->ID ) )
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;
522 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
523 return get_adjacent_post($in_same_cat, $excluded_categories);
526 function get_next_post($in_same_cat = false, $excluded_categories = '') {
527 return get_adjacent_post($in_same_cat, $excluded_categories, false);
530 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
533 if( empty($post) || !is_single() || is_attachment() )
536 $current_post_date = $post->post_date;
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";
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, ',') . ')';
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 = '';
556 if ( !empty($excluded_categories) ) {
557 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
562 $adjacent = $previous ? 'previous' : 'next';
563 $op = $previous ? '<' : '>';
564 $order = $previous ? 'DESC' : 'ASC';
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" );
570 return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
573 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
574 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
577 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
578 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
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);
585 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
590 $title = $post->post_title;
592 if ( empty($post->post_title) )
593 $title = $previous ? __('Previous Post') : __('Next Post');
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>';
600 $format = str_replace('%link', $link, $format);
605 function get_pagenum_link($pagenum = 1) {
608 $pagenum = (int) $pagenum;
610 $request = remove_query_arg( 'paged' );
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 ), '|' );
616 $request = preg_replace('|^'. $home_root . '|', '', $request);
617 $request = preg_replace('|^/+|', '', $request);
619 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
620 $base = trailingslashit( get_bloginfo( 'home' ) );
622 if ( $pagenum > 1 ) {
623 $result = add_query_arg( 'paged', $pagenum, $base . $request );
625 $result = $base . $request;
628 $qs_regex = '|\?.*?$|';
629 preg_match( $qs_regex, $request, $qs_match );
631 if ( !empty( $qs_match[0] ) ) {
632 $query_string = $qs_match[0];
633 $request = preg_replace( $qs_regex, '', $request );
638 $request = preg_replace( '|page/\d+/?$|', '', $request);
639 $request = preg_replace( '|^index\.php|', '', $request);
640 $request = ltrim($request, '/');
642 $base = trailingslashit( get_bloginfo( 'url' ) );
644 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
645 $base .= 'index.php/';
647 if ( $pagenum > 1 ) {
648 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
651 $result = $base . $request . $query_string;
654 $result = apply_filters('get_pagenum_link', $result);
659 function get_next_posts_page_link($max_page = 0) {
662 if ( !is_single() ) {
665 $nextpage = intval($paged) + 1;
666 if ( !$max_page || $max_page >= $nextpage )
667 return get_pagenum_link($nextpage);
671 function next_posts($max_page = 0) {
672 echo clean_url(get_next_posts_page_link($max_page));
675 function next_posts_link($label='Next Page »', $max_page=0) {
676 global $paged, $wp_query;
678 $max_page = $wp_query->max_num_pages;
682 $nextpage = intval($paged) + 1;
683 if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
685 next_posts($max_page);
686 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
690 function get_previous_posts_page_link() {
693 if ( !is_single() ) {
694 $nextpage = intval($paged) - 1;
697 return get_pagenum_link($nextpage);
701 function previous_posts() {
702 echo clean_url(get_previous_posts_page_link());
705 function previous_posts_link($label='« Previous Page') {
707 if ( (!is_single()) && ($paged > 1) ) {
710 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
714 function posts_nav_link($sep=' — ', $prelabel='« Previous Page', $nxtlabel='Next Page »') {
716 if ( !is_singular() ) {
717 $max_num_pages = $wp_query->max_num_pages;
718 $paged = get_query_var('paged');
720 //only have sep if there's both prev and next results
721 if ($paged < 2 || $paged >= $max_num_pages) {
725 if ( $max_num_pages > 1 ) {
726 previous_posts_link($prelabel);
727 echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
728 next_posts_link($nxtlabel);