前提・実現したいこと
wordpress(php)の本文に、記事内ページャ機能をつけた記事本文を、配列化して取得したいです。
ご教授をお願いいたします。
<!--nextpage--> ```を使い、 次項のような 5ページの記事があるとして、 functions.php 内で、各ページ毎に文字列を分割したいと考えています。 ### the_content( $post->post_content )の内容
//以下のような本文が登録されている
$the_content = "
<h2>1P目</h2><p>...</p> <!--nextpage--> <h2>2P目</h2><p>...</p> <!--nextpage--> <h2>3P目</h2><p>...</p> <!--nextpage--> <h2>4P目</h2><p>...</p> <!--nextpage--> <h2>5P目</h2><p>...</p> ";### 取得したいコード例
$matches = array(
0 => '<h2>1P目</h2><p>...</p><!--nextpage-->',
1 => '<h2>2P目</h2><p>...</p><!--nextpage-->',
2 => '<h2>3P目</h2><p>...</p><!--nextpage-->',
3 => '<h2>4P目</h2><p>...</p><!--nextpage-->',
4 => '<h2>5P目</h2><p>...</p>',
);
// もしくは
$matches = array(
0 => '<h2>1P目</h2><p>...</p>',
1 => '<!--nextpage-->',
2 => '<h2>2P目</h2><p>...</p>',
3 => '<!--nextpage-->',
4 => '<h2>3P目</h2><p>...</p>',
5 => '<!--nextpage-->',
6 => '<h2>4P目</h2><p>...</p>',
7 => '<!--nextpage-->',
8 => '<h2>5P目</h2><p>...</p>',
);
### 試したこと シンプルに正規表現で 、preg_match , preg_match_all に渡す pattern がうまく作れればできると予想していますが疎く、 pattern がうまく書けませんでした。 もしくはwordpressに備わっている別の何かで近いことを実現できればそちらの方法でも構いません。
//functions.php
function get_nextpage_arr( $content ) {
global $post;
if ( is_single() ) {
$content_str = $post->post_content;
$pattern = ""; // ★ ここに入れるべきpatternを知りたい
if ( preg_match_all( $pattern, $content_str, $matches ) ) {
// ★ここで取得したい }
}
}
add_filter('the_content','get_nextpage_arr');
### 補足情報 php ver 7.1.0 WordPress 4.8.2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/29 03:01