WordPressの関数で取得したHTMLからテキストだけ取り出したいことや、その逆をしたい時があったので関数をメモしておく。
テキスト -> HTML
wpautop()
という関数を使う。
関数リファレンス/wpautop - WordPress Codex 日本語版
以下は使った時のコード
echo wpautop(get_option('original_theme_options'));
テーマのフッターでテーマカスタマイザによって設定した値を表示する時に使用した。
HTML -> テキスト
wp_strip_all_tags()
という関数を使う。
関数リファレンス/wp strip all tags - WordPress Codex 日本語版
以下は使った時のコード
function get_child_list(){
// クエリの組み立て
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array(
'post_type' => 'page',
'nopaging' => 'false'
));
// 現在ページの子ページを取得
$child_pages = get_page_children( get_the_ID(), $all_wp_pages );
// 子ページが存在しないとき
if(count($child_pages)<1){
return;
}
foreach($child_pages as $child_page){
// 子ページの情報を取得
$child_page_id = $child_page->ID;
$child_page_data = get_post($child_page_id);
$child_page_raw_content = $child_page_data->post_content;
$child_page_url = $child_page_data->guid;
// HTMLを整形( HTMLタグを削除して、110文字を切り出す
$child_page_content = mb_substr(wp_strip_all_tags($child_page_raw_content, true), 0, 55);
}
}
子ページのページのリストを取得するコード。foreach
で出力をすれば各情報を出力することが出来る。
Copyright (c) 2017 Tomoyuki KOYAMA
Released under the MIT license
http://opensource.org/licenses/mit-license.php