具有一定摘录长度的帖子数量

时间:2016-04-29 作者:Cdunn

可能需要一些帮助。。。使用此代码从特定类别调用自定义数量的帖子,并且只想显示前15个字符。然而,它在每篇文章上显示相同的摘录,而不是在每篇文章上显示唯一的摘录。有人能帮我解决这个问题吗?

下面的代码导致相同的摘录被添加到帖子中,即使显示了正确的标题和缩略图!

我是个新手,如果你能解释一下,我将非常感谢!

<?php $posts = get_posts(\'category=3&orderby=date&numberposts=3\'); foreach($posts as $post) { ?>
<a href="<?php the_permalink() ?>" target="_parent">
<div class="aligncenter" style="display: block;">
<?php the_post_thumbnail( \'thumbnail\', array( \'class\' => \'img-responsive\' ) ); ?>
</div>
<h3><?php the_title(); ?></h3></a>
<?php while (have_posts()) : the_post(); ?>
<?php echo excerpt(15); ?>
<?php endwhile; ?>
<a href="<?php the_permalink() ?>" class="blue"" target="_parent">
Read More</a>
</div>
<?php } ?>
这是我的功能。php

function excerpt($limit) {
$excerpt = explode(\' \', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).\'...\';
} else {
$excerpt = implode(" ",$excerpt);
}   
$excerpt = preg_replace(\'`\\[[^\\]]*\\]`\',\'\',$excerpt);
return $excerpt;
}


function content($limit) {
$content = explode(\' \', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).\'...\';
} else {
$content = implode(" ",$content);
}   
$content = preg_replace(\'/\\[.+\\]/\',\'\', $content);
$content = apply_filters(\'the_content\', $content); 
$content = str_replace(\']]>\', \']]&gt;\', $content);
return $content;

2 个回复
最合适的回答,由SO网友:Luis Sanz 整理而成

如果您只是尝试在主题的特定部分中回显摘录,那么只需在循环中使用以下代码即可:

<?php 

    $excerpt = get_the_excerpt();
    echo substr( $excerpt, 0, 15 ) . \'&hellip;\';

?>
如果要全局筛选摘录(在存档、博客页面等中)以匹配一个或多个给定类别的任何帖子部分,可以在主题的functions.php 文件:

<?php

    function wpse_225203_category_based_trim_excerpt( $excerpt ) {

        //Return if there is not a except to work with
        if ( \'\' === $excerpt ) :
            return $excerpt;
        endif;

        //Get the post\'s categories
        $categories = get_the_category();

        //Get the post\'s categories ID
        $categories_id = wp_list_pluck( $categories, \'term_id\' );

        //Set the post\'s categories ID we want to match
        $categories_to_trim_ids = array( 1, 3 ); //Modify to match your ids

        //Match the wanted categories ids and the post\'s categories ids
        if ( ! array_intersect( $categories_to_trim_ids, $categories_id ) ) :
            return $excerpt;
        endif;

        //$excerpt = wp_trim_words( $excerpt, 15 ); //Trim the excerpt to the first 15 words
        $excerpt = substr( $excerpt, 0, 15 ) . \'&hellip;\'; //Trim the excerpt to the first 15 characters

        return $excerpt;

    }

    add_filter( \'get_the_excerpt\', \'wpse_225203_category_based_trim_excerpt\' );

?>

SO网友:Owais Alam

在函数中输入以下代码。php

 function excerpt($limit) {
          $excerpt = explode(\' \', get_the_excerpt(), $limit);
          if (count($excerpt)>=$limit) {
            array_pop($excerpt);
            $excerpt = implode(" ",$excerpt).\'...\';
          } else {
            $excerpt = implode(" ",$excerpt);
          } 
          $excerpt = preg_replace(\'`\\[[^\\]]*\\]`\',\'\',$excerpt);
          return $excerpt;
        }

        function content($limit) {
          $content = explode(\' \', get_the_content(), $limit);
          if (count($content)>=$limit) {
            array_pop($content);
            $content = implode(" ",$content).\'...\';
          } else {
            $content = implode(" ",$content);
          } 
          $content = preg_replace(\'/\\[.+\\]/\',\'\', $content);
          $content = apply_filters(\'the_content\', $content); 
          $content = str_replace(\']]>\', \']]&gt;\', $content);
          return $content;
        }
然后在模板代码中使用。。

<?php echo excerpt(25); ?>
或者在你的模板上试试这个

<?php 

    $excerpt = get_the_excerpt();
    echo substr( $excerpt, 0, 25);

?>

相关推荐

Strip div From Excerpt

我在特定页面中有\\u extract(),我需要去掉一个div并保留文本,但只保留在该页面的摘录中。我相信我需要创建一个带有自定义摘录的函数,但我不知道该怎么做。测试了我在这里找到的几种解决方案,但没有任何效果。