首页 > 解决方案 > 如何防止PHP代码以粗体输出内容?

问题描述

在我的 WordPress 网站中,我创建了一个类别模板文件(存档文件),用于显示来自特定类别的所有帖子。代码工作正常,我得到了我需要的确切结果。但是,我的代码存在一些问题(我无法调查)。

模板文件输出 5 个帖子,前 2 个帖子的内容正是它们的内容。但是,剩下的3篇帖子的内容却以粗体显示,很奇怪。

这是我的代码:

<section class="video_categories" id="content" style="float:left;">
    <?php
        $args = array(
            'post_type' => 'post',
            'category_name' => 'sponsor-spotlight',
            'posts_per_page' => -1
        );

        $obituary_query = new WP_Query($args);
        while ($obituary_query->have_posts()) : $obituary_query->the_post();
        $readMore = '<a href="' . get_the_permalink() . '"> Keep On Reading...</a>';
        echo '<div class="advert-div" style="padding-bottom: 15px;">'; ?>
        <h2 class="ad-title"> <?php echo '<a style="color: #333333;font-weight: 600;" href="' . get_the_permalink() . '">';?> <?php the_title(); echo '</a>'; ?>
        </h2>
    <?php
        $post = get_post( $post->ID );
        $content_arr = get_extended($post->post_content);
        echo apply_filters('the_content', $content_arr['main']);
        echo $readMore;
        echo '</div><hr>';
        endwhile;
        wp_reset_postdata();
    ?>
</section>

根据我的调查,问题在于以下代码行,因为它会导致输出<strong> </strong>包含内容的标签:

$content_arr = get_extended($post->post_content);
echo apply_filters('the_content', $content_arr['main']);

因为当我这样做时:

 echo apply_filters('the_content', $post->post_content);

<strong></strong>我在前端看不到包含内容的任何标签。

这是我上面使用的代码的奇怪输出。

在此处输入图像描述

任何帮助将不胜感激!

标签: phpwordpressoutputcustomization

解决方案


尽管代码没有理由<strong></strong>在前端输出标签,所以我通过更改以粗体显示的每个帖子的状态来练习@Jayr 建议。问题在于这些帖子的内容。我只是重新创建了具有相同内容的新帖子,我的问题得到了解决。

感谢@Jayr 的快速回复!


推荐阅读