【问题标题】:How to prevent PHP Code outputting the content in bold?如何防止PHP代码以粗体输出内容?
【发布时间】:2019-09-17 07:33:58
【问题描述】:

在我的 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>

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

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

因为当我这样做时:

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

我在前端看不到任何包含内容的&lt;strong&gt;&lt;/strong&gt; 标记。

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

任何帮助将不胜感激!

【问题讨论】:

  • &lt;strong&gt;&lt;/strong&gt; 标签真的会在 dom 中输出吗?你确定不只是 CSS 吗?
  • @BrettGregson 我已经更新了我的问题。您可能会看到 &lt;strong&gt;&lt;/strong&gt; 是如何输出的,这也不是 CSS 的原因。
  • var_dump($content_arr)时有什么奇怪的地方吗?
  • 我认为它必须是加载后转换DOM的javascript
  • 据我所知,当短代码没有正确关闭时会发生这种情况。您是否尝试将状态设置为草稿。如果您将每个帖子切换到草稿/发布,您可以看到是什么帖子导致了这种情况。

标签: php wordpress output customization


【解决方案1】:

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

感谢@Jayr 的快速回复!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-28
    • 2011-11-06
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 2011-10-06
    • 1970-01-01
    相关资源
    最近更新 更多