我想在主页上显示WordPress的最新帖子。我使用WordPress api和函数来显示这一点,但之后get_the_content()
删除和转义所有的服务标签和我的帖子不正确显示,如编辑器标签。
这个函数转义HTML标签:
<ul style="list-style: none;margin:5px" class="latest-posts">
<!-- LOOP START -->
<?php $the_query = new WP_Query( 'showposts=3' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<!-- THIS DISPLAYS THE POST THUMBNAIL, The array allows the image to has a custom size but is always kept proportional -->
<li> <?php the_post_thumbnail( array(100,100) );?></li>
<!-- THIS DISPLAYS THE POST TITLE AS A LINK TO THE MAIN POST -->
<li>
<a href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a>
</li>
<!-- THIS DISPLAYS THE EXCERPT OF THE POST -->
<li>
<?php echo mb_substr(get_the_content(), 0, 1200).' ...'; ?>
</li>
<!-- READ MORE LINK -->
<li><a href="<?php the_permalink() ?>">more ...</a></li>
<?php endwhile;?>
<!-- LOOP FINNISH -->
</ul>
基本上有三种输出文章内容摘录的可能性。
-
带
the excerpt()
功能,输出文章编辑器摘录字段的内容,如果没有填充,将显示文章内容的前55个字(使用excerpt_length
过滤器控制摘录的长度) -
使用
<!--more-->
快速标签,the_content()
标签将只显示<!--more-->
快速标签的摘录。 -
手动从帖子内容中剥离HTML并显示自定义摘录。一种方法是使用
wp_filter_nohtml_kses()
函数:echo mb_substr( wp_filter_nohtml_kses( get_the_content() ), 0, 1200 );
上述所有方法都将从帖子内容中剥离HTML。在文章编辑器中使用摘录字段包含HTML, WordPress将保留你在文章编辑器中包含的HTML。也有一些插件,比如Advanced摘录,可以从内容中创建摘录并保存html,但我不相信有一个万无一无的方法来做到这一点。