我們知道wordpress調用摘要內容用<?php the_excerpt(); ?>就可以,但是它會自動添加一個p標簽,例如<p>這里是description</p>,如果我們不想要這個p怎么處理呢?其實很簡單,用下面的代碼就能實現
<?php echo get_the_excerpt(); ?>
同樣的道理,如果想調用內容不帶p標簽,可以用
<?php echo get_the_content(); ?>
另外一種方法去掉p標簽是在你的主題function.php中定義
remove_filter ( 'the_excerpt' , 'wpautop' ); remove_filter ( 'the_content' , 'wpautop' );
你一定會喜歡這篇文章
或者用下面的方法截取文章多個詞
<?php $excerpt = wp_trim_words(get_post_field('post_content', $post_id), 16); echo $excerpt; ?>
或
<?php $trimexcerpt = get_the_content(); $shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 18, $more = '… ' ); echo $shortexcerpt; ?>
或者
<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); ?>