【发布时间】:2018-07-25 03:24:12
【问题描述】:
我正在尝试相关文章的 3 列布局,但如果相关文章的数量少于 3 则需要不同的布局。如果相关文章的数量为 2,则第一列的类别为 col-sm-offset-2,如果列为 1 - 类别为 col-sm-offset-4。
$category = get_post_category();
$relatedArticles = get_posts( [
'exclude' => $post->ID,
'category' => $category->term_id,
'posts_per_page' => 3
] );
?>
<?php if ( count( $relatedArticles ) > 0 ): ?>
<section class="related-articles">
<div class="container">
<div class="row">
<h2 class="text-center"><?php echo __( 'Related Articles' ) ?></h2>
<?php foreach ( $relatedArticles as $relatedArticle ): ?>
<div class="col-sm-4">
<article id="" class="post">
<div class="post-image">
<a href="<?php echo get_permalink( $relatedArticle ) ?>">
<?php echo get_the_post_thumbnail( $relatedArticle, 'post-category' ) ?>
</a>
</div>
<h3 class="post-title">
<a href="<?php echo get_permalink( $relatedArticle ); ?>">
<?php echo get_post_title($relatedArticle) ?>
</a>
</h3>
<div class="post-summary">
<p><?php echo get_summery($relatedArticle);?></p>
</div>
</article>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
<?php endif ?>
【问题讨论】:
-
如果你想要一个工作的 HTML sn-p 作为模板,你需要在你的问题中发布你当前的输出 HTML。另外,请指定您正在使用的 Bootstrap 版本。
标签: php html wordpress twitter-bootstrap layout