显示内容的一小部分 - wordpress

时间:2013-08-29 17:03:40

标签: php wordpress loops

这是我的循环代码。

<?php $posts = get_posts ("orderby=date&numberposts=1"); ?> 
            <?php if ($posts) : ?>
            <?php foreach ($posts as $post) : setup_postdata ($post); ?>
            <a href="<?php the_permalink() ?>" d="center-news">
                <div class="center-news-image">
                    <img src="<?php echo get_template_directory_uri(); ?>/images/example.jpg" />
                </div>
                <div class="center-news-text">
                    <div class="center-news-title"><b><?php the_title(); ?></b></div>
                    <div class="center-news-info">
                    <?php the_content(); ?>
                    </div>
                </div>
            </a>
            <?php endforeach; ?>
            <?php endif; ?>

如果帖子内容很长,则会破坏html。我需要显示一小部分内容。有可能吗?

2 个答案:

答案 0 :(得分:3)

你可以试试这段代码

<?php echo apply_filters('the_content', substr(get_the_content(), 0, 200) ); ?>

答案 1 :(得分:2)

使用

<?php the_excerpt(); ?>

而不是

<?php the_content(); ?>

显示帖子内容的前55个单词

请参阅codex the_excerpt了解更多详情