我在我的定制网站中有WordPress博客,我正在根据如下所示的标签将WordPress博客中的一些帖子显示到我的网站中
require('../news/wp-blog-header.php');
$query = new WP_Query('tag=Dalaman');
if ($query->have_posts()):
while ($query->have_posts()) : $query->the_post();
?>
<h3> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p><?php the_content();?></p>
<?php
endwhile;
endif;
the_content
根据WP_Query
显示WordPress数据库中的10 posts
问题:我想显示帖子的某些部分,假设有 55 个 chracters帖子,在我的数据库中默认没有excerpt
,我不想使用the_exerpt()
因为它会去除 html 标签,并且我的帖子在每个帖子的开头都包含<img>
我尝试了很多东西,但都徒劳无功,我也使用了 PHP 的 substr()
函数,但在这种情况下它不起作用。
那么我怎样才能显示帖子的一部分以及图像呢?
非常感谢。
亲切问候!
你可以像下面这样做,
$limit = 55;
$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(']]>', ']]>', $content);
echo $content;
> http://codex.wordpress.org/Function_Reference/the_content
我建议您按照文章所说的进行操作,并在断点处插入<!--more-->
- 这比去除任意数量的字符更安全,因为您可能会以这种方式破坏 html 标记。
如果您不担心这一点,那么而不是
<?php the_content(); ?>
做
<?php
$content = get_the_content(); //get the content as a string
$content = substr($content, 0, 55); //cut the first 55 characters
echo $content; //display it as usual
?>