我们知道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,"……"); ?>