2013-04-02 54 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)來完成此操作,但我無法幫助正則表達式不知道您使用的格式。