admin管理员组文章数量:1025202
This is my first custom theme so I'm still getting my head around the loop, but I've somehow got each successive post to appear around the previous one. I'm using bootstrap cards so the border for each new card goes around the previous, creating a nesting effect.
How do I stop this, and have each CPT output as a new card?
<?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();
?>
Thanks!
This is my first custom theme so I'm still getting my head around the loop, but I've somehow got each successive post to appear around the previous one. I'm using bootstrap cards so the border for each new card goes around the previous, creating a nesting effect.
How do I stop this, and have each CPT output as a new card?
<?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();
?>
Thanks!
Share Improve this question asked Apr 8, 2019 at 17:56 JakePowellJakePowell 431 silver badge10 bronze badges 2 |1 Answer
Reset to default 0Your closing tags for the card and card body divs are outside of the loop. try this:
<?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();
?>
This is my first custom theme so I'm still getting my head around the loop, but I've somehow got each successive post to appear around the previous one. I'm using bootstrap cards so the border for each new card goes around the previous, creating a nesting effect.
How do I stop this, and have each CPT output as a new card?
<?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();
?>
Thanks!
This is my first custom theme so I'm still getting my head around the loop, but I've somehow got each successive post to appear around the previous one. I'm using bootstrap cards so the border for each new card goes around the previous, creating a nesting effect.
How do I stop this, and have each CPT output as a new card?
<?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();
?>
Thanks!
Share Improve this question asked Apr 8, 2019 at 17:56 JakePowellJakePowell 431 silver badge10 bronze badges 2-
1
You need to move one (or possibly 2) closing
div
tags inside of thewhile
loop – mrben522 Commented Apr 8, 2019 at 18:10 - That did it! Thanks, I moved both the closing div tags up into the loop. So simple. – JakePowell Commented Apr 8, 2019 at 18:12
1 Answer
Reset to default 0Your closing tags for the card and card body divs are outside of the loop. try this:
<?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();
?>
本文标签: loopCustom post type appearing within the previous one on archivephp
版权声明:本文标题:loop - Custom post type appearing within the previous one on archive.php 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614409a2159200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
div
tags inside of thewhile
loop – mrben522 Commented Apr 8, 2019 at 18:10