2017-06-06 12 views
0

私のループを動作していないカウント、次のようになりますWordPressの抜粋文字が

<!-- loop for the posts here --> 
<?php 
    $args = array(
     'post_type' => 'post', 
     'posts_per_page' => 3, 
     'category_name' => 'news' 
    ); 
    $query = new WP_Query($args); 
    while($query->have_posts()) : $query->the_post(); 
?> 
    <div class="news_box_content"> 
     <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5> 
     <figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></figure> 
     <?php if($post->post_excerpt) { ?> 
      <p><?php echo get_the_excerpt(); ?></p> 
      <a href="<?php the_permalink(); ?>">Read more...</a> 
     <?php } else { 
      the_content('Read More'); 
     } ?> 
    </div> 
<?php endwhile; wp_reset_postdata(); ?> 

私が抜粋長をカウントする機能を使用しますが、それは動作しません。

function custom_excerpt_length(){ 
    return 10; 
} 
add_filter('excerpt_length', 'custom_excerpt_length'); 

どのように抜粋の文字数を制限できますか?

+0

あなたのカスタム抜粋関数、つまりcustom_excerpt_length()を呼び出す必要があります。 get_the_excerpt()の代わりに。 – Samyappa

+0

あなたは正しいですよ。 –

答えて

0

、このいずれかを試してみてください

echo substr(get_the_excerpt(), 0,10); 
+0

これは抜粋のためには動作しますが、内容は動作しません。 これは動作します:<?php echo substr(get_the_excerpt()、0,300); ?> これはうまくいきません:echo substr(the_content( 'Read More')、0,150); –

0

余分なフィルタの必要はありません。私はいつもこの1つを使用します

<?php 
     $content = get_the_content(); 
     $content = strip_tags($content); 

     $dots = strlen(substr($content,0,50)) < 50 ? " " : "..."; 
     echo substr($content,0,50) . $dots; 
     ?> 
関連する問題