Php 在while循环中生成多个空白锚定标记
我有以下循环:Php 在while循环中生成多个空白锚定标记,php,wordpress,while-loop,Php,Wordpress,While Loop,我有以下循环: <?php while ( have_posts() ) : the_post(); ?> <a href="<?php the_permalink(); ?>"> <div class="card"> <div class="card__content"> <h3 class="title"><?php echo mb_s
<?php
while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>">
<div class="card">
<div class="card__content">
<h3 class="title"><?php echo mb_strimwidth(get_the_title(), 0, 50, '...');?></h3>
<p><?php echo wp_trim_words( get_the_content(), 30, '...' ); ?></p>
</div>
</div>
</a>
<?php endwhile;
?>
呈现的标记如下所示:
<div class="card ">
<a href="#"></a>
<a href="#">
<div class="card__content ">
<h3 class="title">title</h3>
<p>content</p>
</div>
</a>
</div>
<a href=#"></a>
为什么会这样?我所期望的是一个包装
的单锚。卡您可以使用此代码删除空白的p标记代码:
$pattern = "/<p[^>]*><\\/p[^>]*>/";
preg_replace($pattern,'',get_the_content());
$pattern=“/]*>]*>/”;
preg_replace($pattern',get_content());
当您在标记中包装div结构时,它会添加多个空白标记,因为这不是块标记,如果您想实现这样的效果,则需要使用jQuery wrap()。
在这里我添加了你的更新代码,请你试试这个
<?php
$i = 1;
while ( have_posts() ) : the_post(); ?>
<div class="card" id="linkBox_<?php echo $i; ?>">
<div class="card__content">
<h3 class="title"><?php echo mb_strimwidth(get_the_title(), 0, 50, '...');?></h3>
<p><?php echo wp_trim_words( get_the_content(), 30, '...' ); ?></p>
</div>
</div>
<?php
$tc1 = '#linkBox_' . $i;
?>
<script type="text/javascript">
jQuery( document ).ready(function( $ ) {
t_ID = '<?php echo $tc1 ?>';
$(t_ID).wrap( '<a class="whole_box_link" href="<?php echo the_permalink(); ?>"></a>' );
});</script>
<?php $i++;
endwhile;
?>
在您的代码中,锚定标记缠绕在
周围,但在输出中,锚定标记位于该标记内。不知道怎么做@NigelRen-是的,这就是最简单的检查是更改代码中的某些内容(将其更改为h2),并查看这些更改是否反映在输出中。我尝试了相同的代码,但没有发现此问题。这对我来说很好。你提到的代码中没有问题。也许还有别的事。检查页面模板代码。这不是问题所在。