文字の一部を抜粋して表示する方法

get_posts()で取ってきたものを抜粋する方法
PHP
1 2 3 4 5 6 7 8 9 |
<?php foreach($my_posts as $my_post ) : $post_content = $my_post->post_content; $content = str_replace('\n', '', mb_substr(strip_tags($post_content), 0, 120,'UTF-8')); $content = $content.'......'; echo $content; endforeach; ?> |
メインループ内のもので抜粋する方法
PHP
1 2 3 4 5 6 7 |
<?php $post_content = get_the_content(); $content = str_replace('\n', '', mb_substr(strip_tags($post_content), 0, 120,'UTF-8')); $content = $content.'......'; echo $content; ?> |
postの取ってくる方法が違う。
この場合は、コンテンツの出力になっているけれど、もしタイトルを取ってきたいのであれば、
get_posts(サブループ)なら post_contentをpost_titleに。
メインループならget_the_title()でやってあげれば、タイトルを省略したものを取ってくることができる。