WP PHP

文字の抜粋いろいろ

アーカイブページで、タイトルや本文を抜粋して表示したいとき

タイトルの抜粋

<?php echo mb_substr(get_the_title(), 0, 40); ?>

本文の抜粋

<?php echo mb_substr(get_the_excerpt(), 0, 100); ?>

本文 文末文字指定付き

<?php echo mb_substr(strip_tags($post-> post_content),0,100) . '...'; ?>

カスタムフィールドの値の抜粋

<?php echo mb_substr((post_custom('カスタムフィールド名')),0,100); ?>

カスタムフィールドの値の抜粋(改行も反映させる場合)

<?php echo mb_substr(nl2br(post_custom('カスタムフィールド名')),0,100); ?>
home > TIPS > WP PHP > 文字の抜粋いろいろ

Copyright © 2012 SMILEWORKS All Rights Reserved.