<?php

//页面导航

function pagenavi($query_string){

global $posts_per_page, $paged;

$my_query = new WP_Query($query_string ."&posts_per_page=-1");

$total_posts = $my_query->post_count;

if(empty($paged))$paged = 1;

$prev = $paged - 1;							

$next = $paged + 1;	

$range = 4; // 修改数字,可以显示更多的分页链接

$showitems = ($range * 2)+1;

$pages = ceil($total_posts/$posts_per_page);

if(1 != $pages){

	echo "<div class='pagination'><ul>";

	echo ($paged > 1 )? "<li class='prev previous_page'><a href='".get_pagenum_link($prev)."' title='跳转到上一页'>&#8592; 上一页</a></li>":"<li class='prev previous_page disabled'><a href='".get_pagenum_link($prev)."' title='跳转到上一页'>&#8592; 上一页</a></li>";		

	for ($i=1; $i <= $pages; $i++){

	if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){

	echo ($paged == $i)? "<li class='active'><a href='".get_pagenum_link($i)."' >".$i."</a></li>":"<li><a href='".get_pagenum_link($i)."' >".$i."</a></li>"; 

	}

	}

	echo ($paged < $pages) ? "<li class='next next_page'><a href='".get_pagenum_link($next)."' rel='next'>下一页 &#8594;</a>" :"";

	echo "</ul></div>\n";

	}

}

 //手动随机文章

function matt_random_redirect() {

	global $wpdb;	

	$query = "SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_password = '' AND 	post_status = 'publish' ORDER BY RAND() LIMIT 1";

	if ( isset( $_GET['random_cat_id'] ) ) {

		$random_cat_id = (int) $_GET['random_cat_id'];

		$query = "SELECT DISTINCT ID FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS tr ON (p.ID = tr.object_id AND tr.term_taxonomy_id = $random_cat_id) INNER JOIN  $wpdb->term_taxonomy AS tt ON(tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category') WHERE post_type = 'post' AND post_password = '' AND 	post_status = 'publish' ORDER BY RAND() LIMIT 1";

	}

	if ( isset( $_GET['random_post_type'] ) ) {

		$post_type = preg_replace( '|[^a-z]|i', '', $_GET['random_post_type'] );

		$query = "SELECT ID FROM $wpdb->posts WHERE post_type = '$post_type' AND post_password = '' AND 	post_status = 'publish' ORDER BY RAND() LIMIT 1";

	}

	$random_id = $wpdb->get_var( $query );

	wp_redirect( get_permalink( $random_id ) );

	exit;

}

if ( isset( $_GET['random'] ) )

	add_action( 'template_redirect', 'matt_random_redirect' );

//标题截断

function cut_str($src_str,$cut_length) 

{ 

$return_str=''; 

$i=0; 

$n=0; 

$str_length=strlen($src_str); 

while (($n<$cut_length) && ($i<=$str_length)) 

{ 

$tmp_str=substr($src_str,$i,1); 

$ascnum=ord($tmp_str); 

if ($ascnum>=224) 

{ 

$return_str=$return_str.substr($src_str,$i,3); 

$i=$i+3; 

$n=$n+2; 

} 

elseif ($ascnum>=192) 

{ 

$return_str=$return_str.substr($src_str,$i,2); 

$i=$i+2; 

$n=$n+2; 

} 

elseif ($ascnum>=65 && $ascnum<=90) 

{ 

$return_str=$return_str.substr($src_str,$i,1); 

$i=$i+1; 

$n=$n+2; 

} 

else 

{ 

$return_str=$return_str.substr($src_str,$i,1); 

$i=$i+1; 

$n=$n+1; 

} 

} 

if ($i<$str_length) 

{ 

$return_str = $return_str . ''; 

} 

if (get_post_status() == 'private') 

{ 

$return_str = $return_str . '（private）'; 

} 

return $return_str; 

}

//////////TIME

/* Time Ago by Fanr. */

function time_ago( $type = 'commennt', $day = 30 ) {

$d = $type == 'post' ? 'get_post_time' : 'get_comment_time';

$timediff = time() - $d('U');

 

/*Show Time Diff*/

if ($timediff <= 60*60*24*$day){

echo  human_time_diff($d('U'), strtotime(current_time('mysql', 0))), '前';

}

 

/*Show Datetime*/

if ($timediff > 60*60*24*$day){

echo  date('Y/m/d',get_comment_date('U')), ' ', get_comment_time('H:i');

};

}

//Commentlist外观

function blog_comment($comment, $args, $depth) {



   $GLOBALS['comment'] = $comment; ?>				  

<div class="reply xtheme" id="comment-<?php comment_ID() ?>">

    <div class="pull-left face"><a target="_blank" href="#comment-<?php comment_ID() ?>"><?php echo get_avatar($comment->comment_author_email, 48); ?></a></div>

    <div class="infos">

      <div class="info">

        <span class="name">

          <?php comment_author_link();?>

        </span>

        <span class="opts">

          <?php comment_reply_link(array_merge( $args, array('reply_text' => '回复','depth' => $depth, 'max_depth' => $args['max_depth']))) ?>

        </span>

      </div>

      <div class="body sep-10">

        <?php if ($comment->comment_approved == '0') : ?><?php ('评论将在审核后显示...') ;else: comment_text();?><?php endif; ?>

        <em class="muted pull-right"><?php echo time_ago(); ?></em>

      </div>

      <span class="opts">

      </span>

    </div>

<?php

}



function blog_end_comment() {

		echo '</div>';

}









function getFirstImage($postId) {

 $args = array(

  'numberposts' => 1,

  'order'=> 'ASC',

  'post_mime_type' => 'image',

  'post_parent' => $postId,

  'post_status' => null,

  'post_type' => 'attachment'

 );

 $attachments = get_children($args);

 

 // 如果没有上传图片, 返回空字符串

 if(!$attachments) {

  return '';

 }

 

 // 获取缩略图中的第一个图片, 并组装成 HTML 节点返回

 $image = array_pop($attachments);

 $imageSrc = wp_get_attachment_image_src($image->ID, 'thumbnail');

 $imageUrl = $imageSrc[0];

 $html = '<img src="' . $imageUrl . '" alt="' . the_title('', '', false) . '" />';

 return $html;

}









?>