2013-04-02 52 views
0

我想刮掉我们在文章中的几个字符或文本片段。我们有我们帖子中的故事的日期和来源,但我不喜欢那些包含在我们的摘录中的内容,而且格式化人员坚持认为它必须保留在帖子的顶部。定义Wordpress摘录的开头

我该如何去确切指定我要在帖子中开始的摘录?我可以从<p>标签开始,还是可以在开始之前设置要跳过的字符数?

任何帮助将不胜感激。这里是我的代码至今:

<phpcode> 
<?php $my_query = new WP_Query('category_name=science&showposts=5'); ?> 
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 
<div id="postlist_container"> 
<h4 class="und"></h4> 
<?php get_the_image(array('image_scan' => true , 'image_class' => 'small_image_left','width' => 80 , 'height' => 80)); ?><div class="post_desc"><date><?php the_time('M j, Y') ?></date> &middot; <a href="<?php the_permalink() ?>"> 
<?php the_title(); ?></a> <br /><br /><?php the_excerpt_max_charlength(250); ?> 
</div> 
</div> 
<div class="clear"></div> 
<?php endwhile; ?> 
<?php 

function the_excerpt_max_charlength($charlength) { 
    $excerpt = get_the_excerpt(); 
    $charlength++; 

    if (mb_strlen($excerpt) > $charlength) { 
     $subex = mb_substr($excerpt, 0, $charlength - 5); 
     $exwords = explode(' ', $subex); 
     $excut = - (mb_strlen($exwords[ count($exwords) - 1 ])); 
     if ($excut < 0) { 
      echo mb_substr($subex, 0, $excut); 
     } else { 
      echo $subex; 
     } 
     echo '[...]'; 
    } else { 
     echo $excerpt; 
    } 
} 
?> 
</phpcode> 

回答

1

如果日期/来源都是一样的长度(这可能是不可能的),那么你可以使用上$excerptsubstr()删除字符X号:

// assume we want to remove the first 10 chars 
$chars_to_skip = 10; 
// get the full excerpt 
$excerpt = get_the_excerpt(); 
// check the length 
if (strlen($excerpt) > $chars_to_skip){ 
    // remove chars from the beginning of the excerpt 
    $excerpt = substr($excerpt, $chars_to_skip); 
} 

更有可能的是,您需要执行正则表达式搜索并替换以删除任何模式匹配,即使源或日期文本的确切长度在发布后有所不同。您可以使用preg_replace()api info)来完成此操作,但我无法帮助正则表达式不知道您使用的格式。