前几天帮朋友做WordPress,朋友用的是Polished主题,分别遇上了如下两个问题:

1、即使增加图片,首页的摘要也没有图片

2、摘要部分为“…”

首先来说说第一个的解决办法

进入外观——Polished theme options——general setting

找到Grab the first post image,改为this is enable即可【或者在文章时注意右下角的“特色图片”,手动添加特色图片也行,稍微麻烦点】

第二个我百思不得其解,在主题设置里翻了半天愣是没见,只能从代码上下手了

首先看到featured.php的代码,发现输出摘要部分是这样的:

<div class="slide">
							<h1><?php echo $arr[$i]["title"]; ?></h1>
							<br class="clear" />
							<?php print_thumbnail($arr[$i]["thumb"], $arr[$i]["use_timthumb"], $arr[$i]["fulltitle"] , $width, $height,'featured_img'); ?>
							<?php echo $arr[$i]["excerpt"];?>
							
							
							<span class="readmore_g"><a href="<?php echo $arr[$i]["permalink"]; ?>"> <?php _e('Read More','Polished') ?></a></span>
						</div>
					<?php }; ?>
				</div>

 

<?php echo $arr[$i]["excerpt"];?>

看到这句,然后往前翻

<?php	
	$ids = array();
	$arr = array();
	$i=1;
	
	$width = 177;
	$height = 177;
	
	$width2 = 35;
	$height2 = 35;
		
	$featured_cat = get_option('polished_feat_cat');
	$featured_num = get_option('polished_featured_num');
	
	if (get_option('polished_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_catId($featured_cat));
	else { 
		global $pages_number;
		if (get_option('polished_feat_pages') <> '') $featured_num = $pages_number - count(get_option('polished_feat_pages'));
		else $featured_num = $pages_number;
		
		query_posts(array
						('post_type' => 'page',
						'orderby' => 'menu_order',
						'order' => 'ASC',
						'post__not_in' => get_option('polished_feat_pages'),
						'showposts' => $featured_num
					));
	};
	
	while (have_posts()) : the_post();
			
		$arr[$i]["title"] = truncate_title(22,false);
		$arr[$i]["title2"] = truncate_title(25,false);
		$arr[$i]["fulltitle"] = truncate_title(250,false);
		$arr[$i]["excerpt"] = truncate_post(350,false);//注意这里,调用truncate_post函数

		//$arr[$i]["excerpt"] = the_content('');
		$arr[$i]["permalink"] = get_permalink();
		$arr[$i]["postinfo"] = __("Posted by", "Polished")." ". get_the_author_meta('display_name') . __(' on ','Polished') . get_the_time(get_option('polished_date_format'));
		
		$arr[$i]["thumbnail"] = get_thumbnail($width,$height,'featured_img',$arr[$i]["fulltitle"]);
		$arr[$i]["thumb"] = $arr[$i]["thumbnail"]["thumb"];
		$arr[$i]["use_timthumb"] = $arr[$i]["thumbnail"]["use_timthumb"];
		
		$arr[$i]["thumbnail2"] = get_thumbnail($width2,$height2,'',$arr[$i]["fulltitle"]);
		$arr[$i]["thumb2"] = $arr[$i]["thumbnail2"]["thumb"];

		$i++;
		$ids[]= $post->ID;
	endwhile; wp_reset_query();	?>
	

然后又继续找,找到custom_functions.php,开头就是这个函数【放在这里难道是故意让人首先看到?】

function truncate_post($amount,$echo=true) {
	global $post;
	
	$truncate = $post->post_content;
	if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...';//如果文章长度大于之前传进来的$amount(350字),则在文章后放上“...”

	$truncate = apply_filters('the_content', $truncate);//启用本函数进行过滤
	$truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate);//将script无视(去掉)
	$truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate);//将style无视(去掉)【想的挺周全】
	$truncate = strip_tags($truncate);//去除一些特殊标签
	if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));//这句是重点,如果有输出“...”【意思是文章字数大于$amount】。则截取从开头到$amount[第350字]之间的最后一个空格【空格啊,注意是空格!】
	else $truncate = substr($truncate, 0, $amount);//如果没有“...”则直接输出

	if ($echo) echo $truncate,$echo_out;//如果要求回显【默认】,否则以字符串输出
	else return ($truncate . $echo_out);
}

问题就出在

 

if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));

里了,因为截取的是空格【西欧语言里不可能没有空格】,而中文字里不一定会有空格,所以就杯具了

中文字里不可能没有什么?逗号!,直接将这句改成:

 

if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ','));

即可

done~