2004-01-27 10:58:01 +01:00
< ? php
2005-10-18 01:41:28 +02:00
2004-04-21 06:12:55 +02:00
function the_permalink () {
2005-01-15 00:43:20 +01:00
echo apply_filters ( 'the_permalink' , get_permalink ());
2004-05-18 10:36:53 +02:00
}
2005-10-18 01:41:28 +02:00
2007-02-15 08:07:12 +01:00
/**
* Conditionally adds a trailing slash if the permalink structure
* has a trailing slash , strips the trailing slash if not
* @ global object Uses $wp_rewrite
* @ param $string string a URL with or without a trailing slash
2007-03-10 07:18:43 +01:00
* @ param $type_of_url string the type of URL being considered ( e . g . single , category , etc ) for use in the filter
2007-02-15 08:07:12 +01:00
* @ return string
*/
2007-03-10 07:18:43 +01:00
function user_trailingslashit ( $string , $type_of_url = '' ) {
2007-02-15 08:07:12 +01:00
global $wp_rewrite ;
if ( $wp_rewrite -> use_trailing_slashes )
$string = trailingslashit ( $string );
else
2007-03-10 07:25:33 +01:00
$string = untrailingslashit ( $string );
2007-03-10 07:18:43 +01:00
// Note that $type_of_url can be one of following:
// single, single_trackback, single_feed, single_paged, feed, category, page, year, month, day, paged
$string = apply_filters ( 'user_trailingslashit' , $string , $type_of_url );
2007-02-15 08:07:12 +01:00
return $string ;
}
2004-01-27 10:58:01 +01:00
function permalink_anchor ( $mode = 'id' ) {
2005-10-18 01:41:28 +02: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 10:58:01 +01:00
}
2005-10-18 01:41:28 +02:00
2005-03-27 22:45:01 +02:00
function get_permalink ( $id = 0 ) {
2004-09-05 04:41:01 +02:00
$rewritecode = array (
'%year%' ,
'%monthnum%' ,
'%day%' ,
'%hour%' ,
'%minute%' ,
'%second%' ,
'%postname%' ,
'%post_id%' ,
'%category%' ,
'%author%' ,
'%pagename%'
);
2004-08-10 07:35:59 +02:00
2005-10-18 01:41:28 +02:00
$post = & get_post ( $id );
2007-08-28 20:58:54 +02:00
if ( empty ( $post -> ID ) ) return FALSE ;
2006-02-09 11:03:48 +01:00
if ( $post -> post_type == 'page' )
2005-03-27 22:45:01 +02:00
return get_page_link ( $post -> ID );
2006-02-09 11:03:48 +01:00
elseif ( $post -> post_type == 'attachment' )
2005-11-15 23:55:24 +01:00
return get_attachment_link ( $post -> ID );
2004-11-13 00:08:51 +01:00
2006-08-30 23:46:31 +02:00
$permalink = get_option ( 'permalink_structure' );
2004-08-10 07:35:59 +02:00
2007-06-16 00:11:01 +02:00
if ( '' != $permalink && ! in_array ( $post -> post_status , array ( 'draft' , 'pending' )) ) {
2005-03-27 22:45:01 +02:00
$unixtime = strtotime ( $post -> post_date );
2004-06-04 04:36:46 +02:00
2005-02-16 16:49:02 +01:00
$category = '' ;
2007-03-07 06:29:15 +01:00
if ( strpos ( $permalink , '%category%' ) !== false ) {
2005-03-27 22:45:01 +02:00
$cats = get_the_category ( $post -> ID );
2007-05-29 06:54:45 +02:00
if ( $cats )
usort ( $cats , '_usort_terms_by_ID' ); // order by ID
2007-05-28 20:47:08 +02:00
$category = $cats [ 0 ] -> slug ;
if ( $parent = $cats [ 0 ] -> parent )
2005-10-18 01:41:28 +02:00
$category = get_category_parents ( $parent , FALSE , '/' , TRUE ) . $category ;
2005-02-16 16:49:02 +01:00
}
2005-01-25 06:01:54 +01:00
2005-03-27 22:45:01 +02:00
$authordata = get_userdata ( $post -> post_author );
2004-09-05 04:41:01 +02:00
$author = $authordata -> user_nicename ;
2006-02-17 02:04:20 +01:00
$date = explode ( " " , date ( 'Y m d H i s' , $unixtime ));
2006-11-19 08:56:05 +01:00
$rewritereplace =
2004-09-05 04:41:01 +02:00
array (
2006-02-17 02:04:20 +01:00
$date [ 0 ],
$date [ 1 ],
$date [ 2 ],
$date [ 3 ],
$date [ 4 ],
$date [ 5 ],
2005-03-27 22:45:01 +02:00
$post -> post_name ,
$post -> ID ,
2004-09-05 04:41:01 +02:00
$category ,
$author ,
2005-03-27 22:45:01 +02:00
$post -> post_name ,
2004-09-05 04:41:01 +02:00
);
2007-03-10 07:18:43 +01:00
$permalink = get_option ( 'home' ) . str_replace ( $rewritecode , $rewritereplace , $permalink );
$permalink = user_trailingslashit ( $permalink , 'single' );
return apply_filters ( 'post_link' , $permalink , $post );
2004-09-05 04:41:01 +02:00
} else { // if they're not using the fancy permalink option
2006-08-30 23:46:31 +02:00
$permalink = get_option ( 'home' ) . '/?p=' . $post -> ID ;
2005-03-27 22:45:01 +02:00
return apply_filters ( 'post_link' , $permalink , $post );
2004-09-05 04:41:01 +02:00
}
2004-01-27 10:58:01 +01:00
}
2006-06-08 01:17:59 +02:00
// get permalink from post ID
2007-04-25 01:21:56 +02:00
function post_permalink ( $post_id = 0 , $deprecated = '' ) {
2006-06-08 01:17:59 +02:00
return get_permalink ( $post_id );
}
2006-11-17 21:00:11 +01:00
// Respects page_on_front. Use this one.
2004-10-06 04:18:37 +02:00
function get_page_link ( $id = false ) {
2006-11-17 21:00:11 +01:00
global $post ;
2007-03-22 02:04:19 +01:00
$id = ( int ) $id ;
2006-11-17 21:00:11 +01:00
if ( ! $id )
2007-03-23 01:59:21 +01:00
$id = ( int ) $post -> ID ;
2006-11-17 21:00:11 +01:00
if ( 'page' == get_option ( 'show_on_front' ) && $id == get_option ( 'page_on_front' ) )
$link = get_option ( 'home' );
else
$link = _get_page_link ( $id );
return apply_filters ( 'page_link' , $link , $id );
}
// Ignores page_on_front. Internal use only.
function _get_page_link ( $id = false ) {
2004-12-03 03:38:11 +01:00
global $post , $wp_rewrite ;
2004-10-06 04:18:37 +02:00
2005-10-18 01:41:28 +02:00
if ( ! $id )
2007-03-23 01:59:21 +01:00
$id = ( int ) $post -> ID ;
2004-10-06 04:18:37 +02:00
2005-01-26 23:46:40 +01:00
$pagestruct = $wp_rewrite -> get_page_permastruct ();
2004-10-06 04:18:37 +02:00
2006-04-15 02:59:40 +02:00
if ( '' != $pagestruct && 'draft' != $post -> post_status ) {
2004-10-06 04:18:37 +02:00
$link = get_page_uri ( $id );
2005-01-26 23:46:40 +01:00
$link = str_replace ( '%pagename%' , $link , $pagestruct );
2007-02-15 08:07:12 +01:00
$link = get_option ( 'home' ) . " / $link " ;
2007-03-10 07:18:43 +01:00
$link = user_trailingslashit ( $link , 'page' );
2004-10-06 04:18:37 +02:00
} else {
2006-08-30 23:46:31 +02:00
$link = get_option ( 'home' ) . " /?page_id= $id " ;
2004-10-06 04:18:37 +02:00
}
2006-11-17 21:00:11 +01:00
return apply_filters ( '_get_page_link' , $link , $id );
2004-10-06 04:18:37 +02:00
}
2005-11-15 23:55:24 +01:00
function get_attachment_link ( $id = false ) {
2005-10-20 22:48:32 +02:00
global $post , $wp_rewrite ;
$link = false ;
if ( ! $id ) {
2007-03-23 01:59:21 +01:00
$id = ( int ) $post -> ID ;
2005-10-20 22:48:32 +02:00
}
$object = get_post ( $id );
2006-12-12 23:50:54 +01:00
if ( $wp_rewrite -> using_permalinks () && ( $object -> post_parent > 0 ) && ( $object -> post_parent != $id ) ) {
2005-10-20 22:48:32 +02:00
$parent = get_post ( $object -> post_parent );
2006-11-17 21:00:11 +01:00
if ( 'page' == $parent -> post_type )
$parentlink = _get_page_link ( $object -> post_parent ); // Ignores page_on_front
else
$parentlink = get_permalink ( $object -> post_parent );
2007-03-07 06:29:15 +01:00
if ( strpos ( $parentlink , '?' ) === false )
2005-10-20 22:48:32 +02:00
$link = trim ( $parentlink , '/' ) . '/' . $object -> post_name . '/' ;
}
if ( ! $link ) {
2007-03-07 04:05:41 +01:00
$link = get_bloginfo ( 'url' ) . " /?attachment_id= $id " ;
2005-10-20 22:48:32 +02:00
}
2005-11-15 23:55:24 +01:00
return apply_filters ( 'attachment_link' , $link , $id );
2005-10-20 22:48:32 +02:00
}
2004-11-19 21:54:16 +01:00
function get_year_link ( $year ) {
2005-02-07 08:37:53 +01:00
global $wp_rewrite ;
2005-10-18 01:41:28 +02:00
if ( ! $year )
2006-08-30 23:46:31 +02:00
$year = gmdate ( 'Y' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-18 01:41:28 +02:00
$yearlink = $wp_rewrite -> get_year_permastruct ();
if ( ! empty ( $yearlink ) ) {
$yearlink = str_replace ( '%year%' , $year , $yearlink );
2007-03-10 07:18:43 +01:00
return apply_filters ( 'year_link' , get_option ( 'home' ) . user_trailingslashit ( $yearlink , 'year' ), $year );
2005-10-18 01:41:28 +02:00
} else {
2006-08-30 23:46:31 +02:00
return apply_filters ( 'year_link' , get_option ( 'home' ) . '/?m=' . $year , $year );
2005-10-18 01:41:28 +02:00
}
2004-11-19 21:54:16 +01:00
}
2004-01-27 10:58:01 +01:00
function get_month_link ( $year , $month ) {
2005-10-18 01:41:28 +02:00
global $wp_rewrite ;
if ( ! $year )
2006-08-30 23:46:31 +02:00
$year = gmdate ( 'Y' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-18 01:41:28 +02:00
if ( ! $month )
2006-08-30 23:46:31 +02:00
$month = gmdate ( 'm' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-18 01:41:28 +02: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 );
2007-03-10 07:18:43 +01:00
return apply_filters ( 'month_link' , get_option ( 'home' ) . user_trailingslashit ( $monthlink , 'month' ), $year , $month );
2005-10-18 01:41:28 +02:00
} else {
2006-08-30 23:46:31 +02:00
return apply_filters ( 'month_link' , get_option ( 'home' ) . '/?m=' . $year . zeroise ( $month , 2 ), $year , $month );
2005-10-18 01:41:28 +02:00
}
2004-01-27 10:58:01 +01:00
}
function get_day_link ( $year , $month , $day ) {
2005-10-18 01:41:28 +02:00
global $wp_rewrite ;
if ( ! $year )
2006-08-30 23:46:31 +02:00
$year = gmdate ( 'Y' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-18 01:41:28 +02:00
if ( ! $month )
2006-08-30 23:46:31 +02:00
$month = gmdate ( 'm' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-18 01:41:28 +02:00
if ( ! $day )
2006-08-30 23:46:31 +02:00
$day = gmdate ( 'j' , time () + ( get_option ( 'gmt_offset' ) * 3600 ));
2005-10-18 01:41:28 +02: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 );
2007-03-10 07:18:43 +01:00
return apply_filters ( 'day_link' , get_option ( 'home' ) . user_trailingslashit ( $daylink , 'day' ), $year , $month , $day );
2005-10-18 01:41:28 +02:00
} else {
2006-08-30 23:46:31 +02:00
return apply_filters ( 'day_link' , get_option ( 'home' ) . '/?m=' . $year . zeroise ( $month , 2 ) . zeroise ( $day , 2 ), $year , $month , $day );
2005-10-18 01:41:28 +02:00
}
2004-06-02 09:14:03 +02:00
}
function get_feed_link ( $feed = 'rss2' ) {
2004-12-03 03:38:11 +01:00
global $wp_rewrite ;
2005-01-26 23:46:40 +01:00
$do_perma = 0 ;
2006-08-30 23:46:31 +02:00
$feed_url = get_option ( 'siteurl' );
2005-01-26 23:46:40 +01:00
$comment_feed_url = $feed_url ;
2004-06-02 09:14:03 +02:00
2005-01-26 23:46:40 +01:00
$permalink = $wp_rewrite -> get_feed_permastruct ();
2005-10-18 01:41:28 +02:00
if ( '' != $permalink ) {
2005-01-26 23:46:40 +01: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 );
2007-02-15 08:07:12 +01:00
$permalink = preg_replace ( '#/+#' , '/' , " / $permalink " );
2007-03-10 07:18:43 +01:00
$output = get_option ( 'home' ) . user_trailingslashit ( $permalink , 'feed' );
2005-01-26 23:46:40 +01:00
} else {
if ( false !== strpos ( $feed , 'comments_' ) )
2005-02-26 07:12:16 +01:00
$feed = str_replace ( 'comments_' , 'comments-' , $feed );
2005-01-26 23:46:40 +01:00
2006-08-30 23:46:31 +02:00
$output = get_option ( 'home' ) . " /?feed= { $feed } " ;
2005-01-26 23:46:40 +01:00
}
2004-06-02 09:14:03 +02:00
2005-02-06 04:40:08 +01:00
return apply_filters ( 'feed_link' , $output , $feed );
2004-01-27 10:58:01 +01:00
}
2007-02-23 09:31:07 +01:00
function get_post_comments_feed_link ( $post_id = '' , $feed = 'rss2' ) {
global $id ;
if ( empty ( $post_id ) )
2007-03-23 01:59:21 +01:00
$post_id = ( int ) $id ;
2007-02-23 09:31:07 +01:00
if ( '' != get_option ( 'permalink_structure' ) ) {
2007-08-23 00:42:57 +02:00
$url = trailingslashit ( get_permalink ( $post_id ) ) . 'feed' ;
2007-02-23 09:31:07 +01:00
if ( 'rss2' != $feed )
$url .= " / $feed " ;
2007-03-10 07:18:43 +01:00
$url = user_trailingslashit ( $url , 'single_feed' );
2007-02-23 09:31:07 +01:00
} else {
2007-08-23 00:42:57 +02:00
$type = get_post_field ( 'post_type' , $post_id );
if ( 'page' == $type )
$url = get_option ( 'home' ) . " /?feed= $feed &page_id= $post_id " ;
else
$url = get_option ( 'home' ) . " /?feed= $feed &p= $post_id " ;
2007-02-23 09:31:07 +01:00
}
2007-02-27 16:24:54 +01:00
return apply_filters ( 'post_comments_feed_link' , $url );
2007-02-23 09:31:07 +01:00
}
2007-04-25 01:21:56 +02:00
function get_edit_post_link ( $id = 0 ) {
$post = & get_post ( $id );
2007-06-14 04:25:30 +02:00
2007-04-28 18:45:47 +02:00
if ( $post -> post_type == 'attachment' ) {
2005-12-21 16:47:54 +01:00
return ;
2007-04-28 18:45:47 +02:00
} elseif ( $post -> post_type == 'page' ) {
2007-04-25 01:21:56 +02:00
if ( ! current_user_can ( 'edit_page' , $post -> ID ) )
2006-02-22 20:17:28 +01:00
return ;
2007-06-14 04:25:30 +02:00
2006-02-22 20:17:28 +01:00
$file = 'page' ;
} else {
2007-04-25 01:21:56 +02:00
if ( ! current_user_can ( 'edit_post' , $post -> ID ) )
2006-02-22 20:17:28 +01:00
return ;
2007-06-14 04:25:30 +02:00
2005-12-13 20:19:56 +01:00
$file = 'post' ;
2006-02-22 20:17:28 +01:00
}
2007-06-14 04:25:30 +02:00
2007-04-25 01:21:56 +02:00
return apply_filters ( 'get_edit_post_link' , get_bloginfo ( 'wpurl' ) . '/wp-admin/' . $file . '.php?action=edit&post=' . $post -> ID , $post -> ID );
2004-01-27 10:58:01 +01:00
}
2007-04-25 01:21:56 +02:00
function edit_post_link ( $link = 'Edit This' , $before = '' , $after = '' ) {
global $post ;
2007-04-27 05:51:36 +02:00
2007-04-28 18:45:47 +02:00
if ( $post -> post_type == 'attachment' ) {
2007-04-27 05:51:36 +02:00
return ;
2007-04-28 18:45:47 +02:00
} elseif ( $post -> post_type == 'page' ) {
2007-04-27 05:51:36 +02:00
if ( ! current_user_can ( 'edit_page' , $post -> ID ) )
return ;
2007-06-14 04:25:30 +02:00
2007-04-27 05:51:36 +02:00
$file = 'page' ;
} else {
if ( ! current_user_can ( 'edit_post' , $post -> ID ) )
return ;
2007-06-14 04:25:30 +02:00
2007-04-27 05:51:36 +02:00
$file = 'post' ;
}
2007-04-25 01:21:56 +02:00
$link = '<a href="' . get_edit_post_link ( $post -> ID ) . '" title="' . __ ( 'Edit post' ) . '">' . $link . '</a>' ;
echo $before . apply_filters ( 'edit_post_link' , $link , $post -> ID ) . $after ;
}
2007-04-18 01:00:35 +02:00
2007-04-25 01:21:56 +02:00
function get_edit_comment_link ( $comment_id = 0 ) {
$comment = & get_comment ( $comment_id );
$post = & get_post ( $comment -> comment_post_ID );
2007-06-14 04:25:30 +02:00
2007-04-28 18:45:47 +02:00
if ( $post -> post_type == 'attachment' ) {
2007-04-25 01:21:56 +02:00
return ;
2007-04-28 18:45:47 +02:00
} elseif ( $post -> post_type == 'page' ) {
2007-04-25 01:21:56 +02:00
if ( ! current_user_can ( 'edit_page' , $post -> ID ) )
2006-02-22 20:17:28 +01:00
return ;
2007-04-28 18:45:47 +02:00
} else {
2007-04-25 01:21:56 +02:00
if ( ! current_user_can ( 'edit_post' , $post -> ID ) )
2006-02-22 20:17:28 +01:00
return ;
2007-04-28 18:45:47 +02:00
}
2007-04-25 01:21:56 +02:00
$location = get_bloginfo ( 'wpurl' ) . '/wp-admin/comment.php?action=editcomment&c=' . $comment -> comment_ID ;
return apply_filters ( 'get_edit_comment_link' , $location );
}
2004-01-27 10:58:01 +01:00
2007-04-25 01:21:56 +02:00
function edit_comment_link ( $link = 'Edit This' , $before = '' , $after = '' ) {
2007-04-27 05:51:36 +02:00
global $comment , $post ;
2007-04-28 18:45:47 +02:00
if ( $post -> post_type == 'attachment' ) {
2007-04-27 05:51:36 +02:00
return ;
2007-04-28 18:45:47 +02:00
} elseif ( $post -> post_type == 'page' ) {
2007-04-27 05:51:36 +02:00
if ( ! current_user_can ( 'edit_page' , $post -> ID ) )
return ;
2007-04-28 18:45:47 +02:00
} else {
2007-04-27 05:51:36 +02:00
if ( ! current_user_can ( 'edit_post' , $post -> ID ) )
return ;
2007-04-28 18:45:47 +02:00
}
2007-04-27 05:51:36 +02:00
2007-04-25 01:21:56 +02:00
$link = '<a href="' . get_edit_comment_link ( $comment -> comment_ID ) . '" title="' . __ ( 'Edit comment' ) . '">' . $link . '</a>' ;
echo $before . apply_filters ( 'edit_comment_link' , $link , $comment -> comment_ID ) . $after ;
2004-01-27 10:58:01 +01:00
}
2004-06-02 09:44:43 +02:00
// Navigation links
2004-08-24 03:35:57 +02:00
function get_previous_post ( $in_same_cat = false , $excluded_categories = '' ) {
2005-04-18 23:47:35 +02:00
global $post , $wpdb ;
2004-08-24 03:35:57 +02:00
2005-11-15 23:55:24 +01:00
if ( ! is_single () || is_attachment () )
2005-04-18 23:47:35 +02:00
return null ;
2005-10-18 01:41:28 +02:00
2005-04-18 23:47:35 +02:00
$current_post_date = $post -> post_date ;
2005-10-20 22:48:32 +02:00
2005-04-18 23:47:35 +02:00
$join = '' ;
2005-10-18 01:41:28 +02:00
if ( $in_same_cat ) {
2007-05-29 18:52:31 +02:00
$join = " INNER JOIN $wpdb->term_relationships AS tr ON $wpdb->posts .ID = tr.object_id " ;
2007-06-19 02:33:44 +02:00
$cat_array = wp_get_object_terms ( $post -> ID , 'category' , 'fields=tt_ids' );
2007-05-29 18:52:31 +02:00
$join .= ' AND (tr.term_taxonomy_id = ' . intval ( $cat_array [ 0 ]);
2005-10-18 01:41:28 +02:00
for ( $i = 1 ; $i < ( count ( $cat_array )); $i ++ ) {
2007-05-29 18:52:31 +02:00
$join .= ' OR tr.term_taxonomy_id = ' . intval ( $cat_array [ $i ]);
2005-04-18 23:47:35 +02:00
}
2006-11-19 08:56:05 +01:00
$join .= ')' ;
2005-04-18 23:47:35 +02:00
}
2004-08-24 03:35:57 +02:00
2005-04-18 23:47:35 +02:00
$sql_exclude_cats = '' ;
2005-10-18 01:41:28 +02:00
if ( ! empty ( $excluded_categories ) ) {
2006-02-09 09:26:30 +01:00
$blah = explode ( ' and ' , $excluded_categories );
2007-05-29 18:52:31 +02:00
$posts_in_ex_cats = get_objects_in_term ( $blah , 'category' );
2006-02-09 09:26:30 +01:00
$posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode ( $posts_in_ex_cats , ',' ) . ')' ;
2005-04-18 23:47:35 +02:00
}
2004-08-24 03:35:57 +02:00
2007-01-11 05:30:44 +01:00
$join = apply_filters ( 'get_previous_post_join' , $join , $in_same_cat , $excluded_categories );
$where = apply_filters ( 'get_previous_post_where' , " WHERE post_date < ' $current_post_date ' AND post_type = 'post' AND post_status = 'publish' $posts_in_ex_cats_sql " , $in_same_cat , $excluded_categories );
$sort = apply_filters ( 'get_previous_post_sort' , 'ORDER BY post_date DESC LIMIT 1' );
return @ $wpdb -> get_row ( " SELECT ID, post_title FROM $wpdb->posts $join $where $sort " );
2004-08-24 03:35:57 +02:00
}
function get_next_post ( $in_same_cat = false , $excluded_categories = '' ) {
2005-04-18 23:47:35 +02:00
global $post , $wpdb ;
2004-08-24 03:35:57 +02:00
2005-11-15 23:55:24 +01:00
if ( ! is_single () || is_attachment () )
2005-04-18 23:47:35 +02:00
return null ;
2004-08-24 03:35:57 +02:00
2005-04-18 23:47:35 +02:00
$current_post_date = $post -> post_date ;
2006-02-12 08:53:23 +01:00
2005-04-18 23:47:35 +02:00
$join = '' ;
2005-10-18 01:41:28 +02:00
if ( $in_same_cat ) {
2007-05-29 18:52:31 +02:00
$join = " INNER JOIN $wpdb->term_relationships AS tr ON $wpdb->posts .ID = tr.object_id " ;
2007-06-19 02:33:44 +02:00
$cat_array = wp_get_object_terms ( $post -> ID , 'category' , 'fields=tt_ids' );
2007-05-29 18:52:31 +02:00
$join .= ' AND (tr.term_taxonomy_id = ' . intval ( $cat_array [ 0 ]);
2005-10-18 01:41:28 +02:00
for ( $i = 1 ; $i < ( count ( $cat_array )); $i ++ ) {
2007-05-29 18:52:31 +02:00
$join .= ' OR tr.term_taxonomy_id = ' . intval ( $cat_array [ $i ]);
2005-04-18 23:47:35 +02:00
}
2006-11-19 08:56:05 +01:00
$join .= ')' ;
2005-04-18 23:47:35 +02:00
}
2004-08-24 03:35:57 +02:00
2005-04-18 23:47:35 +02:00
$sql_exclude_cats = '' ;
2005-10-18 01:41:28 +02:00
if ( ! empty ( $excluded_categories ) ) {
2006-02-09 09:26:30 +01:00
$blah = explode ( ' and ' , $excluded_categories );
2007-05-29 18:52:31 +02:00
$posts_in_ex_cats = get_objects_in_term ( $blah , 'category' );
2006-02-09 09:26:30 +01:00
$posts_in_ex_cats_sql = 'AND ID NOT IN (' . implode ( $posts_in_ex_cats , ',' ) . ')' ;
2005-04-18 23:47:35 +02:00
}
2004-08-24 03:35:57 +02:00
2007-01-11 05:30:44 +01:00
$join = apply_filters ( 'get_next_post_join' , $join , $in_same_cat , $excluded_categories );
$where = apply_filters ( 'get_next_post_where' , " WHERE post_date > ' $current_post_date ' AND post_type = 'post' AND post_status = 'publish' $posts_in_ex_cats_sql AND ID != $post->ID " , $in_same_cat , $excluded_categories );
$sort = apply_filters ( 'get_next_post_sort' , 'ORDER BY post_date ASC LIMIT 1' );
return @ $wpdb -> get_row ( " SELECT ID, post_title FROM $wpdb->posts $join $where $sort " );
2004-08-24 03:35:57 +02:00
}
2005-10-18 01:41:28 +02:00
function previous_post_link ( $format = '« %link' , $link = '%title' , $in_same_cat = false , $excluded_categories = '' ) {
2005-11-17 02:57:18 +01:00
if ( is_attachment () )
2005-10-20 22:48:32 +02:00
$post = & get_post ( $GLOBALS [ 'post' ] -> post_parent );
2005-11-17 02:57:18 +01:00
else
2005-10-20 22:48:32 +02:00
$post = get_previous_post ( $in_same_cat , $excluded_categories );
2004-08-24 03:35:57 +02:00
2005-10-18 01:41:28 +02:00
if ( ! $post )
return ;
2004-08-24 03:35:57 +02:00
2005-10-18 01:41:28 +02: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 22:48:32 +02:00
$link = $pre . $string . $link . '</a>' ;
2004-08-24 03:35:57 +02:00
2005-10-20 22:48:32 +02:00
$format = str_replace ( '%link' , $link , $format );
2005-11-17 02:57:18 +01:00
2006-11-19 08:56:05 +01:00
echo $format ;
2004-08-24 03:35:57 +02:00
}
function next_post_link ( $format = '%link »' , $link = '%title' , $in_same_cat = false , $excluded_categories = '' ) {
2005-10-18 01:41:28 +02:00
$post = get_next_post ( $in_same_cat , $excluded_categories );
2004-08-24 03:35:57 +02:00
2005-10-18 01:41:28 +02:00
if ( ! $post )
return ;
2004-08-24 03:35:57 +02:00
2005-10-18 01:41:28 +02: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 03:35:57 +02:00
2006-11-19 08:56:05 +01:00
echo $format ;
2004-08-24 03:35:57 +02:00
}
2005-03-28 04:34:16 +02:00
function get_pagenum_link ( $pagenum = 1 ) {
2004-12-03 03:38:11 +01:00
global $wp_rewrite ;
2007-06-14 04:25:30 +02:00
2007-05-12 02:17:34 +02:00
$pagenum = ( int ) $pagenum ;
2007-06-14 04:25:30 +02:00
2007-05-12 02:17:34 +02:00
$request = remove_query_arg ( 'paged' );
2007-06-14 04:25:30 +02:00
2006-08-30 23:46:31 +02:00
$home_root = parse_url ( get_option ( 'home' ));
2005-03-28 04:34:16 +02:00
$home_root = $home_root [ 'path' ];
2007-05-12 02:17:34 +02:00
$home_root = preg_quote ( trailingslashit ( $home_root ), '|' );
2007-06-14 04:25:30 +02:00
2007-05-16 04:47:24 +02:00
$request = preg_replace ( '|^' . $home_root . '|' , '' , $request );
$request = preg_replace ( '|^/+|' , '' , $request );
2007-06-14 04:25:30 +02:00
2007-05-28 20:34:06 +02:00
if ( ! $wp_rewrite -> using_permalinks () || is_admin () ) {
2007-05-12 02:17:34 +02:00
$base = trailingslashit ( get_bloginfo ( 'home' ) );
2007-05-28 20:34:06 +02:00
2007-05-12 02:17:34 +02:00
if ( $pagenum > 1 ) {
2007-05-16 04:47:24 +02:00
$result = add_query_arg ( 'paged' , $pagenum , $base . $request );
2007-05-12 02:17:34 +02:00
} else {
$result = $base . $request ;
}
2005-03-28 04:34:16 +02:00
} else {
2007-05-12 02:17:34 +02:00
$qs_regex = '|\?.*?$|' ;
preg_match ( $qs_regex , $request , $qs_match );
2007-06-14 04:25:30 +02:00
2007-05-12 02:17:34 +02:00
if ( $qs_match [ 0 ] ) {
$query_string = $qs_match [ 0 ];
$request = preg_replace ( $qs_regex , '' , $request );
2005-03-28 04:34:16 +02:00
} else {
2007-05-12 02:17:34 +02:00
$query_string = '' ;
2005-03-28 04:34:16 +02:00
}
2007-06-14 04:25:30 +02:00
2007-05-31 05:36:40 +02:00
$request = preg_replace ( '|page/(.+)/?$|' , '' , $request );
2007-06-14 04:25:30 +02:00
2007-05-16 04:47:24 +02:00
$base = trailingslashit ( get_bloginfo ( 'url' ) );
2007-06-14 04:25:30 +02:00
2007-05-12 02:17:34 +02:00
if ( $wp_rewrite -> using_index_permalinks () && $pagenum > 1 ) {
$base .= 'index.php/' ;
}
2007-06-14 04:25:30 +02:00
2007-05-16 04:47:24 +02:00
if ( $pagenum > 1 ) {
$request = ( ( ! empty ( $request ) ) ? trailingslashit ( $request ) : $request ) . user_trailingslashit ( 'page/' . $pagenum , 'paged' );
}
2007-06-14 04:25:30 +02:00
2007-05-12 02:17:34 +02:00
$result = $base . $request . $query_string ;
2005-03-28 04:34:16 +02:00
}
2007-06-14 04:25:30 +02:00
2007-05-12 02:17:34 +02:00
return $result ;
2004-06-02 09:44:43 +02:00
}
2007-03-16 09:04:24 +01:00
function get_next_posts_page_link ( $max_page = 0 ) {
2005-10-18 01:41:28 +02:00
global $paged , $pagenow ;
if ( ! is_single () ) {
if ( ! $paged )
$paged = 1 ;
$nextpage = intval ( $paged ) + 1 ;
if ( ! $max_page || $max_page >= $nextpage )
2007-03-16 09:04:24 +01:00
return get_pagenum_link ( $nextpage );
2005-10-18 01:41:28 +02:00
}
2004-06-02 09:44:43 +02:00
}
2007-03-16 09:04:24 +01:00
function next_posts ( $max_page = 0 ) {
2007-03-17 09:46:59 +01:00
echo clean_url ( get_next_posts_page_link ( $max_page ));
2007-03-16 09:04:24 +01:00
}
2004-06-02 09:44:43 +02:00
function next_posts_link ( $label = 'Next Page »' , $max_page = 0 ) {
2006-11-08 22:22:35 +01:00
global $paged , $wpdb , $wp_query ;
2005-10-18 01:41:28 +02:00
if ( ! $max_page ) {
2006-11-08 22:22:35 +01:00
$max_page = $wp_query -> max_num_pages ;
2005-10-18 01:41:28 +02: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 09:44:43 +02:00
}
2007-03-16 09:04:24 +01:00
function get_previous_posts_page_link () {
2005-10-18 01:41:28 +02:00
global $paged , $pagenow ;
2004-06-02 09:44:43 +02:00
2005-10-18 01:41:28 +02:00
if ( ! is_single () ) {
$nextpage = intval ( $paged ) - 1 ;
if ( $nextpage < 1 )
$nextpage = 1 ;
2007-03-16 09:04:24 +01:00
return get_pagenum_link ( $nextpage );
2005-10-18 01:41:28 +02:00
}
2004-06-02 09:44:43 +02:00
}
2007-03-16 09:04:24 +01:00
function previous_posts () {
2007-03-17 09:46:59 +01:00
echo clean_url ( get_previous_posts_page_link ());
2007-03-16 09:04:24 +01:00
}
2005-10-18 01:41:28 +02:00
2004-06-02 09:44:43 +02:00
function previous_posts_link ( $label = '« Previous Page' ) {
2005-10-18 01:41:28 +02: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 09:44:43 +02:00
}
function posts_nav_link ( $sep = ' — ' , $prelabel = '« Previous Page' , $nxtlabel = 'Next Page »' ) {
2006-11-08 22:22:35 +01:00
global $wp_query ;
if ( ! is_singular () ) {
$max_num_pages = $wp_query -> max_num_pages ;
2006-03-20 20:43:57 +01:00
$paged = get_query_var ( 'paged' );
2006-11-19 08:56:05 +01:00
2006-03-20 20:43:57 +01:00
//only have sep if there's both prev and next results
if ( $paged < 2 || $paged >= $max_num_pages ) {
$sep = '' ;
2004-10-12 15:04:35 +02:00
}
2004-08-17 00:50:06 +02:00
2005-10-18 01:41:28 +02:00
if ( $max_num_pages > 1 ) {
previous_posts_link ( $prelabel );
echo preg_replace ( '/&([^#])(?![a-z]{1,8};)/' , '&$1' , $sep );
2006-03-20 20:43:57 +01:00
next_posts_link ( $nxtlabel );
2005-10-18 01:41:28 +02:00
}
}
2004-06-02 09:44:43 +02:00
}
2005-10-20 22:48:32 +02:00
?>