【问题标题】:the_category pulling div out of normal flowthe_category 将 div 拉出正常流程
【发布时间】:2017-12-28 07:52:04
【问题描述】:

尝试使用 <?php the_category( ' ' ); ?> 从我的 wordpress 帖子中放入我的类别。

在循环html内

<a href='<?php the_permalink(); ?>'>
   <section id="post-<?php the_ID(); ?>" class="..">

   <div> 

   <?php the_category( ' ' ); ?>
   <?php the_title( '<h1>', '</h1>' ); ?>

   </div>
</section>
</a>

问题是类别 php 的部分 div 似乎被拉出链接(输出有部分外部链接代码。 没有类别代码它可以完美地工作。

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    您的 HTML 存在问题:您有一个带有内部块类型元素(如 &lt;div&gt;&lt;section&gt;)的 &lt;a&gt; 标记(内联元素)。 看看this page,以便正确理解内联元素和块元素之间的区别。

    在使用the_category() 时,您将显示帖子所属类别的链接,因此您还将&lt;a&gt; 标签放在另一个&lt;a&gt; 标签内。

    因为您只想显示类别的名称,您可以使用以下代码

    foreach((get_the_category()) as $category){
        echo $category->name."<br>";
    }
    

    检查您的格式,一切都会按预期进行。

    【讨论】:

    • 哦,我明白了,所以我需要从类别中删除 a 标签,我只想要文本
    • 更新答案,请查收。
    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 2018-07-11
    • 1970-01-01
    • 2017-01-06
    • 2021-08-24
    • 1970-01-01
    • 2014-10-07
    • 2018-07-29
    相关资源
    最近更新 更多