2004-01-27 09:58:01 +00:00
< ? php
2005-10-17 23:41:28 +00:00
2004-04-21 04:12:55 +00:00
function the_permalink () {
2005-01-14 23:43:20 +00:00
echo apply_filters ( 'the_permalink' , get_permalink ());
2004-04-21 04:12:55 +00:00
}
2005-10-17 23:41:28 +00:00
2004-05-18 08:36:53 +00:00
function permalink_link () { // For backwards compatibility
2005-01-14 23:43:20 +00:00
echo apply_filters ( 'the_permalink' , get_permalink ());
2004-05-18 08:36:53 +00:00
}
2005-10-17 23:41:28 +00:00
2004-01-27 09:58:01 +00:00
function permalink_anchor ( $mode = 'id' ) {
2005-10-17 23:41:28 +00:00
global $post ;
switch ( strtolower ( $mode ) ) {
case 'title' :
$title = sanitize_title ( $post -> post_title ) . '-' . $id ;
echo '<a id="' . $title . '"></a>' ;
break ;
case 'id' :
default :
echo '<a id="post-' . $post -> ID . '"></a>' ;
break ;
}
2004-01-27 09:58:01 +00:00
}
2005-10-17 23:41:28 +00:00
2005-03-27 20:45:01 +00:00
function get_permalink ( $id = 0 ) {
2004-09-05 02:41:01 +00:00
$rewritecode = array (
'%year%' ,
'%monthnum%' ,
'%day%' ,
'%hour%' ,
'%minute%' ,
'%second%' ,
'%postname%' ,
'%post_id%' ,
'%category%' ,
'%author%' ,
'%pagename%'
);
2004-08-10 05:35:59 +00:00
2005-10-17 23:41:28 +00:00
$post = & get_post ( $id );
2006-02-09 10:03:48 +00:00
if ( $post -> post_type == 'page' )
2005-03-27 20:45:01 +00:00
return get_page_link ( $post -> ID );
2006-02-09 10:03:48 +00:00
elseif ( $post -> post_type == 'attachment' )
2005-11-15 22:55:24 +00:00
return get_attachment_link ( $post -> ID );
2004-11-12 23:08:51 +00:00
2006-08-30 21:46:31 +00:00
$permalink = get_option ( 'permalink_structure' );
2004-08-10 05:35:59 +00:00
2005-10-17 23:41:28 +00:00
if ( '' != $permalink && 'draft' != $post -> post_status ) {
2005-03-27 20:45:01 +00:00
$unixtime = strtotime ( $post -> post_date );
2004-06-04 02:36:46 +00:00
2005-02-16 15:49:02 +00:00
$category = '' ;
2005-10-17 23:41:28 +00:00
if ( strstr ( $permalink , '%category%' ) ) {
2005-03-27 20:45:01 +00:00
$cats = get_the_category ( $post -> ID );
2005-02-16 15:49:02 +00:00
$category = $cats [ 0 ] -> category_nicename ;
2005-10-17 23:41:28 +00:00
if ( $parent = $cats [ 0 ] -> category_parent )
$category = get_category_parents ( $parent , FALSE , '/' , TRUE ) . $category ;
2005-02-16 15:49:02 +00:00
}
2005-01-25 05:01:54 +00:00
2005-03-27 20:45:01 +00:00
$authordata = get_userdata ( $post -> post_author );
2004-09-05 02:41:01 +00:00
$author = $authordata -> user_nicename ;
2006-02-17 01:04:20 +00:00
$date = explode ( " " , date ( 'Y m d H i s' , $unixtime ));
2004-09-05 02:41:01 +00:00
$rewritereplace =
array (
2006-02-17 01:04:20 +00:00
$date [ 0 ],
$date [ 1 ],
$date [ 2 ],
$date [ 3 ],
$date [ 4 ],
$date [ 5 ],
2005-03-27 20:45:01 +00:00
$post -> post_name ,
$post -> ID ,
2004-09-05 02:41:01 +00:00
$category ,
$author ,
2005-03-27 20:45:01 +00:00
$post -> post_name ,
2004-09-05 02:41:01 +00:00
);
2006-08-30 21:46:31 +00:00
return apply_filters ( 'post_link' , get_option ( 'home' ) . str_replace ( $rewritecode , $rewritereplace , $permalink ), $post );
2004-09-05 02:41:01 +00:00
} else { // if they're not using the fancy permalink option
2006-08-30 21:46:31 +00:00
$permalink = get_option ( 'home' ) . '/?p=' . $post -> ID ;
2005-03-27 20:45:01 +00:00
return apply_filters ( 'post_link' , $permalink , $post );
2004-09-05 02:41:01 +00:00
}
2004-01-27 09:58:01 +00:00
}
2006-06-07 23:17:59 +00:00
// get permalink from post ID
function post_permalink ( $post_id = 0 , $mode = '' ) { // $mode legacy
return get_permalink ( $post_id );
}
2004-10-06 02:18:37 +00:00
function get_page_link ( $id = false ) {
2004-12-03 02:38:11 +00:00
global $post , $wp_rewrite ;
2004-10-06 02:18:37 +00:00
2005-10-17 23:41:28 +00:00
if ( ! $id )
2004-10-06 02:18:37 +00:00
$id = $post -> ID ;
2005-01-26 22:46:40 +00:00
$pagestruct = $wp_rewrite -> get_page_permastruct ();
2004-10-06 02:18:37 +00:00
2006-04-15 00:59:40 +00:00
if ( '' != $pagestruct && 'draft' != $post -> post_status ) {
2004-10-06 02:18:37 +00:00
$link = get_page_uri ( $id );
2005-01-26 22:46:40 +00:00
$link = str_replace ( '%pagename%' , $link , $pagestruct );
2006-08-30 21:46:31 +00:00
$link = get_option ( 'home' ) . " / $link / " ;
2004-10-06 02:18:37 +00:00
} else {
2006-08-30 21:46:31 +00:00
$link = get_option ( 'home' ) . " /?page_id= $id " ;
2004-10-06 02:18:37 +00:00
}
2006-06-07 23:30:09 +00:00
if ( 'page' == get_option ( 'show_on_front' ) && $id == get_option ( 'page_on_front' ) )
2006-08-30 21:46:31 +00:00
$link = get_option ( 'home' );
2006-06-07 23:30:09 +00:00
2005-02-06 03:40:08 +00:00
return apply_filters ( 'page_link' , $link , $id );
2004-10-06 02:18:37 +00:00
}
2005-11-15 22:55:24 +00:00
function get_attachment_link ( $id = false ) {
2005-10-20 20:48:32 +00:00
global $post , $wp_rewrite ;
$link = false ;
if ( ! $id ) {
$id = $post -> ID ;
}
$object = get_post ( $id );
if ( $wp_rewrite -> using_permalinks () && ( $object -> post_parent > 0 ) ) {
$parent = get_post ( $object -> post_parent );
$parentlink = get_permalink ( $object -> post_parent );
if ( ! strstr ( $parentlink , '?' ) )
$link = trim ( $parentlink , '/' ) . '/' . $object -> post_name . '/' ;
}
if ( ! $link ) {
2005-11-15 22:55:24 +00:00
$link = get_bloginfo ( 'home' ) . " /?attachment_id= $id " ;
2005-10-20 20:48:32 +00:00
}
2005-11-15 22:55:24 +00:00
return apply_filters ( 'attachment_link' , $link , $id );
2005-10-20 20:48:32 +00:00
}
2004-11-19 20:54:16 +00:00
function get_year_link ( $year ) {
2005-02-07 07:37:53 +00:00
global $wp_rewrite ;
2005-10-17 23:41:28 +00:00
if ( ! $year )
2006-08-30 21:46:31 +00:00
$year = gmdate ( 'Y' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-17 23:41:28 +00:00
$yearlink = $wp_rewrite -> get_year_permastruct ();
if ( ! empty ( $yearlink ) ) {
$yearlink = str_replace ( '%year%' , $year , $yearlink );
2006-08-30 21:46:31 +00:00
return apply_filters ( 'year_link' , get_option ( 'home' ) . trailingslashit ( $yearlink ), $year );
2005-10-17 23:41:28 +00:00
} else {
2006-08-30 21:46:31 +00:00
return apply_filters ( 'year_link' , get_option ( 'home' ) . '/?m=' . $year , $year );
2005-10-17 23:41:28 +00:00
}
2004-11-19 20:54:16 +00:00
}
2004-01-27 09:58:01 +00:00
function get_month_link ( $year , $month ) {
2005-10-17 23:41:28 +00:00
global $wp_rewrite ;
if ( ! $year )
2006-08-30 21:46:31 +00:00
$year = gmdate ( 'Y' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-17 23:41:28 +00:00
if ( ! $month )
2006-08-30 21:46:31 +00:00
$month = gmdate ( 'm' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-17 23:41:28 +00:00
$monthlink = $wp_rewrite -> get_month_permastruct ();
if ( ! empty ( $monthlink ) ) {
$monthlink = str_replace ( '%year%' , $year , $monthlink );
$monthlink = str_replace ( '%monthnum%' , zeroise ( intval ( $month ), 2 ), $monthlink );
2006-08-30 21:46:31 +00:00
return apply_filters ( 'month_link' , get_option ( 'home' ) . trailingslashit ( $monthlink ), $year , $month );
2005-10-17 23:41:28 +00:00
} else {
2006-08-30 21:46:31 +00:00
return apply_filters ( 'month_link' , get_option ( 'home' ) . '/?m=' . $year . zeroise ( $month , 2 ), $year , $month );
2005-10-17 23:41:28 +00:00
}
2004-01-27 09:58:01 +00:00
}
function get_day_link ( $year , $month , $day ) {
2005-10-17 23:41:28 +00:00
global $wp_rewrite ;
if ( ! $year )
2006-08-30 21:46:31 +00:00
$year = gmdate ( 'Y' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-17 23:41:28 +00:00
if ( ! $month )
2006-08-30 21:46:31 +00:00
$month = gmdate ( 'm' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-17 23:41:28 +00:00
if ( ! $day )
2006-08-30 21:46:31 +00:00
$day = gmdate ( 'j' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-17 23:41:28 +00:00
$daylink = $wp_rewrite -> get_day_permastruct ();
if ( ! empty ( $daylink ) ) {
$daylink = str_replace ( '%year%' , $year , $daylink );
$daylink = str_replace ( '%monthnum%' , zeroise ( intval ( $month ), 2 ), $daylink );
$daylink = str_replace ( '%day%' , zeroise ( intval ( $day ), 2 ), $daylink );
2006-08-30 21:46:31 +00:00
return apply_filters ( 'day_link' , get_option ( 'home' ) . trailingslashit ( $daylink ), $year , $month , $day );
2005-10-17 23:41:28 +00:00
} else {
2006-08-30 21:46:31 +00:00
return apply_filters ( 'day_link' , get_option ( 'home' ) . '/?m=' . $year . zeroise ( $month , 2 ) . zeroise ( $day , 2 ), $year , $month , $day );
2005-10-17 23:41:28 +00:00
}
2004-06-02 07:14:03 +00:00
}
function get_feed_link ( $feed = 'rss2' ) {
2004-12-03 02:38:11 +00:00
global $wp_rewrite ;
2005-01-26 22:46:40 +00:00
$do_perma = 0 ;
2006-08-30 21:46:31 +00:00
$feed_url = get_option ( 'siteurl' );
2005-01-26 22:46:40 +00:00
$comment_feed_url = $feed_url ;
2004-06-02 07:14:03 +00:00
2005-01-26 22:46:40 +00:00
$permalink = $wp_rewrite -> get_feed_permastruct ();
2005-10-17 23:41:28 +00:00
if ( '' != $permalink ) {
2005-01-26 22:46:40 +00:00
if ( false !== strpos ( $feed , 'comments_' ) ) {
$feed = str_replace ( 'comments_' , '' , $feed );
$permalink = $wp_rewrite -> get_comment_feed_permastruct ();
}
if ( 'rss2' == $feed )
$feed = '' ;
$permalink = str_replace ( '%feed%' , $feed , $permalink );
2005-02-01 02:01:38 +00:00
$permalink = preg_replace ( '#/+#' , '/' , " / $permalink / " );
2006-08-30 21:46:31 +00:00
$output = get_option ( 'home' ) . $permalink ;
2005-01-26 22:46:40 +00:00
} else {
if ( false !== strpos ( $feed , 'comments_' ) )
2005-02-26 06:12:16 +00:00
$feed = str_replace ( 'comments_' , 'comments-' , $feed );
2005-01-26 22:46:40 +00:00
2006-08-30 21:46:31 +00:00
$output = get_option ( 'home' ) . " /?feed= { $feed } " ;
2005-01-26 22:46:40 +00:00
}
2004-06-02 07:14:03 +00:00
2005-02-06 03:40:08 +00:00
return apply_filters ( 'feed_link' , $output , $feed );
2004-01-27 09:58:01 +00:00
}
function edit_post_link ( $link = 'Edit This' , $before = '' , $after = '' ) {
2006-02-07 01:06:04 +00:00
global $post ;
2004-01-27 09:58:01 +00:00
2005-12-13 19:19:56 +00:00
if ( is_attachment () )
2005-12-21 15:47:54 +00:00
return ;
2006-02-22 19:17:28 +00:00
if ( $post -> post_type == 'page' ) {
if ( ! current_user_can ( 'edit_page' , $post -> ID ) )
return ;
$file = 'page' ;
} else {
if ( ! current_user_can ( 'edit_post' , $post -> ID ) )
return ;
2005-12-13 19:19:56 +00:00
$file = 'post' ;
2006-02-22 19:17:28 +00:00
}
2005-12-13 19:19:56 +00:00
2006-08-30 21:46:31 +00:00
$location = get_option ( 'siteurl' ) . " /wp-admin/ { $file } .php?action=edit&post= $post->ID " ;
2005-10-17 23:41:28 +00:00
echo $before . " <a href= \" $location\ " > $link </ a > " . $after ;
2004-01-27 09:58:01 +00:00
}
function edit_comment_link ( $link = 'Edit This' , $before = '' , $after = '' ) {
2006-02-07 01:06:04 +00:00
global $post , $comment ;
2004-01-27 09:58:01 +00:00
2006-03-20 19:45:46 +00:00
if ( $post -> post_type == 'page' ){
2006-02-22 19:17:28 +00:00
if ( ! current_user_can ( 'edit_page' , $post -> ID ) )
return ;
2006-03-20 19:45:46 +00:00
} else {
2006-02-22 19:17:28 +00:00
if ( ! current_user_can ( 'edit_post' , $post -> ID ) )
return ;
2006-03-20 19:45:46 +00:00
}
2004-01-27 09:58:01 +00:00
2006-10-31 06:13:43 +00:00
$location = get_option ( 'siteurl' ) . " /wp-admin/comment.php?action=editcomment&c= $comment->comment_ID " ;
2005-10-17 23:41:28 +00:00
echo $before . " <a href=' $location '> $link </a> " . $after ;
2004-01-27 09:58:01 +00:00
}
2004-06-02 07:44:43 +00:00
// Navigation links
2004-08-24 01:35:57 +00:00
function get_previous_post ( $in_same_cat = false , $excluded_categories = '' ) {
2005-04-18 21:47:35 +00:00
global $post , $wpdb ;
2004-08-24 01:35:57 +00:00
2005-11-15 22:55:24 +00:00
if ( ! is_single () || is_attachment () )
2005-04-18 21:47:35 +00:00
return null ;
2005-10-17 23:41:28 +00:00
2005-04-18 21:47:35 +00:00
$current_post_date = $post -> post_date ;
2005-10-20 20:48:32 +00:00
2005-04-18 21:47:35 +00:00
$join = '' ;
2005-10-17 23:41:28 +00:00
if ( $in_same_cat ) {
2005-04-18 21:47:35 +00:00
$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts .ID= $wpdb->post2cat .post_id " ;
$cat_array = get_the_category ( $post -> ID );
2005-10-17 23:41:28 +00:00
$join .= ' AND (category_id = ' . intval ( $cat_array [ 0 ] -> cat_ID );
for ( $i = 1 ; $i < ( count ( $cat_array )); $i ++ ) {
2005-04-18 21:47:35 +00:00
$join .= ' OR category_id = ' . intval ( $cat_array [ $i ] -> cat_ID );
}
$join .= ')' ;
}
2004-08-24 01:35:57 +00:00
2005-04-18 21:47:35 +00:00
$sql_exclude_cats = '' ;
2005-10-17 23:41:28 +00:00
if ( ! empty ( $excluded_categories ) ) {
2006-02-09 08:26:30 +00:00
$blah = explode ( ' and ' , $excluded_categories );
2005-10-17 23:41:28 +00:00
foreach ( $blah as $category ) {
2005-04-18 21:47:35 +00:00
$category = intval ( $category );
2006-02-09 08:26:30 +00:00
$sql_cat_ids = " OR pc.category_ID = ' $category ' " ;
2005-04-18 21:47:35 +00:00
}
2006-02-09 08:26:30 +00:00
$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 " );
$posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode ( $posts_in_ex_cats , ',' ) . ')' ;
2005-04-18 21:47:35 +00:00
}
2004-08-24 01:35:57 +00:00
2006-02-09 10:03:48 +00:00
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 " );
2004-08-24 01:35:57 +00:00
}
function get_next_post ( $in_same_cat = false , $excluded_categories = '' ) {
2005-04-18 21:47:35 +00:00
global $post , $wpdb ;
2004-08-24 01:35:57 +00:00
2005-11-15 22:55:24 +00:00
if ( ! is_single () || is_attachment () )
2005-04-18 21:47:35 +00:00
return null ;
2004-08-24 01:35:57 +00:00
2005-04-18 21:47:35 +00:00
$current_post_date = $post -> post_date ;
2006-02-12 07:53:23 +00:00
2005-04-18 21:47:35 +00:00
$join = '' ;
2005-10-17 23:41:28 +00:00
if ( $in_same_cat ) {
2005-04-18 21:47:35 +00:00
$join = " INNER JOIN $wpdb->post2cat ON $wpdb->posts .ID= $wpdb->post2cat .post_id " ;
$cat_array = get_the_category ( $post -> ID );
2005-10-17 23:41:28 +00:00
$join .= ' AND (category_id = ' . intval ( $cat_array [ 0 ] -> cat_ID );
for ( $i = 1 ; $i < ( count ( $cat_array )); $i ++ ) {
2005-04-18 21:47:35 +00:00
$join .= ' OR category_id = ' . intval ( $cat_array [ $i ] -> cat_ID );
}
$join .= ')' ;
}
2004-08-24 01:35:57 +00:00
2005-04-18 21:47:35 +00:00
$sql_exclude_cats = '' ;
2005-10-17 23:41:28 +00:00
if ( ! empty ( $excluded_categories ) ) {
2006-02-09 08:26:30 +00:00
$blah = explode ( ' and ' , $excluded_categories );
2005-10-17 23:41:28 +00:00
foreach ( $blah as $category ) {
2005-04-18 21:47:35 +00:00
$category = intval ( $category );
2006-02-09 08:26:30 +00:00
$sql_cat_ids = " OR pc.category_ID = ' $category ' " ;
2005-04-18 21:47:35 +00:00
}
2006-02-09 08:26:30 +00:00
$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 " );
$posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode ( $posts_in_ex_cats , ',' ) . ')' ;
2005-04-18 21:47:35 +00:00
}
2004-08-24 01:35:57 +00:00
2006-02-12 07:41:56 +00:00
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 " );
2004-08-24 01:35:57 +00:00
}
2005-10-17 23:41:28 +00:00
function previous_post_link ( $format = '« %link' , $link = '%title' , $in_same_cat = false , $excluded_categories = '' ) {
2005-11-17 01:57:18 +00:00
if ( is_attachment () )
2005-10-20 20:48:32 +00:00
$post = & get_post ( $GLOBALS [ 'post' ] -> post_parent );
2005-11-17 01:57:18 +00:00
else
2005-10-20 20:48:32 +00:00
$post = get_previous_post ( $in_same_cat , $excluded_categories );
2004-08-24 01:35:57 +00:00
2005-10-17 23:41:28 +00:00
if ( ! $post )
return ;
2004-08-24 01:35:57 +00:00
2005-10-17 23:41:28 +00:00
$title = apply_filters ( 'the_title' , $post -> post_title , $post );
$string = '<a href="' . get_permalink ( $post -> ID ) . '">' ;
$link = str_replace ( '%title' , $title , $link );
2005-10-20 20:48:32 +00:00
$link = $pre . $string . $link . '</a>' ;
2004-08-24 01:35:57 +00:00
2005-10-20 20:48:32 +00:00
$format = str_replace ( '%link' , $link , $format );
2005-11-17 01:57:18 +00:00
2005-10-20 20:48:32 +00:00
echo $format ;
2004-08-24 01:35:57 +00:00
}
function next_post_link ( $format = '%link »' , $link = '%title' , $in_same_cat = false , $excluded_categories = '' ) {
2005-10-17 23:41:28 +00:00
$post = get_next_post ( $in_same_cat , $excluded_categories );
2004-08-24 01:35:57 +00:00
2005-10-17 23:41:28 +00:00
if ( ! $post )
return ;
2004-08-24 01:35:57 +00:00
2005-10-17 23:41:28 +00:00
$title = apply_filters ( 'the_title' , $post -> post_title , $post );
$string = '<a href="' . get_permalink ( $post -> ID ) . '">' ;
$link = str_replace ( '%title' , $title , $link );
$link = $string . $link . '</a>' ;
$format = str_replace ( '%link' , $link , $format );
2004-08-24 01:35:57 +00:00
2005-10-20 20:48:32 +00:00
echo $format ;
2004-08-24 01:35:57 +00:00
}
2005-03-28 02:34:16 +00:00
function get_pagenum_link ( $pagenum = 1 ) {
2004-12-03 02:38:11 +00:00
global $wp_rewrite ;
2006-01-15 23:42:10 +00:00
$qstr = wp_specialchars ( $_SERVER [ 'REQUEST_URI' ]);
2005-03-28 02:34:16 +00:00
$page_querystring = " paged " ;
$page_modstring = " page/ " ;
$page_modregex = " page/? " ;
$permalink = 0 ;
2006-08-30 21:46:31 +00:00
$home_root = parse_url ( get_option ( 'home' ));
2005-03-28 02:34:16 +00:00
$home_root = $home_root [ 'path' ];
$home_root = trailingslashit ( $home_root );
$qstr = preg_replace ( '|^' . $home_root . '|' , '' , $qstr );
$qstr = preg_replace ( '|^/+|' , '' , $qstr );
2005-05-13 21:11:26 +00:00
$index = $_SERVER [ 'PHP_SELF' ];
2005-03-30 06:51:55 +00:00
$index = preg_replace ( '|^' . $home_root . '|' , '' , $index );
$index = preg_replace ( '|^/+|' , '' , $index );
2005-03-28 02:34:16 +00:00
// if we already have a QUERY style page string
2005-10-17 23:41:28 +00:00
if ( stristr ( $qstr , $page_querystring ) ) {
2005-03-28 02:34:16 +00:00
$replacement = " $page_querystring = $pagenum " ;
$qstr = preg_replace ( " / " . $page_querystring . " [^ \ d]+ \ d+/ " , $replacement , $qstr );
// if we already have a mod_rewrite style page string
2005-10-17 23:41:28 +00:00
} elseif ( preg_match ( '|' . $page_modregex . '\d+|' , $qstr ) ) {
2005-03-28 02:34:16 +00:00
$permalink = 1 ;
$qstr = preg_replace ( '|' . $page_modregex . '\d+|' , " $page_modstring $pagenum " , $qstr );
// if we don't have a page string at all ...
// lets see what sort of URL we have...
} else {
// we need to know the way queries are being written
// if there's a querystring_start (a "?" usually), it's definitely not mod_rewritten
2005-10-17 23:41:28 +00:00
if ( stristr ( $qstr , '?' ) ) {
2005-03-28 02:34:16 +00:00
// so append the query string (using &, since we already have ?)
2005-10-17 23:41:28 +00:00
$qstr .= '&' . $page_querystring . '=' . $pagenum ;
2005-03-28 02:34:16 +00:00
// otherwise, it could be rewritten, OR just the default index ...
2006-08-30 21:46:31 +00:00
} elseif ( '' != get_option ( 'permalink_structure' ) && ! is_admin () ) {
2005-03-28 02:34:16 +00:00
$permalink = 1 ;
$index = $wp_rewrite -> index ;
// If it's not a path info permalink structure, trim the index.
2005-10-17 23:41:28 +00:00
if ( ! $wp_rewrite -> using_index_permalinks () ) {
2005-03-28 02:34:16 +00:00
$qstr = preg_replace ( " #/* " . $index . " /*# " , '/' , $qstr );
} else {
// If using path info style permalinks, make sure the index is in
2006-08-30 16:40:17 +00:00
// the URL.
2005-10-17 23:41:28 +00:00
if ( strpos ( $qstr , $index ) === false )
2005-03-28 02:34:16 +00:00
$qstr = '/' . $index . $qstr ;
}
2005-10-17 23:41:28 +00:00
$qstr = trailingslashit ( $qstr ) . $page_modstring . $pagenum ;
2005-03-28 02:34:16 +00:00
} else {
$qstr = $index . '?' . $page_querystring . '=' . $pagenum ;
}
}
2004-06-02 07:44:43 +00:00
2005-03-28 02:34:16 +00:00
$qstr = preg_replace ( '|^/+|' , '' , $qstr );
2005-10-17 23:41:28 +00:00
if ( $permalink )
$qstr = trailingslashit ( $qstr );
2006-08-30 21:46:31 +00:00
$qstr = preg_replace ( '/&([^#])(?![a-z]{1,8};)/' , '&$1' , trailingslashit ( get_option ( 'home' ) ) . $qstr );
2006-02-12 07:53:23 +00:00
2005-08-23 07:43:11 +00:00
// showing /page/1/ or ?paged=1 is redundant
2005-10-17 23:41:28 +00:00
if ( 1 === $pagenum ) {
2005-08-23 07:43:11 +00:00
$qstr = str_replace ( 'page/1/' , '' , $qstr ); // for mod_rewrite style
$qstr = remove_query_arg ( 'paged' , $qstr ); // for query style
}
return $qstr ;
2004-06-02 07:44:43 +00:00
}
function next_posts ( $max_page = 0 ) { // original by cfactor at cooltux.org
2005-10-17 23:41:28 +00:00
global $paged , $pagenow ;
if ( ! is_single () ) {
if ( ! $paged )
$paged = 1 ;
$nextpage = intval ( $paged ) + 1 ;
if ( ! $max_page || $max_page >= $nextpage )
echo get_pagenum_link ( $nextpage );
}
2004-06-02 07:44:43 +00:00
}
function next_posts_link ( $label = 'Next Page »' , $max_page = 0 ) {
2006-11-08 21:22:35 +00:00
global $paged , $wpdb , $wp_query ;
2005-10-17 23:41:28 +00:00
if ( ! $max_page ) {
2006-11-08 21:22:35 +00:00
$max_page = $wp_query -> max_num_pages ;
2005-10-17 23:41:28 +00:00
}
if ( ! $paged )
$paged = 1 ;
$nextpage = intval ( $paged ) + 1 ;
if ( ( ! is_single ()) && ( empty ( $paged ) || $nextpage <= $max_page ) ) {
echo '<a href="' ;
next_posts ( $max_page );
echo '">' . preg_replace ( '/&([^#])(?![a-z]{1,8};)/' , '&$1' , $label ) . '</a>' ;
}
2004-06-02 07:44:43 +00:00
}
function previous_posts () { // original by cfactor at cooltux.org
2005-10-17 23:41:28 +00:00
global $paged , $pagenow ;
2004-06-02 07:44:43 +00:00
2005-10-17 23:41:28 +00:00
if ( ! is_single () ) {
$nextpage = intval ( $paged ) - 1 ;
if ( $nextpage < 1 )
$nextpage = 1 ;
echo get_pagenum_link ( $nextpage );
}
2004-06-02 07:44:43 +00:00
}
2005-10-17 23:41:28 +00:00
2004-06-02 07:44:43 +00:00
function previous_posts_link ( $label = '« Previous Page' ) {
2005-10-17 23:41:28 +00:00
global $paged ;
if ( ( ! is_single ()) && ( $paged > 1 ) ) {
echo '<a href="' ;
previous_posts ();
echo '">' . preg_replace ( '/&([^#])(?![a-z]{1,8};)/' , '&$1' , $label ) . '</a>' ;
}
2004-06-02 07:44:43 +00:00
}
function posts_nav_link ( $sep = ' — ' , $prelabel = '« Previous Page' , $nxtlabel = 'Next Page »' ) {
2006-11-08 21:22:35 +00:00
global $wp_query ;
if ( ! is_singular () ) {
$max_num_pages = $wp_query -> max_num_pages ;
2006-03-20 19:43:57 +00:00
$paged = get_query_var ( 'paged' );
//only have sep if there's both prev and next results
if ( $paged < 2 || $paged >= $max_num_pages ) {
$sep = '' ;
2004-10-12 13:04:35 +00:00
}
2004-08-16 22:50:06 +00:00
2005-10-17 23:41:28 +00:00
if ( $max_num_pages > 1 ) {
previous_posts_link ( $prelabel );
echo preg_replace ( '/&([^#])(?![a-z]{1,8};)/' , '&$1' , $sep );
2006-03-20 19:43:57 +00:00
next_posts_link ( $nxtlabel );
2005-10-17 23:41:28 +00:00
}
}
2004-06-02 07:44:43 +00:00
}
2005-10-20 20:48:32 +00:00
?>