0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[WordPressテーマ] HTMLからテキスト、テキストからHTMLに変換する

0
Last updated at Posted at 2017-12-19

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?