3 * WordPress Link Template Functions
10 * Display the permalink for the current post.
13 * @uses apply_filters() Calls 'the_permalink' filter on the permalink string.
15 function the_permalink() {
16 echo apply_filters('the_permalink', get_permalink());
20 * Retrieve trailing slash string, if blog set for adding trailing slashes.
22 * Conditionally adds a trailing slash if the permalink structure has a trailing
23 * slash, strips the trailing slash if not. The string is passed through the
24 * 'user_trailingslashit' filter. Will remove trailing slash from string, if
25 * blog is not set to have them.
30 * @param $string String a URL with or without a trailing slash.
31 * @param $type_of_url String the type of URL being considered (e.g. single, category, etc) for use in the filter.
34 function user_trailingslashit($string, $type_of_url = '') {
36 if ( $wp_rewrite->use_trailing_slashes )
37 $string = trailingslashit($string);
39 $string = untrailingslashit($string);
41 // Note that $type_of_url can be one of following:
42 // single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged
43 $string = apply_filters('user_trailingslashit', $string, $type_of_url);
48 * Display permalink anchor for current post.
50 * The permalink mode title will use the post title for the 'a' element 'id'
51 * attribute. The id mode uses 'post-' with the post ID for the 'id' attribute.
55 * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
57 function permalink_anchor($mode = 'id') {
59 switch ( strtolower($mode) ) {
61 $title = sanitize_title($post->post_title) . '-' . $post->ID;
62 echo '<a id="'.$title.'"></a>';
66 echo '<a id="post-' . $post->ID . '"></a>';
72 * Retrieve full permalink for current post or post ID.
76 * @param int $id Optional. Post ID.
77 * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name.
80 function get_permalink($id = 0, $leavename = false) {
88 $leavename? '' : '%postname%',
92 $leavename? '' : '%pagename%',
95 if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter )
98 $post = &get_post($id);
100 if ( empty($post->ID) ) return false;
102 if ( $post->post_type == 'page' )
103 return get_page_link($post->ID, $leavename);
104 elseif ($post->post_type == 'attachment')
105 return get_attachment_link($post->ID);
107 $permalink = get_option('permalink_structure');
109 if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
110 $unixtime = strtotime($post->post_date);
113 if ( strpos($permalink, '%category%') !== false ) {
114 $cats = get_the_category($post->ID);
116 usort($cats, '_usort_terms_by_ID'); // order by ID
117 $category = $cats[0]->slug;
118 if ( $parent = $cats[0]->parent )
119 $category = get_category_parents($parent, false, '/', true) . $category;
121 // show default category in permalinks, without
122 // having to assign it explicitly
123 if ( empty($category) ) {
124 $default_category = get_category( get_option( 'default_category' ) );
125 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
130 if ( strpos($permalink, '%author%') !== false ) {
131 $authordata = get_userdata($post->post_author);
132 $author = $authordata->user_nicename;
135 $date = explode(" ",date('Y m d H i s', $unixtime));
150 $permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
151 $permalink = user_trailingslashit($permalink, 'single');
152 return apply_filters('post_link', $permalink, $post, $leavename);
153 } else { // if they're not using the fancy permalink option
154 $permalink = get_option('home') . '/?p=' . $post->ID;
155 return apply_filters('post_link', $permalink, $post, $leavename);
160 * Retrieve permalink from post ID.
164 * @param int $post_id Optional. Post ID.
165 * @param mixed $deprecated Not used.
168 function post_permalink($post_id = 0, $deprecated = '') {
169 return get_permalink($post_id);
173 * Retrieve the permalink for current page or page ID.
175 * Respects page_on_front. Use this one.
179 * @param int $id Optional. Post ID.
180 * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name.
183 function get_page_link($id = false, $leavename = false) {
188 $id = (int) $post->ID;
190 if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
191 $link = get_option('home');
193 $link = _get_page_link( $id , $leavename );
195 return apply_filters('page_link', $link, $id);
199 * Retrieve the page permalink.
201 * Ignores page_on_front. Internal use only.
206 * @param int $id Optional. Post ID.
207 * @param bool $leavename Optional. Leave name.
210 function _get_page_link( $id = false, $leavename = false ) {
211 global $post, $wp_rewrite;
214 $id = (int) $post->ID;
216 $post = &get_post($id);
218 $pagestruct = $wp_rewrite->get_page_permastruct();
220 if ( '' != $pagestruct && isset($post->post_status) && 'draft' != $post->post_status ) {
221 $link = get_page_uri($id);
222 $link = ( $leavename ) ? $pagestruct : str_replace('%pagename%', $link, $pagestruct);
223 $link = get_option('home') . "/$link";
224 $link = user_trailingslashit($link, 'page');
226 $link = get_option('home') . "/?page_id=$id";
229 return apply_filters( '_get_page_link', $link, $id );
233 * Retrieve permalink for attachment.
235 * This can be used in the WordPress Loop or outside of it.
239 * @param int $id Optional. Post ID.
242 function get_attachment_link($id = false) {
243 global $post, $wp_rewrite;
248 $id = (int) $post->ID;
251 $object = get_post($id);
252 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) {
253 $parent = get_post($object->post_parent);
254 if ( 'page' == $parent->post_type )
255 $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front
257 $parentlink = get_permalink( $object->post_parent );
258 if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
259 $name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
261 $name = $object->post_name;
262 if (strpos($parentlink, '?') === false)
263 $link = user_trailingslashit( trailingslashit($parentlink) . $name );
267 $link = get_bloginfo('url') . "/?attachment_id=$id";
270 return apply_filters('attachment_link', $link, $id);
274 * Retrieve the permalink for the year archives.
278 * @param int|bool $year False for current year or year for permalink.
281 function get_year_link($year) {
284 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
285 $yearlink = $wp_rewrite->get_year_permastruct();
286 if ( !empty($yearlink) ) {
287 $yearlink = str_replace('%year%', $year, $yearlink);
288 return apply_filters('year_link', get_option('home') . user_trailingslashit($yearlink, 'year'), $year);
290 return apply_filters('year_link', get_option('home') . '/?m=' . $year, $year);
295 * Retrieve the permalink for the month archives with year.
299 * @param bool|int $year False for current year. Integer of year.
300 * @param bool|int $month False for current month. Integer of month.
303 function get_month_link($year, $month) {
306 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
308 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
309 $monthlink = $wp_rewrite->get_month_permastruct();
310 if ( !empty($monthlink) ) {
311 $monthlink = str_replace('%year%', $year, $monthlink);
312 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
313 return apply_filters('month_link', get_option('home') . user_trailingslashit($monthlink, 'month'), $year, $month);
315 return apply_filters('month_link', get_option('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
320 * Retrieve the permalink for the day archives with year and month.
324 * @param bool|int $year False for current year. Integer of year.
325 * @param bool|int $month False for current month. Integer of month.
326 * @param bool|int $day False for current day. Integer of day.
329 function get_day_link($year, $month, $day) {
332 $year = gmdate('Y', time()+(get_option('gmt_offset') * 3600));
334 $month = gmdate('m', time()+(get_option('gmt_offset') * 3600));
336 $day = gmdate('j', time()+(get_option('gmt_offset') * 3600));
338 $daylink = $wp_rewrite->get_day_permastruct();
339 if ( !empty($daylink) ) {
340 $daylink = str_replace('%year%', $year, $daylink);
341 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
342 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
343 return apply_filters('day_link', get_option('home') . user_trailingslashit($daylink, 'day'), $year, $month, $day);
345 return apply_filters('day_link', get_option('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
350 * Retrieve the permalink for the feed type.
354 * @param string $feed Optional, defaults to default feed. Feed type.
357 function get_feed_link($feed = '') {
360 $permalink = $wp_rewrite->get_feed_permastruct();
361 if ( '' != $permalink ) {
362 if ( false !== strpos($feed, 'comments_') ) {
363 $feed = str_replace('comments_', '', $feed);
364 $permalink = $wp_rewrite->get_comment_feed_permastruct();
367 if ( get_default_feed() == $feed )
370 $permalink = str_replace('%feed%', $feed, $permalink);
371 $permalink = preg_replace('#/+#', '/', "/$permalink");
372 $output = get_option('home') . user_trailingslashit($permalink, 'feed');
375 $feed = get_default_feed();
377 if ( false !== strpos($feed, 'comments_') )
378 $feed = str_replace('comments_', 'comments-', $feed);
380 $output = get_option('home') . "/?feed={$feed}";
383 return apply_filters('feed_link', $output, $feed);
387 * Retrieve the permalink for the post comments feed.
391 * @param int $post_id Optional. Post ID.
392 * @param string $feed Optional. Feed type.
395 function get_post_comments_feed_link($post_id = '', $feed = '') {
398 if ( empty($post_id) )
399 $post_id = (int) $id;
402 $feed = get_default_feed();
404 if ( '' != get_option('permalink_structure') ) {
405 $url = trailingslashit( get_permalink($post_id) ) . 'feed';
406 if ( $feed != get_default_feed() )
408 $url = user_trailingslashit($url, 'single_feed');
410 $type = get_post_field('post_type', $post_id);
411 if ( 'page' == $type )
412 $url = get_option('home') . "/?feed=$feed&page_id=$post_id";
414 $url = get_option('home') . "/?feed=$feed&p=$post_id";
417 return apply_filters('post_comments_feed_link', $url);
421 * Display the comment feed link for a post.
423 * Prints out the comment feed link for a post. Link text is placed in the
424 * anchor. If no link text is specified, default text is used. If no post ID is
425 * specified, the current post is used.
431 * @param string $link_text Descriptive text.
432 * @param int $post_id Optional post ID. Default to current post.
433 * @param string $feed Optional. Feed format.
434 * @return string Link to the comment feed for the current post.
436 function post_comments_feed_link( $link_text = '', $post_id = '', $feed = '' ) {
437 $url = get_post_comments_feed_link($post_id, $feed);
438 if ( empty($link_text) )
439 $link_text = __('Comments Feed');
441 echo "<a href='$url'>$link_text</a>";
445 * Retrieve the feed link for a given author.
447 * Returns a link to the feed for all posts by a given author. A specific feed
448 * can be requested or left blank to get the default feed.
454 * @param int $author_id ID of an author.
455 * @param string $feed Optional. Feed type.
456 * @return string Link to the feed for the author specified by $author_id.
458 function get_author_feed_link( $author_id, $feed = '' ) {
459 $author_id = (int) $author_id;
460 $permalink_structure = get_option('permalink_structure');
463 $feed = get_default_feed();
465 if ( '' == $permalink_structure ) {
466 $link = get_option('home') . "?feed=$feed&author=" . $author_id;
468 $link = get_author_posts_url($author_id);
469 if ( $feed == get_default_feed() )
472 $feed_link = "feed/$feed";
474 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
477 $link = apply_filters('author_feed_link', $link, $feed);
483 * Retrieve the feed link for a category.
485 * Returns a link to the feed for all post in a given category. A specific feed
486 * can be requested or left blank to get the default feed.
492 * @param int $cat_id ID of a category.
493 * @param string $feed Optional. Feed type.
494 * @return string Link to the feed for the category specified by $cat_id.
496 function get_category_feed_link($cat_id, $feed = '') {
497 $cat_id = (int) $cat_id;
499 $category = get_category($cat_id);
501 if ( empty($category) || is_wp_error($category) )
505 $feed = get_default_feed();
507 $permalink_structure = get_option('permalink_structure');
509 if ( '' == $permalink_structure ) {
510 $link = get_option('home') . "?feed=$feed&cat=" . $cat_id;
512 $link = get_category_link($cat_id);
513 if( $feed == get_default_feed() )
516 $feed_link = "feed/$feed";
518 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
521 $link = apply_filters('category_feed_link', $link, $feed);
527 * Retrieve permalink for feed of tag.
531 * @param int $tag_id Tag ID.
532 * @param string $feed Optional. Feed type.
535 function get_tag_feed_link($tag_id, $feed = '') {
536 $tag_id = (int) $tag_id;
538 $tag = get_tag($tag_id);
540 if ( empty($tag) || is_wp_error($tag) )
543 $permalink_structure = get_option('permalink_structure');
546 $feed = get_default_feed();
548 if ( '' == $permalink_structure ) {
549 $link = get_option('home') . "?feed=$feed&tag=" . $tag->slug;
551 $link = get_tag_link($tag->term_id);
552 if ( $feed == get_default_feed() )
555 $feed_link = "feed/$feed";
556 $link = trailingslashit($link) . user_trailingslashit($feed_link, 'feed');
559 $link = apply_filters('tag_feed_link', $link, $feed);
565 * Retrieve edit tag link.
569 * @param int $tag_id Tag ID
572 function get_edit_tag_link( $tag_id = 0 ) {
573 $tag = get_term($tag_id, 'post_tag');
575 if ( !current_user_can('manage_categories') )
578 $location = admin_url('edit-tags.php?action=edit&tag_ID=') . $tag->term_id;
579 return apply_filters( 'get_edit_tag_link', $location );
583 * Display or retrieve edit tag link with formatting.
587 * @param string $link Optional. Anchor text.
588 * @param string $before Optional. Display before edit link.
589 * @param string $after Optional. Display after edit link.
590 * @param int|object $tag Tag object or ID
591 * @return string|null HTML content, if $echo is set to false.
593 function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
594 $tag = get_term($tag, 'post_tag');
596 if ( !current_user_can('manage_categories') )
600 $link = __('Edit This');
602 $link = '<a href="' . get_edit_tag_link( $tag->term_id ) . '" title="' . __( 'Edit tag' ) . '">' . $link . '</a>';
603 echo $before . apply_filters( 'edit_tag_link', $link, $tag->term_id ) . $after;
607 * Retrieve the permalink for the feed of the search results.
611 * @param string $search_query Optional. Search query.
612 * @param string $feed Optional. Feed type.
615 function get_search_feed_link($search_query = '', $feed = '') {
616 if ( empty($search_query) )
617 $search = attribute_escape(get_search_query());
619 $search = attribute_escape(stripslashes($search_query));
622 $feed = get_default_feed();
624 $link = get_option('home') . "?s=$search&feed=$feed";
626 $link = apply_filters('search_feed_link', $link);
632 * Retrieve the permalink for the comments feed of the search results.
636 * @param string $search_query Optional. Search query.
637 * @param string $feed Optional. Feed type.
640 function get_search_comments_feed_link($search_query = '', $feed = '') {
641 if ( empty($search_query) )
642 $search = attribute_escape(get_search_query());
644 $search = attribute_escape(stripslashes($search_query));
647 $feed = get_default_feed();
649 $link = get_option('home') . "?s=$search&feed=comments-$feed";
651 $link = apply_filters('search_feed_link', $link);
657 * Retrieve edit posts link for post.
659 * Can be used within the WordPress loop or outside of it. Can be used with
660 * pages, posts, attachments, and revisions.
664 * @param int $id Optional. Post ID.
665 * @param string $context Optional, default to display. How to write the '&', defaults to '&'.
668 function get_edit_post_link( $id = 0, $context = 'display' ) {
669 if ( !$post = &get_post( $id ) )
672 if ( 'display' == $context )
673 $action = 'action=edit&';
675 $action = 'action=edit&';
677 switch ( $post->post_type ) :
679 if ( !current_user_can( 'edit_page', $post->ID ) )
685 if ( !current_user_can( 'edit_post', $post->ID ) )
688 $var = 'attachment_id';
691 if ( !current_user_can( 'edit_post', $post->ID ) )
698 if ( !current_user_can( 'edit_post', $post->ID ) )
705 return apply_filters( 'get_edit_post_link', admin_url("$file.php?{$action}$var=$post->ID"), $post->ID, $context );
709 * Retrieve edit posts link for post.
713 * @param string $link Optional. Anchor text.
714 * @param string $before Optional. Display before edit link.
715 * @param string $after Optional. Display after edit link.
717 function edit_post_link( $link = 'Edit This', $before = '', $after = '' ) {
720 if ( $post->post_type == 'page' ) {
721 if ( !current_user_can( 'edit_page', $post->ID ) )
724 if ( !current_user_can( 'edit_post', $post->ID ) )
728 $link = '<a class="post-edit-link" href="' . get_edit_post_link( $post->ID ) . '" title="' . attribute_escape( __( 'Edit post' ) ) . '">' . $link . '</a>';
729 echo $before . apply_filters( 'edit_post_link', $link, $post->ID ) . $after;
733 * Retrieve edit comment link.
737 * @param int $comment_id Optional. Comment ID.
740 function get_edit_comment_link( $comment_id = 0 ) {
741 $comment = &get_comment( $comment_id );
742 $post = &get_post( $comment->comment_post_ID );
744 if ( $post->post_type == 'page' ) {
745 if ( !current_user_can( 'edit_page', $post->ID ) )
748 if ( !current_user_can( 'edit_post', $post->ID ) )
752 $location = admin_url('comment.php?action=editcomment&c=') . $comment->comment_ID;
753 return apply_filters( 'get_edit_comment_link', $location );
757 * Display or retrieve edit comment link with formatting.
761 * @param string $link Optional. Anchor text.
762 * @param string $before Optional. Display before edit link.
763 * @param string $after Optional. Display after edit link.
764 * @return string|null HTML content, if $echo is set to false.
766 function edit_comment_link( $link = 'Edit This', $before = '', $after = '' ) {
767 global $comment, $post;
769 if ( $post->post_type == 'attachment' ) {
770 } elseif ( $post->post_type == 'page' ) {
771 if ( !current_user_can( 'edit_page', $post->ID ) )
774 if ( !current_user_can( 'edit_post', $post->ID ) )
778 $link = '<a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) . '" title="' . __( 'Edit comment' ) . '">' . $link . '</a>';
779 echo $before . apply_filters( 'edit_comment_link', $link, $comment->comment_ID ) . $after;
783 * Display edit bookmark (literally a URL external to blog) link.
787 * @param int $link Optional. Bookmark ID.
790 function get_edit_bookmark_link( $link = 0 ) {
791 $link = get_bookmark( $link );
793 if ( !current_user_can('manage_links') )
796 $location = admin_url('link.php?action=edit&link_id=') . $link->link_id;
797 return apply_filters( 'get_edit_bookmark_link', $location, $link->link_id );
801 * Display edit bookmark (literally a URL external to blog) link anchor content.
805 * @param string $link Optional. Anchor text.
806 * @param string $before Optional. Display before edit link.
807 * @param string $after Optional. Display after edit link.
808 * @param int $bookmark Optional. Bookmark ID.
810 function edit_bookmark_link( $link = '', $before = '', $after = '', $bookmark = null ) {
811 $bookmark = get_bookmark($bookmark);
813 if ( !current_user_can('manage_links') )
817 $link = __('Edit This');
819 $link = '<a href="' . get_edit_bookmark_link( $link ) . '" title="' . __( 'Edit link' ) . '">' . $link . '</a>';
820 echo $before . apply_filters( 'edit_bookmark_link', $link, $bookmark->link_id ) . $after;
826 * Retrieve previous post link that is adjacent to current post.
830 * @param bool $in_same_cat Optional. Whether link should be in same category.
831 * @param string $excluded_categories Optional. Excluded categories IDs.
834 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
835 return get_adjacent_post($in_same_cat, $excluded_categories);
839 * Retrieve next post link that is adjacent to current post.
843 * @param bool $in_same_cat Optional. Whether link should be in same category.
844 * @param string $excluded_categories Optional. Excluded categories IDs.
847 function get_next_post($in_same_cat = false, $excluded_categories = '') {
848 return get_adjacent_post($in_same_cat, $excluded_categories, false);
852 * Retrieve adjacent post link.
854 * Can either be next or previous post link.
858 * @param bool $in_same_cat Optional. Whether link should be in same category.
859 * @param string $excluded_categories Optional. Excluded categories IDs.
860 * @param bool $previous Optional. Whether to retrieve previous post.
863 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
866 if( empty($post) || !is_single() || is_attachment() )
869 $current_post_date = $post->post_date;
872 $posts_in_ex_cats_sql = '';
873 if ( $in_same_cat || !empty($excluded_categories) ) {
874 $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";
876 if ( $in_same_cat ) {
877 $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
878 $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
881 $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
882 if ( !empty($excluded_categories) ) {
883 $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
884 if ( !empty($cat_array) ) {
885 $excluded_categories = array_diff($excluded_categories, $cat_array);
886 $posts_in_ex_cats_sql = '';
889 if ( !empty($excluded_categories) ) {
890 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
895 $adjacent = $previous ? 'previous' : 'next';
896 $op = $previous ? '<' : '>';
897 $order = $previous ? 'DESC' : 'ASC';
899 $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
900 $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 );
901 $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
903 return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
907 * Display previous post link that is adjacent to the current post.
911 * @param string $format Optional. Link anchor format.
912 * @param string $link Optional. Link permalink format.
913 * @param bool $in_same_cat Optional. Whether link should be in same category.
914 * @param string $excluded_categories Optional. Excluded categories IDs.
916 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
917 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, true);
921 * Display next post link that is adjacent to the current post.
925 * @param string $format Optional. Link anchor format.
926 * @param string $link Optional. Link permalink format.
927 * @param bool $in_same_cat Optional. Whether link should be in same category.
928 * @param string $excluded_categories Optional. Excluded categories IDs.
930 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
931 adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false);
935 * Display adjacent post link.
937 * Can be either next post link or previous.
941 * @param string $format Link anchor format.
942 * @param string $link Link permalink format.
943 * @param bool $in_same_cat Optional. Whether link should be in same category.
944 * @param string $excluded_categories Optional. Excluded categories IDs.
945 * @param bool $previous Optional, default is true. Whether display link to previous post.
947 function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
948 if ( $previous && is_attachment() )
949 $post = & get_post($GLOBALS['post']->post_parent);
951 $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
956 $title = $post->post_title;
958 if ( empty($post->post_title) )
959 $title = $previous ? __('Previous Post') : __('Next Post');
961 $title = apply_filters('the_title', $title, $post);
962 $date = mysql2date(get_option('date_format'), $post->post_date);
964 $string = '<a href="'.get_permalink($post).'">';
965 $link = str_replace('%title', $title, $link);
966 $link = str_replace('%date', $date, $link);
967 $link = $string . $link . '</a>';
969 $format = str_replace('%link', $link, $format);
971 $adjacent = $previous ? 'previous' : 'next';
972 echo apply_filters( "{$adjacent}_post_link", $format, $link );
976 * Retrieve get links for page numbers.
980 * @param int $pagenum Optional. Page ID.
983 function get_pagenum_link($pagenum = 1) {
986 $pagenum = (int) $pagenum;
988 $request = remove_query_arg( 'paged' );
990 $home_root = parse_url(get_option('home'));
991 $home_root = ( isset($home_root['path']) ) ? $home_root['path'] : '';
992 $home_root = preg_quote( trailingslashit( $home_root ), '|' );
994 $request = preg_replace('|^'. $home_root . '|', '', $request);
995 $request = preg_replace('|^/+|', '', $request);
997 if ( !$wp_rewrite->using_permalinks() || is_admin() ) {
998 $base = trailingslashit( get_bloginfo( 'home' ) );
1000 if ( $pagenum > 1 ) {
1001 $result = add_query_arg( 'paged', $pagenum, $base . $request );
1003 $result = $base . $request;
1006 $qs_regex = '|\?.*?$|';
1007 preg_match( $qs_regex, $request, $qs_match );
1009 if ( !empty( $qs_match[0] ) ) {
1010 $query_string = $qs_match[0];
1011 $request = preg_replace( $qs_regex, '', $request );
1016 $request = preg_replace( '|page/\d+/?$|', '', $request);
1017 $request = preg_replace( '|^index\.php|', '', $request);
1018 $request = ltrim($request, '/');
1020 $base = trailingslashit( get_bloginfo( 'url' ) );
1022 if ( $wp_rewrite->using_index_permalinks() && ( $pagenum > 1 || '' != $request ) )
1023 $base .= 'index.php/';
1025 if ( $pagenum > 1 ) {
1026 $request = ( ( !empty( $request ) ) ? trailingslashit( $request ) : $request ) . user_trailingslashit( 'page/' . $pagenum, 'paged' );
1029 $result = $base . $request . $query_string;
1032 $result = apply_filters('get_pagenum_link', $result);
1038 * Retrieve next posts pages link.
1040 * Backported from 2.1.3 to 2.0.10.
1044 * @param int $max_page Optional. Max pages.
1047 function get_next_posts_page_link($max_page = 0) {
1050 if ( !is_single() ) {
1053 $nextpage = intval($paged) + 1;
1054 if ( !$max_page || $max_page >= $nextpage )
1055 return get_pagenum_link($nextpage);
1060 * Display or return the next posts pages link.
1064 * @param int $max_page Optional. Max pages.
1065 * @param boolean $echo Optional. Echo or return;
1067 function next_posts( $max_page = 0, $echo = true ) {
1068 $output = clean_url( get_next_posts_page_link( $max_page ) );
1077 * Return the next posts pages link.
1081 * @param string $label Content for link text.
1082 * @param int $max_page Optional. Max pages.
1083 * @return string|null
1085 function get_next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
1086 global $paged, $wp_query;
1089 $max_page = $wp_query->max_num_pages;
1095 $nextpage = intval($paged) + 1;
1097 if ( !is_single() && ( empty($paged) || $nextpage <= $max_page) ) {
1098 $attr = apply_filters( 'next_posts_link_attributes', '' );
1099 return '<a href="' . next_posts( $max_page, false ) . "\" $attr>". preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
1104 * Display the next posts pages link.
1107 * @uses get_next_posts_link()
1109 * @param string $label Content for link text.
1110 * @param int $max_page Optional. Max pages.
1112 function next_posts_link( $label = 'Next Page »', $max_page = 0 ) {
1113 echo get_next_posts_link( $label, $max_page );
1117 * Retrieve previous post pages link.
1119 * Will only return string, if not on a single page or post.
1121 * Backported to 2.0.10 from 2.1.3.
1125 * @return string|null
1127 function get_previous_posts_page_link() {
1130 if ( !is_single() ) {
1131 $nextpage = intval($paged) - 1;
1132 if ( $nextpage < 1 )
1134 return get_pagenum_link($nextpage);
1139 * Display or return the previous posts pages link.
1143 * @param boolean $echo Optional. Echo or return;
1145 function previous_posts( $echo = true ) {
1146 $output = clean_url( get_previous_posts_page_link() );
1155 * Return the previous posts pages link.
1159 * @param string $label Optional. Previous page link text.
1160 * @return string|null
1162 function get_previous_posts_link( $label = '« Previous Page' ) {
1165 if ( !is_single() && $paged > 1 ) {
1166 $attr = apply_filters( 'previous_posts_link_attributes', '' );
1167 return '<a href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&$1', $label ) .'</a>';
1172 * Display the previous posts page link.
1175 * @uses get_previous_posts_link()
1177 * @param string $label Optional. Previous page link text.
1179 function previous_posts_link( $label = '« Previous Page' ) {
1180 echo get_previous_posts_link( $label );
1184 * Display post pages link navigation for previous and next pages.
1188 * @param string $sep Optional. Separator for posts navigation links.
1189 * @param string $prelabel Optional. Label for previous pages.
1190 * @param string $nxtlabel Optional Label for next pages.
1192 function posts_nav_link( $sep = ' — ', $prelabel = '« Previous Page', $nxtlabel = 'Next Page »' ) {
1194 if ( !is_singular() ) {
1195 $max_num_pages = $wp_query->max_num_pages;
1196 $paged = get_query_var('paged');
1198 //only have sep if there's both prev and next results
1199 if ($paged < 2 || $paged >= $max_num_pages) {
1203 if ( $max_num_pages > 1 ) {
1204 previous_posts_link($prelabel);
1205 echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
1206 next_posts_link($nxtlabel);
1212 * Retrieve page numbers links.
1216 * @param int $pagenum Optional. Page number.
1219 function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
1220 global $post, $wp_rewrite;
1222 $pagenum = (int) $pagenum;
1224 $result = get_permalink( $post->ID );
1226 if ( 'newest' == get_option('default_comments_page') ) {
1227 if ( $pagenum != $max_page ) {
1228 if ( $wp_rewrite->using_permalinks() )
1229 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1231 $result = add_query_arg( 'cpage', $pagenum, $result );
1233 } elseif ( $pagenum > 1 ) {
1234 if ( $wp_rewrite->using_permalinks() )
1235 $result = user_trailingslashit( trailingslashit($result) . 'comment-page-' . $pagenum, 'commentpaged');
1237 $result = add_query_arg( 'cpage', $pagenum, $result );
1240 $result .= '#comments';
1242 $result = apply_filters('get_comments_pagenum_link', $result);
1248 * Return the link to next comments pages.
1252 * @param string $label Optional. Label for link text.
1253 * @param int $max_page Optional. Max page.
1254 * @return string|null
1256 function get_next_comments_link( $label = '', $max_page = 0 ) {
1259 if ( !is_singular() )
1262 $page = get_query_var('cpage');
1264 $nextpage = intval($page) + 1;
1266 if ( empty($max_page) )
1267 $max_page = $wp_query->max_num_comment_pages;
1269 if ( empty($max_page) )
1270 $max_page = get_comment_pages_count();
1272 if ( $nextpage > $max_page )
1275 if ( empty($label) )
1276 $label = __('Newer Comments »');
1278 return '<a href="' . clean_url( get_comments_pagenum_link( $nextpage, $max_page ) ) . '" ' . apply_filters( 'next_comments_link_attributes', '' ) . '>'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
1282 * Display the link to next comments pages.
1286 * @param string $label Optional. Label for link text.
1287 * @param int $max_page Optional. Max page.
1289 function next_comments_link( $label = '', $max_page = 0 ) {
1290 echo get_next_comments_link( $label, $max_page );
1294 * Return the previous comments page link.
1298 * @param string $label Optional. Label for comments link text.
1299 * @return string|null
1301 function get_previous_comments_link( $label = '' ) {
1302 if ( !is_singular() )
1305 $page = get_query_var('cpage');
1307 if ( intval($page) <= 1 )
1310 $prevpage = intval($page) - 1;
1312 if ( empty($label) )
1313 $label = __('« Older Comments');
1315 return '<a href="' . clean_url( get_comments_pagenum_link( $prevpage ) ) . '" ' . apply_filters( 'previous_comments_link_attributes', '' ) . '>' . preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
1319 * Display the previous comments page link.
1323 * @param string $label Optional. Label for comments link text.
1325 function previous_comments_link( $label = '' ) {
1326 echo get_previous_comments_link( $label );
1330 * Create pagination links for the comments on the current post.
1332 * @see paginate_links()
1335 * @param string|array $args Optional args. See paginate_links.
1336 * @return string Markup for pagination links.
1338 function paginate_comments_links($args = array()) {
1339 global $wp_query, $wp_rewrite;
1341 if ( !is_singular() )
1344 $page = get_query_var('cpage');
1347 $max_page = get_comment_pages_count();
1349 'base' => add_query_arg( 'cpage', '%#%' ),
1351 'total' => $max_page,
1354 'add_fragment' => '#comments'
1356 if ( $wp_rewrite->using_permalinks() )
1357 $defaults['base'] = user_trailingslashit(trailingslashit(get_permalink()) . 'comment-page-%#%', 'commentpaged');
1359 $args = wp_parse_args( $args, $defaults );
1360 $page_links = paginate_links( $args );
1362 if ( $args['echo'] )
1369 * Retrieve shortcut link.
1371 * Use this in 'a' element 'href' attribute.
1377 function get_shortcut_link() {
1378 $link = "javascript:
1384 s=(e?e():(k)?k():(x?x.createRange().text:0)),
1385 f='" . admin_url('press-this.php') . "',
1387 e=encodeURIComponent,
1388 g=f+'?u='+e(l.href)+'&t='+e(d.title)+'&s='+e(s)+'&v=2';
1390 if(!w.open(g,'t','toolbar=0,resizable=0,scrollbars=1,status=1,width=720,height=570')){
1394 if (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false)
1395 $link .= 'setTimeout(a,0);';
1399 $link .= "void(0);";
1401 $link = str_replace(array("\r", "\n", "\t"), '', $link);
1403 return apply_filters('shortcut_link', $link);
1407 * Retrieve the site url.
1409 * Returns the 'site_url' option with the appropriate protocol, 'https' if
1410 * is_ssl() and 'http' otherwise. If $scheme is 'http' or 'https', is_ssl() is
1413 * @package WordPress
1416 * @param string $path Optional. Path relative to the site url.
1417 * @param string $scheme Optional. Scheme to give the site url context. Currently 'http','https', 'login', 'login_post', or 'admin'.
1418 * @return string Site url link with optional path appended.
1420 function site_url($path = '', $scheme = null) {
1421 // should the list of allowed schemes be maintained elsewhere?
1422 $orig_scheme = $scheme;
1423 if ( !in_array($scheme, array('http', 'https')) ) {
1424 if ( ('login_post' == $scheme) && ( force_ssl_login() || force_ssl_admin() ) )
1426 elseif ( ('login' == $scheme) && ( force_ssl_admin() ) )
1428 elseif ( ('admin' == $scheme) && force_ssl_admin() )
1431 $scheme = ( is_ssl() ? 'https' : 'http' );
1434 $url = str_replace( 'http://', "{$scheme}://", get_option('siteurl') );
1436 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1437 $url .= '/' . ltrim($path, '/');
1439 return apply_filters('site_url', $url, $path, $orig_scheme);
1443 * Retrieve the url to the admin area.
1445 * @package WordPress
1448 * @param string $path Optional path relative to the admin url
1449 * @return string Admin url link with optional path appended
1451 function admin_url($path = '') {
1452 $url = site_url('wp-admin/', 'admin');
1454 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1455 $url .= ltrim($path, '/');
1461 * Retrieve the url to the includes directory.
1463 * @package WordPress
1466 * @param string $path Optional. Path relative to the includes url.
1467 * @return string Includes url link with optional path appended.
1469 function includes_url($path = '') {
1470 $url = site_url() . '/' . WPINC . '/';
1472 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1473 $url .= ltrim($path, '/');
1479 * Retrieve the url to the content directory.
1481 * @package WordPress
1484 * @param string $path Optional. Path relative to the content url.
1485 * @return string Content url link with optional path appended.
1487 function content_url($path = '') {
1488 $scheme = ( is_ssl() ? 'https' : 'http' );
1489 $url = WP_CONTENT_URL;
1490 if ( 0 === strpos($url, 'http') ) {
1492 $url = str_replace( 'http://', "{$scheme}://", $url );
1495 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1496 $url .= '/' . ltrim($path, '/');
1502 * Retrieve the url to the plugins directory.
1504 * @package WordPress
1507 * @param string $path Optional. Path relative to the plugins url.
1508 * @return string Plugins url link with optional path appended.
1510 function plugins_url($path = '') {
1511 $scheme = ( is_ssl() ? 'https' : 'http' );
1512 $url = WP_PLUGIN_URL;
1513 if ( 0 === strpos($url, 'http') ) {
1515 $url = str_replace( 'http://', "{$scheme}://", $url );
1518 if ( !empty($path) && is_string($path) && strpos($path, '..') === false )
1519 $url .= '/' . ltrim($path, '/');