4 function the_permalink() {
5 echo apply_filters('the_permalink', get_permalink());
9 function permalink_link() { // For backwards compatibility
10 echo apply_filters('the_permalink', get_permalink());
14 function permalink_anchor($mode = 'id') {
16 switch ( strtolower($mode) ) {
18 $title = sanitize_title($post->post_title) . '-' . $id;
19 echo '<a id="'.$title.'"></a>';
23 echo '<a id="post-' . $post->ID . '"></a>';
29 function get_permalink($id = 0) {
44 $post = &get_post($id);
45 if ( $post->post_status == 'static' )
46 return get_page_link($post->ID);
47 elseif ($post->post_status == 'attachment')
48 return get_attachment_link($post->ID);
50 $permalink = get_settings('permalink_structure');
52 if ( '' != $permalink && 'draft' != $post->post_status ) {
53 $unixtime = strtotime($post->post_date);
56 if ( strstr($permalink, '%category%') ) {
57 $cats = get_the_category($post->ID);
58 $category = $cats[0]->category_nicename;
59 if ( $parent=$cats[0]->category_parent )
60 $category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
63 $authordata = get_userdata($post->post_author);
64 $author = $authordata->user_nicename;
65 $date = explode(" ",date('Y m d H i s', $unixtime));
80 return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
81 } else { // if they're not using the fancy permalink option
82 $permalink = get_settings('home') . '/?p=' . $post->ID;
83 return apply_filters('post_link', $permalink, $post);
87 function get_page_link($id = false) {
88 global $post, $wp_rewrite;
92 $id = (int) $post->ID;
94 $pagestruct = $wp_rewrite->get_page_permastruct();
96 if ( '' != $pagestruct ) {
97 $link = get_page_uri($id);
98 $link = str_replace('%pagename%', $link, $pagestruct);
99 $link = get_settings('home') . "/$link/";
101 $link = get_settings('home') . "/?page_id=$id";
104 return apply_filters('page_link', $link, $id);
107 function get_attachment_link($id = false) {
108 global $post, $wp_rewrite;
113 $id = (int) $post->ID;
116 $object = get_post($id);
117 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) ) {
118 $parent = get_post($object->post_parent);
119 $parentlink = get_permalink($object->post_parent);
120 if (! strstr($parentlink, '?') )
121 $link = trim($parentlink, '/') . '/' . $object->post_name . '/';
125 $link = get_bloginfo('home') . "/?attachment_id=$id";
128 return apply_filters('attachment_link', $link, $id);
131 function get_year_link($year) {
134 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
135 $yearlink = $wp_rewrite->get_year_permastruct();
136 if ( !empty($yearlink) ) {
137 $yearlink = str_replace('%year%', $year, $yearlink);
138 return apply_filters('year_link', get_settings('home') . trailingslashit($yearlink), $year);
140 return apply_filters('year_link', get_settings('home') . '/?m=' . $year, $year);
144 function get_month_link($year, $month) {
147 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
149 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
150 $monthlink = $wp_rewrite->get_month_permastruct();
151 if ( !empty($monthlink) ) {
152 $monthlink = str_replace('%year%', $year, $monthlink);
153 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
154 return apply_filters('month_link', get_settings('home') . trailingslashit($monthlink), $year, $month);
156 return apply_filters('month_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
160 function get_day_link($year, $month, $day) {
163 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
165 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
167 $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
169 $daylink = $wp_rewrite->get_day_permastruct();
170 if ( !empty($daylink) ) {
171 $daylink = str_replace('%year%', $year, $daylink);
172 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
173 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
174 return apply_filters('day_link', get_settings('home') . trailingslashit($daylink), $year, $month, $day);
176 return apply_filters('day_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
180 function get_feed_link($feed='rss2') {
183 $feed_url = get_settings('siteurl');
184 $comment_feed_url = $feed_url;
186 $permalink = $wp_rewrite->get_feed_permastruct();
187 if ( '' != $permalink ) {
188 if ( false !== strpos($feed, 'comments_') ) {
189 $feed = str_replace('comments_', '', $feed);
190 $permalink = $wp_rewrite->get_comment_feed_permastruct();
193 if ( 'rss2' == $feed )
196 $permalink = str_replace('%feed%', $feed, $permalink);
197 $permalink = preg_replace('#/+#', '/', "/$permalink/");
198 $output = get_settings('home') . $permalink;
200 if ( false !== strpos($feed, 'comments_') )
201 $feed = str_replace('comments_', 'comments-', $feed);
203 $output = get_settings('home') . "/?feed={$feed}";
206 return apply_filters('feed_link', $output, $feed);
209 function edit_post_link($link = 'Edit This', $before = '', $after = '') {
212 if ( ! current_user_can('edit_post', $post->ID) )
215 if ( is_attachment() )
220 $location = get_settings('siteurl') . "/wp-admin/{$file}.php?action=edit&post=$post->ID";
221 echo $before . "<a href=\"$location\">$link</a>" . $after;
224 function edit_comment_link($link = 'Edit This', $before = '', $after = '') {
225 global $post, $comment;
227 if ( ! current_user_can('edit_post', $post->ID) )
230 $location = get_settings('siteurl') . "/wp-admin/post.php?action=editcomment&comment=$comment->comment_ID";
231 echo $before . "<a href='$location'>$link</a>" . $after;
236 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
239 if( !is_single() || is_attachment() )
242 $current_post_date = $post->post_date;
245 if ( $in_same_cat ) {
246 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
247 $cat_array = get_the_category($post->ID);
248 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
249 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
250 $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
255 $sql_exclude_cats = '';
256 if ( !empty($excluded_categories) ) {
257 $blah = explode('and', $excluded_categories);
258 foreach ( $blah as $category ) {
259 $category = intval($category);
260 $sql_exclude_cats .= " AND post_category != $category";
264 return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join WHERE post_date < '$current_post_date' AND post_status = 'publish' $sqlcat $sql_exclude_cats ORDER BY post_date DESC LIMIT 1");
267 function get_next_post($in_same_cat = false, $excluded_categories = '') {
270 if( !is_single() || is_attachment() )
273 $current_post_date = $post->post_date;
276 if ( $in_same_cat ) {
277 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
278 $cat_array = get_the_category($post->ID);
279 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
280 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
281 $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
286 $sql_exclude_cats = '';
287 if ( !empty($excluded_categories) ) {
288 $blah = explode('and', $excluded_categories);
289 foreach ( $blah as $category ) {
290 $category = intval($category);
291 $sql_exclude_cats .= " AND post_category != $category";
295 $now = current_time('mysql');
297 return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts $join WHERE post_date > '$current_post_date' AND post_date < '$now' AND post_status = 'publish' $sqlcat $sql_exclude_cats AND ID != $post->ID ORDER BY post_date ASC LIMIT 1");
301 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
303 if ( is_attachment() )
304 $post = & get_post($GLOBALS['post']->post_parent);
306 $post = get_previous_post($in_same_cat, $excluded_categories);
311 $title = apply_filters('the_title', $post->post_title, $post);
312 $string = '<a href="'.get_permalink($post->ID).'">';
313 $link = str_replace('%title', $title, $link);
314 $link = $pre . $string . $link . '</a>';
316 $format = str_replace('%link', $link, $format);
321 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
322 $post = get_next_post($in_same_cat, $excluded_categories);
327 $title = apply_filters('the_title', $post->post_title, $post);
328 $string = '<a href="'.get_permalink($post->ID).'">';
329 $link = str_replace('%title', $title, $link);
330 $link = $string . $link . '</a>';
331 $format = str_replace('%link', $link, $format);
337 // Deprecated. Use previous_post_link().
338 function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
340 if ( empty($in_same_cat) || 'no' == $in_same_cat )
341 $in_same_cat = false;
345 $post = get_previous_post($in_same_cat, $excluded_categories);
350 $string = '<a href="'.get_permalink($post->ID).'">'.$previous;
351 if ( 'yes' == $title )
352 $string .= apply_filters('the_title', $post->post_title, $post);
354 $format = str_replace('%', $string, $format);
358 // Deprecated. Use next_post_link().
359 function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
361 if ( empty($in_same_cat) || 'no' == $in_same_cat )
362 $in_same_cat = false;
366 $post = get_next_post($in_same_cat, $excluded_categories);
371 $string = '<a href="'.get_permalink($post->ID).'">'.$next;
372 if ( 'yes' == $title )
373 $string .= apply_filters('the_title', $post->post_title, $nextpost);
375 $format = str_replace('%', $string, $format);
379 function get_pagenum_link($pagenum = 1) {
382 $qstr = $_SERVER['REQUEST_URI'];
384 $page_querystring = "paged";
385 $page_modstring = "page/";
386 $page_modregex = "page/?";
389 $home_root = parse_url(get_settings('home'));
390 $home_root = $home_root['path'];
391 $home_root = trailingslashit($home_root);
392 $qstr = preg_replace('|^'. $home_root . '|', '', $qstr);
393 $qstr = preg_replace('|^/+|', '', $qstr);
395 $index = $_SERVER['PHP_SELF'];
396 $index = preg_replace('|^'. $home_root . '|', '', $index);
397 $index = preg_replace('|^/+|', '', $index);
399 // if we already have a QUERY style page string
400 if ( stristr( $qstr, $page_querystring ) ) {
401 $replacement = "$page_querystring=$pagenum";
402 $qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr);
403 // if we already have a mod_rewrite style page string
404 } elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ) {
406 $qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr);
408 // if we don't have a page string at all ...
409 // lets see what sort of URL we have...
411 // we need to know the way queries are being written
412 // if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten
413 if ( stristr( $qstr, '?' ) ) {
414 // so append the query string (using &, since we already have ?)
415 $qstr .= '&' . $page_querystring . '=' . $pagenum;
416 // otherwise, it could be rewritten, OR just the default index ...
417 } elseif( '' != get_settings('permalink_structure') && ! is_admin() ) {
419 $index = $wp_rewrite->index;
420 // If it's not a path info permalink structure, trim the index.
421 if ( !$wp_rewrite->using_index_permalinks() ) {
422 $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
424 // If using path info style permalinks, make sure the index is in
426 if ( strpos($qstr, $index) === false )
427 $qstr = '/' . $index . $qstr;
430 $qstr = trailingslashit($qstr) . $page_modstring . $pagenum;
432 $qstr = $index . '?' . $page_querystring . '=' . $pagenum;
436 $qstr = preg_replace('|^/+|', '', $qstr);
438 $qstr = trailingslashit($qstr);
439 $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', trailingslashit( get_settings('home') ) . $qstr );
441 // showing /page/1/ or ?paged=1 is redundant
442 if ( 1 === $pagenum ) {
443 $qstr = str_replace('page/1/', '', $qstr); // for mod_rewrite style
444 $qstr = remove_query_arg('paged', $qstr); // for query style
449 function get_next_posts_page_link($max_page = 0) {
450 global $paged, $pagenow;
452 if ( !is_single() ) {
455 $nextpage = intval($paged) + 1;
456 if ( !$max_page || $max_page >= $nextpage )
457 return get_pagenum_link($nextpage);
461 function next_posts($max_page = 0) {
462 echo clean_url(get_next_posts_page_link($max_page));
465 function next_posts_link($label='Next Page »', $max_page=0) {
466 global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages;
468 if ( isset($max_num_pages) ) {
469 $max_page = $max_num_pages;
471 preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
472 $fromwhere = $matches[1];
473 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
474 $max_page = $max_num_pages = ceil($numposts / $posts_per_page);
479 $nextpage = intval($paged) + 1;
480 if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
482 next_posts($max_page);
483 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
487 function get_previous_posts_page_link() {
488 global $paged, $pagenow;
490 if ( !is_single() ) {
491 $nextpage = intval($paged) - 1;
494 return get_pagenum_link($nextpage);
498 function previous_posts() {
499 echo clean_url(get_previous_posts_page_link());
502 function previous_posts_link($label='« Previous Page') {
504 if ( (!is_single()) && ($paged > 1) ) {
507 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
511 function posts_nav_link($sep=' — ', $prelabel='« Previous Page', $nxtlabel='Next Page »') {
512 global $request, $posts_per_page, $wpdb, $max_num_pages;
513 if ( !is_single() ) {
515 if ( 'posts' == get_query_var('what_to_show') ) {
516 if ( !isset($max_num_pages) ) {
517 preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
518 $fromwhere = $matches[1];
519 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
520 $max_num_pages = ceil($numposts / $posts_per_page);
523 $max_num_pages = 999999;
526 if ( $max_num_pages > 1 ) {
527 previous_posts_link($prelabel);
528 echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
529 next_posts_link($nxtlabel, $max_page);