| [ Index ] |
PHP Cross Reference of WordPress Trunk (Latest) |
[Summary view] [Print] [Text view]
1 <?php 2 3 4 function the_permalink() { 5 echo apply_filters('the_permalink', get_permalink()); 6 } 7 8 9 function permalink_link() { // For backwards compatibility 10 echo apply_filters('the_permalink', get_permalink()); 11 } 12 13 14 function permalink_anchor($mode = 'id') { 15 global $post; 16 switch ( strtolower($mode) ) { 17 case 'title': 18 $title = sanitize_title($post->post_title) . '-' . $id; 19 echo '<a id="'.$title.'"></a>'; 20 break; 21 case 'id': 22 default: 23 echo '<a id="post-' . $post->ID . '"></a>'; 24 break; 25 } 26 } 27 28 29 function get_permalink($id = 0) { 30 $rewritecode = array( 31 '%year%', 32 '%monthnum%', 33 '%day%', 34 '%hour%', 35 '%minute%', 36 '%second%', 37 '%postname%', 38 '%post_id%', 39 '%category%', 40 '%author%', 41 '%pagename%' 42 ); 43 44 $post = &get_post($id); 45 if ( $post->post_type == 'page' ) 46 return get_page_link($post->ID); 47 elseif ($post->post_type == 'attachment') 48 return get_attachment_link($post->ID); 49 50 $permalink = get_settings('permalink_structure'); 51 52 if ( '' != $permalink && 'draft' != $post->post_status ) { 53 $unixtime = strtotime($post->post_date); 54 55 $category = ''; 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; 61 } 62 63 $authordata = get_userdata($post->post_author); 64 $author = $authordata->user_nicename; 65 $date = explode(" ",date('Y m d H i s', $unixtime)); 66 $rewritereplace = 67 array( 68 $date[0], 69 $date[1], 70 $date[2], 71 $date[3], 72 $date[4], 73 $date[5], 74 $post->post_name, 75 $post->ID, 76 $category, 77 $author, 78 $post->post_name, 79 ); 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); 84 } 85 } 86 87 // get permalink from post ID 88 function post_permalink($post_id = 0, $mode = '') { // $mode legacy 89 return get_permalink($post_id); 90 } 91 92 function get_page_link($id = false) { 93 global $post, $wp_rewrite; 94 95 if ( !$id ) 96 $id = $post->ID; 97 98 $pagestruct = $wp_rewrite->get_page_permastruct(); 99 100 if ( '' != $pagestruct && 'draft' != $post->post_status ) { 101 $link = get_page_uri($id); 102 $link = str_replace('%pagename%', $link, $pagestruct); 103 $link = get_settings('home') . "/$link/"; 104 } else { 105 $link = get_settings('home') . "/?page_id=$id"; 106 } 107 108 if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) 109 $link = get_settings('home'); 110 111 return apply_filters('page_link', $link, $id); 112 } 113 114 function get_attachment_link($id = false) { 115 global $post, $wp_rewrite; 116 117 $link = false; 118 119 if (! $id) { 120 $id = $post->ID; 121 } 122 123 $object = get_post($id); 124 if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) ) { 125 $parent = get_post($object->post_parent); 126 $parentlink = get_permalink($object->post_parent); 127 if (! strstr($parentlink, '?') ) 128 $link = trim($parentlink, '/') . '/' . $object->post_name . '/'; 129 } 130 131 if (! $link ) { 132 $link = get_bloginfo('home') . "/?attachment_id=$id"; 133 } 134 135 return apply_filters('attachment_link', $link, $id); 136 } 137 138 function get_year_link($year) { 139 global $wp_rewrite; 140 if ( !$year ) 141 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600)); 142 $yearlink = $wp_rewrite->get_year_permastruct(); 143 if ( !empty($yearlink) ) { 144 $yearlink = str_replace('%year%', $year, $yearlink); 145 return apply_filters('year_link', get_settings('home') . trailingslashit($yearlink), $year); 146 } else { 147 return apply_filters('year_link', get_settings('home') . '/?m=' . $year, $year); 148 } 149 } 150 151 function get_month_link($year, $month) { 152 global $wp_rewrite; 153 if ( !$year ) 154 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600)); 155 if ( !$month ) 156 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600)); 157 $monthlink = $wp_rewrite->get_month_permastruct(); 158 if ( !empty($monthlink) ) { 159 $monthlink = str_replace('%year%', $year, $monthlink); 160 $monthlink = str_replace('%monthnum%', zeroise(intval($month), 2), $monthlink); 161 return apply_filters('month_link', get_settings('home') . trailingslashit($monthlink), $year, $month); 162 } else { 163 return apply_filters('month_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2), $year, $month); 164 } 165 } 166 167 function get_day_link($year, $month, $day) { 168 global $wp_rewrite; 169 if ( !$year ) 170 $year = gmdate('Y', time()+(get_settings('gmt_offset') * 3600)); 171 if ( !$month ) 172 $month = gmdate('m', time()+(get_settings('gmt_offset') * 3600)); 173 if ( !$day ) 174 $day = gmdate('j', time()+(get_settings('gmt_offset') * 3600)); 175 176 $daylink = $wp_rewrite->get_day_permastruct(); 177 if ( !empty($daylink) ) { 178 $daylink = str_replace('%year%', $year, $daylink); 179 $daylink = str_replace('%monthnum%', zeroise(intval($month), 2), $daylink); 180 $daylink = str_replace('%day%', zeroise(intval($day), 2), $daylink); 181 return apply_filters('day_link', get_settings('home') . trailingslashit($daylink), $year, $month, $day); 182 } else { 183 return apply_filters('day_link', get_settings('home') . '/?m=' . $year . zeroise($month, 2) . zeroise($day, 2), $year, $month, $day); 184 } 185 } 186 187 function get_feed_link($feed='rss2') { 188 global $wp_rewrite; 189 $do_perma = 0; 190 $feed_url = get_settings('siteurl'); 191 $comment_feed_url = $feed_url; 192 193 $permalink = $wp_rewrite->get_feed_permastruct(); 194 if ( '' != $permalink ) { 195 if ( false !== strpos($feed, 'comments_') ) { 196 $feed = str_replace('comments_', '', $feed); 197 $permalink = $wp_rewrite->get_comment_feed_permastruct(); 198 } 199 200 if ( 'rss2' == $feed ) 201 $feed = ''; 202 203 $permalink = str_replace('%feed%', $feed, $permalink); 204 $permalink = preg_replace('#/+#', '/', "/$permalink/"); 205 $output = get_settings('home') . $permalink; 206 } else { 207 if ( false !== strpos($feed, 'comments_') ) 208 $feed = str_replace('comments_', 'comments-', $feed); 209 210 $output = get_settings('home') . "/?feed={$feed}"; 211 } 212 213 return apply_filters('feed_link', $output, $feed); 214 } 215 216 function edit_post_link($link = 'Edit This', $before = '', $after = '') { 217 global $post; 218 219 if ( is_attachment() ) 220 return; 221 222 if( $post->post_type == 'page' ) { 223 if ( ! current_user_can('edit_page', $post->ID) ) 224 return; 225 $file = 'page'; 226 } else { 227 if ( ! current_user_can('edit_post', $post->ID) ) 228 return; 229 $file = 'post'; 230 } 231 232 $location = get_settings('siteurl') . "/wp-admin/{$file}.php?action=edit&post=$post->ID"; 233 echo $before . "<a href=\"$location\">$link</a>" . $after; 234 } 235 236 function edit_comment_link($link = 'Edit This', $before = '', $after = '') { 237 global $post, $comment; 238 239 if( $post->post_type == 'page' ){ 240 if ( ! current_user_can('edit_page', $post->ID) ) 241 return; 242 } else { 243 if ( ! current_user_can('edit_post', $post->ID) ) 244 return; 245 } 246 247 $location = get_settings('siteurl') . "/wp-admin/comment.php?action=editcomment&comment=$comment->comment_ID"; 248 echo $before . "<a href='$location'>$link</a>" . $after; 249 } 250 251 // Navigation links 252 253 function get_previous_post($in_same_cat = false, $excluded_categories = '') { 254 global $post, $wpdb; 255 256 if( !is_single() || is_attachment() ) 257 return null; 258 259 $current_post_date = $post->post_date; 260 261 $join = ''; 262 if ( $in_same_cat ) { 263 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id "; 264 $cat_array = get_the_category($post->ID); 265 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID); 266 for ( $i = 1; $i < (count($cat_array)); $i++ ) { 267 $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID); 268 } 269 $join .= ')'; 270 } 271 272 $sql_exclude_cats = ''; 273 if ( !empty($excluded_categories) ) { 274 $blah = explode(' and ', $excluded_categories); 275 foreach ( $blah as $category ) { 276 $category = intval($category); 277 $sql_cat_ids = " OR pc.category_ID = '$category'"; 278 } 279 $posts_in_ex_cats = $wpdb->get_col("SELECT p.ID FROM $wpdb->posts p LEFT JOIN $wpdb->post2cat pc ON pc.post_id=p.ID WHERE 1 = 0 $sql_cat_ids GROUP BY p.ID"); 280 $posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')'; 281 } 282 283 return @$wpdb->get_row("SELECT ID, post_title FROM $wpdb->posts $join WHERE post_date < '$current_post_date' AND post_type = 'post' AND post_status = 'publish' $posts_in_ex_cats_sql ORDER BY post_date DESC LIMIT 1"); 284 } 285 286 function get_next_post($in_same_cat = false, $excluded_categories = '') { 287 global $post, $wpdb; 288 289 if( !is_single() || is_attachment() ) 290 return null; 291 292 $current_post_date = $post->post_date; 293 294 $join = ''; 295 if ( $in_same_cat ) { 296 $join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts.ID= $wpdb->post2cat.post_id "; 297 $cat_array = get_the_category($post->ID); 298 $join .= ' AND (category_id = ' . intval($cat_array[0]->cat_ID); 299 for ( $i = 1; $i < (count($cat_array)); $i++ ) { 300 $join .= ' OR category_id = ' . intval($cat_array[$i]->cat_ID); 301 } 302 $join .= ')'; 303 } 304 305 $sql_exclude_cats = ''; 306 if ( !empty($excluded_categories) ) { 307 $blah = explode(' and ', $excluded_categories); 308 foreach ( $blah as $category ) { 309 $category = intval($category); 310 $sql_cat_ids = " OR pc.category_ID = '$category'"; 311 } 312 $posts_in_ex_cats = $wpdb->get_col("SELECT p.ID from $wpdb->posts p LEFT JOIN $wpdb->post2cat pc ON pc.post_id = p.ID WHERE 1 = 0 $sql_cat_ids GROUP BY p.ID"); 313 $posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')'; 314 } 315 316 return @$wpdb->get_row("SELECT ID,post_title FROM $wpdb->posts $join WHERE post_date > '$current_post_date' AND post_type = 'post' AND post_status = 'publish' $posts_in_ex_cats_sql AND ID != $post->ID ORDER BY post_date ASC LIMIT 1"); 317 } 318 319 320 function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') { 321 322 if ( is_attachment() ) 323 $post = & get_post($GLOBALS['post']->post_parent); 324 else 325 $post = get_previous_post($in_same_cat, $excluded_categories); 326 327 if ( !$post ) 328 return; 329 330 $title = apply_filters('the_title', $post->post_title, $post); 331 $string = '<a href="'.get_permalink($post->ID).'">'; 332 $link = str_replace('%title', $title, $link); 333 $link = $pre . $string . $link . '</a>'; 334 335 $format = str_replace('%link', $link, $format); 336 337 echo $format; 338 } 339 340 function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') { 341 $post = get_next_post($in_same_cat, $excluded_categories); 342 343 if ( !$post ) 344 return; 345 346 $title = apply_filters('the_title', $post->post_title, $post); 347 $string = '<a href="'.get_permalink($post->ID).'">'; 348 $link = str_replace('%title', $title, $link); 349 $link = $string . $link . '</a>'; 350 $format = str_replace('%link', $link, $format); 351 352 echo $format; 353 } 354 355 function get_pagenum_link($pagenum = 1) { 356 global $wp_rewrite; 357 358 $qstr = wp_specialchars($_SERVER['REQUEST_URI']); 359 360 $page_querystring = "paged"; 361 $page_modstring = "page/"; 362 $page_modregex = "page/?"; 363 $permalink = 0; 364 365 $home_root = parse_url(get_settings('home')); 366 $home_root = $home_root['path']; 367 $home_root = trailingslashit($home_root); 368 $qstr = preg_replace('|^'. $home_root . '|', '', $qstr); 369 $qstr = preg_replace('|^/+|', '', $qstr); 370 371 $index = $_SERVER['PHP_SELF']; 372 $index = preg_replace('|^'. $home_root . '|', '', $index); 373 $index = preg_replace('|^/+|', '', $index); 374 375 // if we already have a QUERY style page string 376 if ( stristr( $qstr, $page_querystring ) ) { 377 $replacement = "$page_querystring=$pagenum"; 378 $qstr = preg_replace("/".$page_querystring."[^\d]+\d+/", $replacement, $qstr); 379 // if we already have a mod_rewrite style page string 380 } elseif ( preg_match( '|'.$page_modregex.'\d+|', $qstr ) ) { 381 $permalink = 1; 382 $qstr = preg_replace('|'.$page_modregex.'\d+|',"$page_modstring$pagenum",$qstr); 383 384 // if we don't have a page string at all ... 385 // lets see what sort of URL we have... 386 } else { 387 // we need to know the way queries are being written 388 // if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten 389 if ( stristr( $qstr, '?' ) ) { 390 // so append the query string (using &, since we already have ?) 391 $qstr .= '&' . $page_querystring . '=' . $pagenum; 392 // otherwise, it could be rewritten, OR just the default index ... 393 } elseif( '' != get_settings('permalink_structure') && ! is_admin() ) { 394 $permalink = 1; 395 $index = $wp_rewrite->index; 396 // If it's not a path info permalink structure, trim the index. 397 if ( !$wp_rewrite->using_index_permalinks() ) { 398 $qstr = preg_replace("#/*" . $index . "/*#", '/', $qstr); 399 } else { 400 // If using path info style permalinks, make sure the index is in 401 // the URI. 402 if ( strpos($qstr, $index) === false ) 403 $qstr = '/' . $index . $qstr; 404 } 405 406 $qstr = trailingslashit($qstr) . $page_modstring . $pagenum; 407 } else { 408 $qstr = $index . '?' . $page_querystring . '=' . $pagenum; 409 } 410 } 411 412 $qstr = preg_replace('|^/+|', '', $qstr); 413 if ( $permalink ) 414 $qstr = trailingslashit($qstr); 415 $qstr = preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', trailingslashit( get_settings('home') ) . $qstr ); 416 417 // showing /page/1/ or ?paged=1 is redundant 418 if ( 1 === $pagenum ) { 419 $qstr = str_replace('page/1/', '', $qstr); // for mod_rewrite style 420 $qstr = remove_query_arg('paged', $qstr); // for query style 421 } 422 return $qstr; 423 } 424 425 function next_posts($max_page = 0) { // original by cfactor at cooltux.org 426 global $paged, $pagenow; 427 428 if ( !is_single() ) { 429 if ( !$paged ) 430 $paged = 1; 431 $nextpage = intval($paged) + 1; 432 if ( !$max_page || $max_page >= $nextpage ) 433 echo get_pagenum_link($nextpage); 434 } 435 } 436 437 function next_posts_link($label='Next Page »', $max_page=0) { 438 global $paged, $wpdb; 439 if ( !$max_page ) { 440 $max_page = _max_num_pages(); 441 } 442 if ( !$paged ) 443 $paged = 1; 444 $nextpage = intval($paged) + 1; 445 if ( (! is_single()) && (empty($paged) || $nextpage <= $max_page) ) { 446 echo '<a href="'; 447 next_posts($max_page); 448 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>'; 449 } 450 } 451 452 453 function previous_posts() { // original by cfactor at cooltux.org 454 global $paged, $pagenow; 455 456 if ( !is_single() ) { 457 $nextpage = intval($paged) - 1; 458 if ( $nextpage < 1 ) 459 $nextpage = 1; 460 echo get_pagenum_link($nextpage); 461 } 462 } 463 464 465 function previous_posts_link($label='« Previous Page') { 466 global $paged; 467 if ( (!is_single()) && ($paged > 1) ) { 468 echo '<a href="'; 469 previous_posts(); 470 echo '">'. preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $label) .'</a>'; 471 } 472 } 473 474 function _max_num_pages() { 475 static $max_num_pages; 476 global $wpdb, $wp_query; 477 478 if (isset($max_num_pages)) return $max_num_pages; 479 480 if ( 'posts' == get_query_var('what_to_show') ) { 481 preg_match('#FROM\s(.*)\sORDER BY#siU', $wp_query->request, $matches); 482 $fromwhere = $matches[1]; 483 $numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $fromwhere"); 484 $max_num_pages = ceil($numposts / get_option('posts_per_page')); 485 } else { 486 preg_match('#FROM\s(.*)\sORDER BY#siU', $wp_query->request, $matches); 487 $fromwhere = preg_replace('/( AND )?post_date >= (\'|\")(.*?)(\'|\")( AND post_date <= (\'\")(.*?)(\'\"))?/siU', '', $matches[1]); 488 $num_days = $wpdb->query("SELECT DISTINCT post_date FROM $fromwhere GROUP BY year(post_date), month(post_date), dayofmonth(post_date)"); 489 $max_num_pages = ceil($num_days / get_option('posts_per_page')); 490 } 491 492 return $max_num_pages; 493 } 494 495 function posts_nav_link($sep=' — ', $prelabel='« Previous Page', $nxtlabel='Next Page »') { 496 if ( !is_single() ) { 497 $max_num_pages = _max_num_pages(); 498 $paged = get_query_var('paged'); 499 500 //only have sep if there's both prev and next results 501 if ($paged < 2 || $paged >= $max_num_pages) { 502 $sep = ''; 503 } 504 505 if ( $max_num_pages > 1 ) { 506 previous_posts_link($prelabel); 507 echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep); 508 next_posts_link($nxtlabel); 509 } 510 } 511 } 512 513 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Jun 11 00:10:35 2006 | Courtesy of Taragana |