下面是我的代码,因为我尝试添加html/css标签我的页面错误。我正在使用文字新闻
<?php
// check if the repeater field has rows of data
if( have_rows('lyrics') ):
// loop through the rows of data
while ( have_rows('lyrics') ) : the_row();
// display a sub field value
the_sub_field('lyric_title');
// display a sub field value
the_sub_field('lyrics_text');
endwhile;
else :
// no rows found
endif;
?>
我无法弄清楚:
- 如何在每个重复周围添加div 以进行样式设置
- 将 h3 添加到 the_sub_field("lyric_title");
- 将带有类的 p 添加到 the_sub_field('lyrics_text');
这是因为HTML不会进入PHP。您有两种选择。
1)分解php标签
<?php if( have_rows('lyrics') ): ?>
<?php while ( have_rows('lyrics') ) : the_row(); ?>
<div class="">
<?php the_sub_field('lyric_title'); ?>
<?php the_sub_field('lyrics_text'); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<div class="">
Text
</div>
<?php endif; ?>
2) 回显网页
<?php
if( have_rows('lyrics') ) :
while ( have_rows('lyrics') ) : the_row();
echo "<div class=''>";
the_sub_field('lyric_title');
the_sub_field('lyrics_text');
echo "</div>";
endwhile;
else :
echo "<div class=''>";
echo "Text";
echo "</div>";
endif;
?>
试试这个:
<?php
// check if the repeater field has rows of data
if( have_rows('lyrics') ):
// loop through the rows of data
while ( have_rows('lyrics') ) :
?>
<div>
<?php the_row(); ?>
// display a sub field value
<h3>
<?php the_sub_field('lyric_title'); ?>
</h3>
// display a sub field value
<p class="class">
the_sub_field('lyrics_text');
</p>
</div>
endwhile;
else :
// no rows found
endif;
?>
您可以像这样轻松地在Wordpress模板中将PHP与HTML组合在一起。只要确保保持可读性即可。
在 PHP 标记中,使用 echo 命令打印 HTML:
echo '<div class="enclosed">';
while ( have_rows('lyrics') ) : the_row();
echo '</div>';
或者您可以关闭 PHP 代码,按原样编写标签并再次打开 PHP 代码:
//PHP code before
?>
<div class="enclosed">
<?php
while ( have_rows('lyrics') ) : the_row();
?>
</div>
<?php
//PHP code continues here