【问题标题】:Wordpress different layout of related articlesWordPress相关文章的不同布局
【发布时间】: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


【解决方案1】:

您可以为包含该文章的div 的开始标记添加 if/else 子句,在不同版本中添加所需的类(仅适用于第一篇文章 - 我为此添加了一个计数器):

$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 $counter = 1; ?>
      <?php foreach ( $relatedArticles as $relatedArticle ): ?>
      <?php if ( count( $relatedArticles ) >= 3 ) { ?>
        <div class="col-sm-4">
      <?php } elseif (( count( $relatedArticles ) == 2) && ($counter == 1)) { ?>
        <div class="col-sm-4 col-sm-offset-2">
      <?php } elseif (( count( $relatedArticles ) == 1) &&  ($counter == 1)) { ?>
        <div class="col-sm-4 col-sm-offset-4">
      <?php } ?>
        <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 $counter++; ?>
      <?php endforeach; ?>
    </div>
  </div>
</section>
<?php endif ?>

【讨论】:

  • 如果计数为2,只有第一列必须有class col-sm-offset-2,可以吗?
  • 我刚刚添加了一个计数器并检查了第一个 foreach 循环实例 - 现在应该可以正常工作了
猜你喜欢
  • 2019-04-12
  • 1970-01-01
  • 2010-11-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
相关资源
最近更新 更多