这是我的第一个自定义主题,所以我仍然在想这个循环,但我已经让每个连续的帖子围绕前一个出现了。我使用的是引导卡,所以每张新卡的边框都围绕着前一张卡,产生了嵌套效果。
如何停止此操作,并将每个CPT输出为新卡?
<?php
get_header();
if (have_posts()) :
while (have_posts()) :
?>
<div class="card container">
<a href="<?php
// this conditional outputs a different link url for CPT \'resources\'
if (is_post_type_archive( $resources )) {
echo \'https://\' . get_field(\'website\');
} else {
echo the_permalink();
}
?>">
<div class="card-body row"><?php
the_post();
?>
<div class="col-sm img-fluid"><?php
the_post_thumbnail(\'medium\');
?></div>
<div class="col-sm p-2 m-0">
<h2 class="card-title text-center"><?php the_title(); ?></h2>
</a>
<?php
// This section is supposed to get all custom fields and display them if they exist
$fields = get_fields();
if( $fields ): ?>
<ul>
<?php foreach( $fields as $name => $value ): ?>
<li><?php
echo $value;
?></li>
<?php endforeach; ?>
</ul>
<?php
endif;
?><p class="card-text"><?php the_excerpt(); ?></p>
</div><?php
endwhile;
endif;
?>
</div> <!-- card body -->
</div><!-- card -->
<?php
get_footer();
?>
谢谢!
最合适的回答,由SO网友:mrben522 整理而成
卡和卡体div的结束标记位于循环之外。尝试以下操作:
<?php
get_header();
if (have_posts()) :
while (have_posts()) :
?>
<div class="card container">
<a href="<?php
// this conditional outputs a different link url for CPT \'resources\'
if (is_post_type_archive( $resources )) {
echo \'https://\' . get_field(\'website\');
} else {
echo get_the_permalink();
}
?>">
<div class="card-body row"><?php
the_post();
?>
<div class="col-sm img-fluid"><?php
the_post_thumbnail(\'medium\');
?></div>
<div class="col-sm p-2 m-0">
<h2 class="card-title text-center"><?php the_title(); ?></h2>
</a>
<?php
// This section is supposed to get all custom fields and display them if they exist
$fields = get_fields();
if( $fields ): ?>
<ul>
<?php foreach( $fields as $name => $value ): ?>
<li><?php
echo $value;
?></li>
<?php endforeach; ?>
</ul>
<?php
endif;
?><p class="card-text"><?php the_excerpt(); ?></p>
</div>
</div> <!-- card body -->
</div><!-- card -->
<?php
endwhile;
endif;
?>
<?php
get_footer();
?>