LoginSignup
0
0

More than 3 years have passed since last update.

Wordpress 本文の抜粋にタグやショートコードが出力されないように出力

Posted at

忘れてしまわないように自分用にメモです

functions.phpに以下を記述

//本文の抜粋にタグやショートコードが出力されないように + 続き文字を「…」に指定
function custom_excerpt($excerpt_length){
  $my_excerpt = get_the_content();
  $my_excerpt = strip_tags($my_excerpt);
  $my_excerpt = strip_shortcodes($my_excerpt);
  $my_excerpt = mb_substr($my_excerpt, 0, $excerpt_length) . '…';
  echo $my_excerpt;
}

出力させたいphpファイルには以下を記述

<?php custom_excerpt(100); ?>

記入例

archive.php
<?php if ( have_posts() ) : ?>

    <ul class="archive-post-area">
      <?php while ( have_posts() ) : the_post(); ?>

          <li id="post-<?php the_ID(); ?>" >
          <a href="<?php the_permalink(); ?>">
            <dl>
              <div class="title-area">
                <?php $cat = get_the_category(); $cat = $cat[0];?>
               <dt class="cat <?php echo $cat->category_nicename; ?>"><?php echo get_cat_name($cat->term_id); ?></dt>
               <dt class="date"><?php echo get_the_date(); ?></dt>                
              </div>

              <dt class="title"><?php echo wp_trim_words( get_the_title(), 40, '…' ); ?></dt>
              <dd class="text"><?php custom_excerpt(100); ?></dd>
            </dl>
          </a> 
          </li>

      <?php endwhile;?>
          </ul>

      <?php else : ?>
        <div class="error">
          <p>お探しの記事は見つかりませんでした。</p>
        </div>
      <?php endif; ?>

以下のサイトを参考にさせていただきました。
https://www.tsukimi.net/wordpress_excerpt.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0