在 php 语句中显示 div 标签
show div tag in php statement
我现在正在使用高级自定义字段插件开发 WordPress 主题库,我想在 if
语句为真时显示 <div>
标签。这是我的代码:
<?php
$rows = get_field('classification');
$sort = get_sub_field('sort');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++)?>
<?php if ( $i==1 || $i%5==0) { ?>
<div class="bor"></div>
<h3 style="text-align:center">
<?php echo $sort; ?>
<a id="browser"></a></h3>
<div class="bor"></div>
<?php } ?>
或类似的东西
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++)?>
<?php if ( $i==1 || $i%5==0) { ?>
echo '<div class="bor"></div>';
echo '<h3 style="text-align:center">';
<?php echo $fenlei; ?>
echo '<a id="browser"></a></h3>';
<div class="bor"></div>
<?php } ?>
但是 div 标签的内容没有显示。
任何回复都非常感谢!非常感谢。
如果你想在php中显示html,我建议你使用下面的代码。
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for($i = 1; $i <= $row_count; $i++){
if ( $i==1 || $i%5==0) {
echo '>div class="bor"<>/div<';
echo '>h3 style="text-align:center"<';
echo $fenlei;
echo '>a id="browser"<>/a<>/h3<';
echo '>div class="bor"<>/div<';
}
}
?>
你的一些 echo 语句超出了 php 标签。使用这个:
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++){
if ( $i==1 || $i%5==0) {
echo "<div class='bor'></div>
<h3 style='text-align:center'>".$fenlei."
<a id='browser'></a></h3>
<div class='bor'></div>";
}
}
?>
我猜您正在使用 ACF Pro Repeater 场。在这种情况下,您需要使用 the_row()
来设置子字段的正确内容。查看 Docs 中的这个编辑示例:
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
the_sub_field('sub_field_name');
endwhile;
endif;
?>
所以,我认为您的代码应该更像:
<?php
if(have_rows('classification')):
while (have_rows('classification') ) : the_row();
// Your Code...
endwhile;
endif;
?>
而且,我最近了解到您需要计算 while
循环外的行数。否则它不会捕获行数。
我现在正在使用高级自定义字段插件开发 WordPress 主题库,我想在 if
语句为真时显示 <div>
标签。这是我的代码:
<?php
$rows = get_field('classification');
$sort = get_sub_field('sort');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++)?>
<?php if ( $i==1 || $i%5==0) { ?>
<div class="bor"></div>
<h3 style="text-align:center">
<?php echo $sort; ?>
<a id="browser"></a></h3>
<div class="bor"></div>
<?php } ?>
或类似的东西
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++)?>
<?php if ( $i==1 || $i%5==0) { ?>
echo '<div class="bor"></div>';
echo '<h3 style="text-align:center">';
<?php echo $fenlei; ?>
echo '<a id="browser"></a></h3>';
<div class="bor"></div>
<?php } ?>
但是 div 标签的内容没有显示。 任何回复都非常感谢!非常感谢。
如果你想在php中显示html,我建议你使用下面的代码。
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for($i = 1; $i <= $row_count; $i++){
if ( $i==1 || $i%5==0) {
echo '>div class="bor"<>/div<';
echo '>h3 style="text-align:center"<';
echo $fenlei;
echo '>a id="browser"<>/a<>/h3<';
echo '>div class="bor"<>/div<';
}
}
?>
你的一些 echo 语句超出了 php 标签。使用这个:
<?php
$rows = get_field('classification');
$fenlei = get_sub_field('fenlei');
$row_count = count($rows);
for ($i = 1; $i <= $row_count; $i++){
if ( $i==1 || $i%5==0) {
echo "<div class='bor'></div>
<h3 style='text-align:center'>".$fenlei."
<a id='browser'></a></h3>
<div class='bor'></div>";
}
}
?>
我猜您正在使用 ACF Pro Repeater 场。在这种情况下,您需要使用 the_row()
来设置子字段的正确内容。查看 Docs 中的这个编辑示例:
<?php // check if the repeater field has rows of data if( have_rows('repeater_field_name') ): // loop through the rows of data while ( have_rows('repeater_field_name') ) : the_row(); // display a sub field value the_sub_field('sub_field_name'); endwhile; endif; ?>
所以,我认为您的代码应该更像:
<?php
if(have_rows('classification')):
while (have_rows('classification') ) : the_row();
// Your Code...
endwhile;
endif;
?>
而且,我最近了解到您需要计算 while
循环外的行数。否则它不会捕获行数。