2013年09月11日 追記・変更

WordPressタグのthe_content()をもっと使いやすくする
今回は、記事本文の呼び出しで使用しているthe_contentの一歩進んだ使い方。the_contentのmore前後を切り分けて出力すれば見せ方に幅が広がる。
まずはfunctions.phpにページ前後切り分け用の関数を登録
<?php function get_the_divided_content( $more_link_text = null, $stripteaser = 0, $more_file = '' ) { $regex = '#(<p><span id="more-[\d]+"></span></p>|<span id="more-[\d]+"></span>)#'; $content = get_the_content( $more_link_text, $stripteaser, $more_file ); $content = apply_filters( 'the_content', $content ); $content = str_replace( ']]>', ']]>', $content ); if ( preg_match( $regex, $content ) ) { list( $content_array['before'], $content_array['after'] ) = preg_split( $regex, $content, 2 ); } else { $content_array['before'] = ''; $content_array['after'] = $content; } return $content_array; } ?>
記事のループ箇所へ関数読み出しのための準備
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 上記の直後に下記を記述して関数を呼び出す <?php $content = get_the_divided_content( '続きを読む' ); ?>
記事のmore以前の呼び出し方法
<?php echo $content['before']; ?>
記事のmore以降の呼び出し方法
<?php echo $content['after']; ?>
通常の呼び出し方法はthe_content()を使う
<?php the_content(); ?>