0

我正在尝试删除我们帖子中的一些字符或文本。我们有我们帖子中的故事拍摄的日期和来源,但我不希望将其包含在我们的摘录中,并且格式人们坚持认为它必须保留在帖子的顶部。

我将如何准确地指定我希望摘录在帖子中从哪里开始?我可以让它从<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>
4

1 回答 1

1

If the date/source are always the same length (which is probably unlikely), then you could use substr() on $excerpt to remove X number of characters:

// 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 );
}

What's more likely is that you would need to do a regex search and replace to remove whatever the pattern matches even when the exact length of the source or date text differs post to post. You could use preg_replace() (api info) to accomplish this, but I can't help with the regular expression not knowing the format you're using.

于 2013-04-03T04:07:05.777 回答