the_category将div拉出正常流量

问题描述 投票:0回答:1

试图使用<?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的section div似乎被拉出链接(输出有链接代码以外的部分。没有类别代码它完美地工作。

php html wordpress
1个回答
2
投票

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

在使用the_category()时,您将显示指向帖子所属类别或类别的链接,因此您还将<a>标记放在另一个<a>标记内。

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

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

检查您的格式,一切都将按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.