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;
93 $pagestruct = $wp_rewrite->get_page_permastruct();
95 if ( '' != $pagestruct ) {
96 $link = get_page_uri($id);
97 $link = str_replace('%pagename%', $link, $pagestruct);
98 $link = get_settings('home') . "/$link/";
100 $link = get_settings('home') . "/?page_id=$id";
103 return apply_filters('page_link', $link, $id);
106 function get_attachment_link($id = false) {
107 global $post, $wp_rewrite;
115 $object = get_post($id);
116 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) ) {
117 $parent = get_post($object->post_parent);
118 $parentlink = get_permalink($object->post_parent);
119 if (! strstr($parentlink, '?') )
120 $link = trim($parentlink, '/') . '/' . $object->post_name . '/';
124 $link = get_bloginfo('home') . "/?attachment_id=$id";
127 return apply_filters('attachment_link', $link, $id);
130 function get_year_link($year) {
133 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
134 $yearlink = $wp_rewrite->get_year_permastruct();
135 if ( !empty($yearlink) ) {
136 $yearlink = str_replace('%year%', $year, $yearlink);
137 return apply_filters('year_link', get_settings('home') . trailingslashit($yearlink), $year);
139 return apply_filters('year_link', get_settings('home') . '/?m=' . $year, $year);
143 function get_month_link($year, $month) {
146 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
148 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
149 $monthlink = $wp_rewrite->get_month_permastruct();
150 if ( !empty($monthlink) ) {
151 $monthlink = str_replace('%year%', $year, $monthlink);
152 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
153 return apply_filters('month_link', get_settings('home') . trailingslashit($monthlink), $year, $month);
155 return apply_filters('month_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
159 function get_day_link($year, $month, $day) {
162 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
164 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
166 $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
168 $daylink = $wp_rewrite->get_day_permastruct();
169 if ( !empty($daylink) ) {
170 $daylink = str_replace('%year%', $year, $daylink);
171 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
172 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
173 return apply_filters('day_link', get_settings('home') . trailingslashit($daylink), $year, $month, $day);
175 return apply_filters('day_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
179 function get_feed_link($feed='rss2') {
182 $feed_url = get_settings('siteurl');
183 $comment_feed_url = $feed_url;
185 $permalink = $wp_rewrite->get_feed_permastruct();
186 if ( '' != $permalink ) {
187 if ( false !== strpos($feed, 'comments_') ) {
188 $feed = str_replace('comments_', '', $feed);
189 $permalink = $wp_rewrite->get_comment_feed_permastruct();
192 if ( 'rss2' == $feed )
195 $permalink = str_replace('%feed%', $feed, $permalink);
196 $permalink = preg_replace('#/+#', '/', "/$permalink/");
197 $output = get_settings('home') . $permalink;
199 if ( false !== strpos($feed, 'comments_') )
200 $feed = str_replace('comments_', 'comments-', $feed);
202 $output = get_settings('home') . "/?feed={$feed}";
205 return apply_filters('feed_link', $output, $feed);
208 function edit_post_link($link = 'Edit This', $before = '', $after = '') {
211 if ( ! current_user_can('edit_post', $post->ID) )
214 if ( is_attachment() )
219 $location = get_settings('siteurl') . "/wp-admin/{$file}.php?action=edit&post=$post->ID";
220 echo $before . "<a href=\"$location\">$link</a>" . $after;
223 function edit_comment_link($link = 'Edit This', $before = '', $after = '') {
224 global $post, $comment;
226 if ( ! current_user_can('edit_post', $post->ID) )
229 $location = get_settings('siteurl') . "/wp-admin/post.php?action=editcomment&comment=$comment->comment_ID";
230 echo $before . "<a href='$location'>$link</a>" . $after;
235 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
238 if( !is_single() || is_attachment() )
241 $current_post_date = $post->post_date;
244 if ( $in_same_cat ) {
245 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
246 $cat_array = get_the_category($post->ID);
247 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
248 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
249 $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
254 $sql_exclude_cats = '';
255 if ( !empty($excluded_categories) ) {
256 $blah = explode('and', $excluded_categories);
257 foreach ( $blah as $category ) {
258 $category = intval($category);
259 $sql_exclude_cats .= " AND post_category != $category";
263 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");
266 function get_next_post($in_same_cat = false, $excluded_categories = '') {
269 if( !is_single() || is_attachment() )
272 $current_post_date = $post->post_date;
275 if ( $in_same_cat ) {
276 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
277 $cat_array = get_the_category($post->ID);
278 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
279 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
280 $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
285 $sql_exclude_cats = '';
286 if ( !empty($excluded_categories) ) {
287 $blah = explode('and', $excluded_categories);
288 foreach ( $blah as $category ) {
289 $category = intval($category);
290 $sql_exclude_cats .= " AND post_category != $category";
294 $now = current_time('mysql');
296 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");
300 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
302 if ( is_attachment() )
303 $post = & get_post($GLOBALS['post']->post_parent);
305 $post = get_previous_post($in_same_cat, $excluded_categories);
310 $title = apply_filters('the_title', $post->post_title, $post);
311 $string = '<a href="'.get_permalink($post->ID).'">';
312 $link = str_replace('%title', $title, $link);
313 $link = $pre . $string . $link . '</a>';
315 $format = str_replace('%link', $link, $format);
320 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
321 $post = get_next_post($in_same_cat, $excluded_categories);
326 $title = apply_filters('the_title', $post->post_title, $post);
327 $string = '<a href="'.get_permalink($post->ID).'">';
328 $link = str_replace('%title', $title, $link);
329 $link = $string . $link . '</a>';
330 $format = str_replace('%link', $link, $format);
336 // Deprecated. Use previous_post_link().
337 function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
339 if ( empty($in_same_cat) || 'no' == $in_same_cat )
340 $in_same_cat = false;
344 $post = get_previous_post($in_same_cat, $excluded_categories);
349 $string = '<a href="'.get_permalink($post->ID).'">'.$previous;
350 if ( 'yes' == $title )
351 $string .= apply_filters('the_title', $post->post_title, $post);
353 $format = str_replace('%', $string, $format);
357 // Deprecated. Use next_post_link().
358 function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
360 if ( empty($in_same_cat) || 'no' == $in_same_cat )
361 $in_same_cat = false;
365 $post = get_next_post($in_same_cat, $excluded_categories);
370 $string = '<a href="'.get_permalink($post->ID).'">'.$next;
371 if ( 'yes' == $title )
372 $string .= apply_filters('the_title', $post->post_title, $nextpost);
374 $format = str_replace('%', $string, $format);
378 function get_pagenum_link($pagenum = 1) {
381 $qstr = wp_specialchars($_SERVER['REQUEST_URI']);
383 $page_querystring = "paged";
384 $page_modstring = "page/";
385 $page_modregex = "page/?";
388 $home_root = parse_url(get_settings('home'));
389 $home_root = $home_root['path'];
390 $home_root = trailingslashit($home_root);
391 $qstr = preg_replace('|^'. $home_root . '|', '', $qstr);
392 $qstr = preg_replace('|^/+|', '', $qstr);
394 $index = $_SERVER['PHP_SELF'];
395 $index = preg_replace('|^'. $home_root . '|', '', $index);
396 $index = preg_replace('|^/+|', '', $index);
398 // if we already have a QUERY style page string
399 if ( stristr( $qstr, $page_querystring ) ) {
400 $replacement = "$page_querystring=$pagenum";
401 $qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr);
402 // if we already have a mod_rewrite style page string
403 } elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ) {
405 $qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr);
407 // if we don't have a page string at all ...
408 // lets see what sort of URL we have...
410 // we need to know the way queries are being written
411 // if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten
412 if ( stristr( $qstr, '?' ) ) {
413 // so append the query string (using &, since we already have ?)
414 $qstr .= '&' . $page_querystring . '=' . $pagenum;
415 // otherwise, it could be rewritten, OR just the default index ...
416 } elseif( '' != get_settings('permalink_structure') && ! is_admin() ) {
418 $index = $wp_rewrite->index;
419 // If it's not a path info permalink structure, trim the index.
420 if ( !$wp_rewrite->using_index_permalinks() ) {
421 $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
423 // If using path info style permalinks, make sure the index is in
425 if ( strpos($qstr, $index) === false )
426 $qstr = '/' . $index . $qstr;
429 $qstr = trailingslashit($qstr) . $page_modstring . $pagenum;
431 $qstr = $index . '?' . $page_querystring . '=' . $pagenum;
435 $qstr = preg_replace('|^/+|', '', $qstr);
437 $qstr = trailingslashit($qstr);
438 $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', trailingslashit( get_settings('home') ) . $qstr );
440 // showing /page/1/ or ?paged=1 is redundant
441 if ( 1 === $pagenum ) {
442 $qstr = str_replace('page/1/', '', $qstr); // for mod_rewrite style
443 $qstr = remove_query_arg('paged', $qstr); // for query style
448 function next_posts($max_page = 0) { // original by cfactor at cooltux.org
449 global $paged, $pagenow;
451 if ( !is_single() ) {
454 $nextpage = intval($paged) + 1;
455 if ( !$max_page || $max_page >= $nextpage )
456 echo get_pagenum_link($nextpage);
460 function next_posts_link($label='Next Page »', $max_page=0) {
461 global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages;
463 if ( isset($max_num_pages) ) {
464 $max_page = $max_num_pages;
466 preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
467 $fromwhere = $matches[1];
468 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
469 $max_page = $max_num_pages = ceil($numposts / $posts_per_page);
474 $nextpage = intval($paged) + 1;
475 if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
477 next_posts($max_page);
478 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
483 function previous_posts() { // original by cfactor at cooltux.org
484 global $paged, $pagenow;
486 if ( !is_single() ) {
487 $nextpage = intval($paged) - 1;
490 echo get_pagenum_link($nextpage);
495 function previous_posts_link($label='« Previous Page') {
497 if ( (!is_single()) && ($paged > 1) ) {
500 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
504 function posts_nav_link($sep=' — ', $prelabel='« Previous Page', $nxtlabel='Next Page »') {
505 global $request, $posts_per_page, $wpdb, $max_num_pages;
506 if ( !is_single() ) {
508 if ( 'posts' == get_query_var('what_to_show') ) {
509 if ( !isset($max_num_pages) ) {
510 preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
511 $fromwhere = $matches[1];
512 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
513 $max_num_pages = ceil($numposts / $posts_per_page);
516 $max_num_pages = 999999;
519 if ( $max_num_pages > 1 ) {
520 previous_posts_link($prelabel);
521 echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
522 next_posts_link($nxtlabel, $max_page);