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;
79 return apply_filters('post_link', get_settings('home') . str_replace($rewritecode, $rewritereplace, $permalink), $post);
80 } else { // if they're not using the fancy permalink option
81 $permalink = get_settings('home') . '/?p=' . $post->ID;
82 return apply_filters('post_link', $permalink, $post);
86 function get_page_link($id = false) {
87 global $post, $wp_rewrite;
92 $pagestruct = $wp_rewrite->get_page_permastruct();
94 if ( '' != $pagestruct ) {
95 $link = get_page_uri($id);
96 $link = str_replace('%pagename%', $link, $pagestruct);
97 $link = get_settings('home') . "/$link/";
99 $link = get_settings('home') . "/?page_id=$id";
102 return apply_filters('page_link', $link, $id);
105 function get_attachment_link($id = false) {
106 global $post, $wp_rewrite;
114 $object = get_post($id);
115 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) ) {
116 $parent = get_post($object->post_parent);
117 $parentlink = get_permalink($object->post_parent);
118 if (! strstr($parentlink, '?') )
119 $link = trim($parentlink, '/') . '/' . $object->post_name . '/';
123 $link = get_bloginfo('home') . "/?attachment_id=$id";
126 return apply_filters('attachment_link', $link, $id);
129 function get_year_link($year) {
132 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
133 $yearlink = $wp_rewrite->get_year_permastruct();
134 if ( !empty($yearlink) ) {
135 $yearlink = str_replace('%year%', $year, $yearlink);
136 return apply_filters('year_link', get_settings('home') . trailingslashit($yearlink), $year);
138 return apply_filters('year_link', get_settings('home') . '/?m=' . $year, $year);
142 function get_month_link($year, $month) {
145 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
147 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
148 $monthlink = $wp_rewrite->get_month_permastruct();
149 if ( !empty($monthlink) ) {
150 $monthlink = str_replace('%year%', $year, $monthlink);
151 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink);
152 return apply_filters('month_link', get_settings('home') . trailingslashit($monthlink), $year, $month);
154 return apply_filters('month_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2), $year, $month);
158 function get_day_link($year, $month, $day) {
161 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600));
163 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600));
165 $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600));
167 $daylink = $wp_rewrite->get_day_permastruct();
168 if ( !empty($daylink) ) {
169 $daylink = str_replace('%year%', $year, $daylink);
170 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink);
171 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink);
172 return apply_filters('day_link', get_settings('home') . trailingslashit($daylink), $year, $month, $day);
174 return apply_filters('day_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day);
178 function get_feed_link($feed='rss2') {
181 $feed_url = get_settings('siteurl');
182 $comment_feed_url = $feed_url;
184 $permalink = $wp_rewrite->get_feed_permastruct();
185 if ( '' != $permalink ) {
186 if ( false !== strpos($feed, 'comments_') ) {
187 $feed = str_replace('comments_', '', $feed);
188 $permalink = $wp_rewrite->get_comment_feed_permastruct();
191 if ( 'rss2' == $feed )
194 $permalink = str_replace('%feed%', $feed, $permalink);
195 $permalink = preg_replace('#/+#', '/', "/$permalink/");
196 $output = get_settings('home') . $permalink;
198 if ( false !== strpos($feed, 'comments_') )
199 $feed = str_replace('comments_', 'comments-', $feed);
201 $output = get_settings('home') . "/?feed={$feed}";
204 return apply_filters('feed_link', $output, $feed);
207 function edit_post_link($link = 'Edit This', $before = '', $after = '') {
210 if ( ! current_user_can('edit_post', $post->ID) )
213 if ( is_attachment() )
218 $location = get_settings('siteurl') . "/wp-admin/{$file}.php?action=edit&post=$post->ID";
219 echo $before . "<a href=\"$location\">$link</a>" . $after;
222 function edit_comment_link($link = 'Edit This', $before = '', $after = '') {
223 global $post, $comment;
225 if ( ! current_user_can('edit_post', $post->ID) )
228 $location = get_settings('siteurl') . "/wp-admin/post.php?action=editcomment&comment=$comment->comment_ID";
229 echo $before . "<a href='$location'>$link</a>" . $after;
234 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
237 if( !is_single() || is_attachment() )
240 $current_post_date = $post->post_date;
243 if ( $in_same_cat ) {
244 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
245 $cat_array = get_the_category($post->ID);
246 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
247 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
248 $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
253 $sql_exclude_cats = '';
254 if ( !empty($excluded_categories) ) {
255 $blah = explode('and', $excluded_categories);
256 foreach ( $blah as $category ) {
257 $category = intval($category);
258 $sql_exclude_cats .= " AND post_category != $category";
262 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");
265 function get_next_post($in_same_cat = false, $excluded_categories = '') {
268 if( !is_single() || is_attachment() )
271 $current_post_date = $post->post_date;
274 if ( $in_same_cat ) {
275 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id ";
276 $cat_array = get_the_category($post->ID);
277 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID);
278 for ( $i = 1; $i < (count($cat_array)); $i++ ) {
279 $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID);
284 $sql_exclude_cats = '';
285 if ( !empty($excluded_categories) ) {
286 $blah = explode('and', $excluded_categories);
287 foreach ( $blah as $category ) {
288 $category = intval($category);
289 $sql_exclude_cats .= " AND post_category != $category";
293 $now = current_time('mysql');
295 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");
299 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
301 if ( is_attachment() )
302 $post = & get_post($GLOBALS['post']->post_parent);
304 $post = get_previous_post($in_same_cat, $excluded_categories);
309 $title = apply_filters('the_title', $post->post_title, $post);
310 $string = '<a href="'.get_permalink($post->ID).'">';
311 $link = str_replace('%title', $title, $link);
312 $link = $pre . $string . $link . '</a>';
314 $format = str_replace('%link', $link, $format);
319 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
320 $post = get_next_post($in_same_cat, $excluded_categories);
325 $title = apply_filters('the_title', $post->post_title, $post);
326 $string = '<a href="'.get_permalink($post->ID).'">';
327 $link = str_replace('%title', $title, $link);
328 $link = $string . $link . '</a>';
329 $format = str_replace('%link', $link, $format);
335 // Deprecated. Use previous_post_link().
336 function previous_post($format='%', $previous='previous post: ', $title='yes', $in_same_cat='no', $limitprev=1, $excluded_categories='') {
338 if ( empty($in_same_cat) || 'no' == $in_same_cat )
339 $in_same_cat = false;
343 $post = get_previous_post($in_same_cat, $excluded_categories);
348 $string = '<a href="'.get_permalink($post->ID).'">'.$previous;
349 if ( 'yes' == $title )
350 $string .= apply_filters('the_title', $post->post_title, $post);
352 $format = str_replace('%', $string, $format);
356 // Deprecated. Use next_post_link().
357 function next_post($format='%', $next='next post: ', $title='yes', $in_same_cat='no', $limitnext=1, $excluded_categories='') {
359 if ( empty($in_same_cat) || 'no' == $in_same_cat )
360 $in_same_cat = false;
364 $post = get_next_post($in_same_cat, $excluded_categories);
369 $string = '<a href="'.get_permalink($post->ID).'">'.$next;
370 if ( 'yes' == $title )
371 $string .= apply_filters('the_title', $post->post_title, $nextpost);
373 $format = str_replace('%', $string, $format);
377 function get_pagenum_link($pagenum = 1) {
380 $qstr = wp_specialchars($_SERVER['REQUEST_URI']);
382 $page_querystring = "paged";
383 $page_modstring = "page/";
384 $page_modregex = "page/?";
387 $home_root = parse_url(get_settings('home'));
388 $home_root = $home_root['path'];
389 $home_root = trailingslashit($home_root);
390 $qstr = preg_replace('|^'. $home_root . '|', '', $qstr);
391 $qstr = preg_replace('|^/+|', '', $qstr);
393 $index = $_SERVER['PHP_SELF'];
394 $index = preg_replace('|^'. $home_root . '|', '', $index);
395 $index = preg_replace('|^/+|', '', $index);
397 // if we already have a QUERY style page string
398 if ( stristr( $qstr, $page_querystring ) ) {
399 $replacement = "$page_querystring=$pagenum";
400 $qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr);
401 // if we already have a mod_rewrite style page string
402 } elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ) {
404 $qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr);
406 // if we don't have a page string at all ...
407 // lets see what sort of URL we have...
409 // we need to know the way queries are being written
410 // if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten
411 if ( stristr( $qstr, '?' ) ) {
412 // so append the query string (using &, since we already have ?)
413 $qstr .= '&' . $page_querystring . '=' . $pagenum;
414 // otherwise, it could be rewritten, OR just the default index ...
415 } elseif( '' != get_settings('permalink_structure') && ! is_admin() ) {
417 $index = $wp_rewrite->index;
418 // If it's not a path info permalink structure, trim the index.
419 if ( !$wp_rewrite->using_index_permalinks() ) {
420 $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr);
422 // If using path info style permalinks, make sure the index is in
424 if ( strpos($qstr, $index) === false )
425 $qstr = '/' . $index . $qstr;
428 $qstr = trailingslashit($qstr) . $page_modstring . $pagenum;
430 $qstr = $index . '?' . $page_querystring . '=' . $pagenum;
434 $qstr = preg_replace('|^/+|', '', $qstr);
436 $qstr = trailingslashit($qstr);
437 $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', trailingslashit( get_settings('home') ) . $qstr );
439 // showing /page/1/ or ?paged=1 is redundant
440 if ( 1 === $pagenum ) {
441 $qstr = str_replace('page/1/', '', $qstr); // for mod_rewrite style
442 $qstr = remove_query_arg('paged', $qstr); // for query style
447 function next_posts($max_page = 0) { // original by cfactor at cooltux.org
448 global $paged, $pagenow;
450 if ( !is_single() ) {
453 $nextpage = intval($paged) + 1;
454 if ( !$max_page || $max_page >= $nextpage )
455 echo get_pagenum_link($nextpage);
459 function next_posts_link($label='Next Page »', $max_page=0) {
460 global $paged, $result, $request, $posts_per_page, $wpdb, $max_num_pages;
462 if ( isset($max_num_pages) ) {
463 $max_page = $max_num_pages;
465 preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
466 $fromwhere = $matches[1];
467 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
468 $max_page = $max_num_pages = ceil($numposts / $posts_per_page);
473 $nextpage = intval($paged) + 1;
474 if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) {
476 next_posts($max_page);
477 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
482 function previous_posts() { // original by cfactor at cooltux.org
483 global $paged, $pagenow;
485 if ( !is_single() ) {
486 $nextpage = intval($paged) - 1;
489 echo get_pagenum_link($nextpage);
494 function previous_posts_link($label='« Previous Page') {
496 if ( (!is_single()) && ($paged > 1) ) {
499 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>';
503 function posts_nav_link($sep=' — ', $prelabel='« Previous Page', $nxtlabel='Next Page »') {
504 global $request, $posts_per_page, $wpdb, $max_num_pages;
505 if ( !is_single() ) {
507 if ( 'posts' == get_query_var('what_to_show') ) {
508 if ( !isset($max_num_pages) ) {
509 preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
510 $fromwhere = $matches[1];
511 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere");
512 $max_num_pages = ceil($numposts / $posts_per_page);
515 $max_num_pages = 999999;
518 if ( $max_num_pages > 1 ) {
519 previous_posts_link($prelabel);
520 echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
521 next_posts_link($nxtlabel, $max_page);