WordPressで抜粋文を取得する方法

ここでは、WordPressで抜粋文を取得する方法について説明していきます。

the_excerpt()

the_excerpt()を用いることで、投稿の抜粋文を取得することができます。

構文

<?php the_excerpt(); ?>

使用例

<section class="home-main__section-contents section-contents home-main-posts">
  <ul class="home-main-posts__ul home-main-posts-ul">
    <?php
    if (have_posts()) :
      while (have_posts()) : the_post();
    ?>
        <li class="home-main-posts-ul__li">
          <a href="<?php the_permalink(); ?>" target="blank">
            <?php the_title(); ?>
          </a>
          <p><?php the_excerpt(); ?></p>
          <div class="home-main-posts-ul__release-date">
            公開日:<time class="home-main-posts-ul__time"><?php the_date(); ?></time>
          </div>
          <!-- /.home-main-posts-ul__release-date -->
        </li>
    <?php
      endwhile;
    endif;
    ?>
  </ul>
</section>

出力結果

まとめ

このように、the_excerpt()を用いることで、投稿の抜粋文を表示させることができます。