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

日付/ソースが常に同じ長さである場合 (これはおそらくありそうもないことです)、substr()onを使用し$excerptて 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 ) を使用してこれを達成することもできますが、使用している形式がわからない正規表現を使用することはできません。

于 2013-04-03T04:07:05.777 に答える