投稿記事にスライドショーの追加

投稿記事にスライドショーの追加

画像スライダーをつけるには
Meta Slider
プラグインを使います

本来なら撮影した写真を使うのですが
今回は実験
とはいっても最初から素材を用意するのは大変なので
無料写真素材サイト
FOODIE’S FEED

のものを使わせていただくことにします

なお、本来は、カスタム投稿でアップロードしたものを
使う予定でしたが、方法がわからないため
プラグインと投稿ごと行うことになります

まずは
Meta Slider
をインストールします

ダッシュボード > プラグイン >
新規追加で
Meta Slider
で検索し、インストールと有効化を行います

meta

するとダッシュボードに
Meta Slider という項目ができるので
これをクリックし、
最初のスライドショーを作成するため
+ボタンをクリックします

meta2

名前が新規スライダーだとわかりにくいので
sweet に変更し、スライドを追加をクリック

meta3

ファイルアップロード画面になるので
ダウンロードしておいたファイルを選択します

なお、アップロードするサイズが大きいとエラーがでます

meta5[

初期設定では2MBなので
変更しないとほぼ無理だと思います

今回は Ubuntu 14.04 での設定変更です
方法は2つあり
php.ini を変更する
もしくは
.htaccess での設定になります

今回はphp.ini の変更にします

sudo vim /etc/php5/apache2/php.ini 

で設定ファイルを開き

/post_max
で検索

673行目ぐらいに

post_max_size = 8M

があるので

post_max_size = 2000M

に変更

次に、
/upload_max
で検索し
805行目あたりの

upload_max_filesize = 2M

upload_max_filesize = 2000M

に変更

これで2GBまでアップできます

設定できたら

sudo service apache2 restart

で apache2 を再起動

もう一度アップロードすると成功します

アップロードが完了すると
一覧ができるので
保存をクリックします

meta6

次に、投稿記事に挿入していきます

テストとして
ダッシュボードの投稿から
Hello world を選びます

モードをビジュアルからテキストに変更し
スライダーを追加
をクリックします

meta7

どれを追加するか選ぶ画面になりますが
今回は1つしかないので
そのまま
Insert slideshow
をクリックします

meta8

すると、

[metaslider id=16]
というように
ショートコードが挿入されます

meta10

これで投稿にスライダーが追加されます

meta9

今回は記事の下へ追加しましたが
場所を変更することで
投稿内の好きな場所へ配置することができます

なお、今回の変更で不要になるギャラリー部分は削除しますので

<h3>ギャラリー</h3>
<img src="image/food.jpg" alt="" width="300" height="300">

を削除します

これでソースは

<?php get_header(); ?>

<?php if(have_posts()): while(have_posts()): the_post(); ?>

  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?>
  <h3><?php echo post_custom("Impression")?></h3>
  <?php endif;?>
  <?php the_content(); ?>
  <img src="image/tee.jpg" alt="" width="300" height="300">

  <h3>メニュー</h3>
  <div data-role="collapsible">
    <h3>私のおすすめメニュー</h3>
    <p>イカスミパスタ</p>
  </div>
<?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?>
<h3>店舗情報</h3>
  <table data-role="table" data-mode="reflow" class="ui-responsive">
    <thead>
      <tr>
        <th>店名</th>
        <th>定休日</th>
        <th>営業時間</th>
        <th>ランチ価格</th>
        <th>住所</th>
        <th>電話番号</th>
        <th>HP</th>
        <th>アクセス</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><?php echo post_custom("ShopName")?></td>
        <td><?php echo post_custom("Holiday")?></td>
        <td><?php echo post_custom("Time")?></td>
        <td><?php echo post_custom("lunch")?></td>
        <td><?php echo post_custom("Address")?></td>
        <td><?php echo post_custom("tel")?></td>
        <td><?php echo post_custom("Hp")?></td>
        <td><?php echo post_custom("Access")?></td>
      </tr>
    </tbody>
  </table>
<h3>周辺地図</h3>
<?php
    $map = esc_html(get_post_meta($post->ID,"Address",true));

    echo do_shortcode('[map addr="'.$map.'" height="400px" zoom="16"]');
 ?>
  <?php endif;?>

  <?php endwhile;   else: ?>
    <p>記事はありません</p>
  <?php endif; ?>


<?php get_footer(); ?>

となります

Google Map を埋め込む wordpress プラグイン

Google Map を埋め込む wordpress プラグイン

現在の状態だと

  <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3241.7481415393895!2d139.74543744402948!3d35.65857638920314!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x60188bbd9009ec09%3A0x481a93f0d2a409dd!2z5p2x5Lqs44K_44Ov44O8!5e0!3m2!1sja!2sjp!4v1436313888247" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>

となっているため地図が固定されています

これを投稿したお店の場所に変えてみます

今回は Google Map を埋め込めるプラグインを使います

ダッシュボード > プラグイン >新規追加

Simple Map を検索し
インストールして有効化します

map

なお、 Simple Map を
投稿記事内に使いたい場合は
Google Mapを埋め込むWordPressプラグイン「Simple Map」の使い方

を参考に

今回は、テーマファイルに埋め込み
カスタムフィールドの値を元に Map を出します

この場合、
カスタムフィールドで入力した住所を変数に格納し
それを
do_shortcode() で表示します

なお、カスタムフィールドの値を取得し
変数にいれるときには
esc_html() でエスケープします

esc_html() については
WordPressでechoする文字を適切にエスケープする

の解説を参考に

今回、取得するカスタムフィールドの値は
Address になります

[解決済み] 投稿ページから記事ごとに地図を表示したい

カスタムフィールドに住所を入れてgoogle mapsを表示する
を参考に
テンプレートファイルの single.php に埋め込みました

まず、カスタムフィールドで入力した住所を
変数に入れます

$map = esc_html(get_post_meta($post->ID,"Address",true));

Adress は、カスタムフィールドで設定したIDです

次に、地図の表示です
決め打ちで表示するのなら

<?php echo do_shortcode('[map addr="東京都千代田区永田町1-7"]'); ?>

とすれば表示されます

構文は
[map addr=”住所”]
となります

住所は 変数 $map に格納しているので

echo do_shortcode('[map addr="'.$map.'"]');

で表示できます

これで地図も記事ごとに表示できるようになりました

cus4

今回の変更により以下のコードになります

<?php get_header(); ?>

<?php if(have_posts()): while(have_posts()): the_post(); ?>

  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?>
  <h3><?php echo post_custom("Impression")?></h3>
  <?php endif;?>
  <?php the_content(); ?>
  <img src="image/tee.jpg" alt="" width="300" height="300">

<h3>ギャラリー</h3>
<img src="image/food.jpg" alt="" width="300" height="300">

  <h3>メニュー</h3>
  <div data-role="collapsible">
    <h3>私のおすすめメニュー</h3>
    <p>イカスミパスタ</p>
  </div>
<?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?>
<h3>店舗情報</h3>
  <table data-role="table" data-mode="reflow" class="ui-responsive">
    <thead>
      <tr>
        <th>店名</th>
        <th>定休日</th>
        <th>営業時間</th>
        <th>ランチ価格</th>
        <th>住所</th>
        <th>電話番号</th>
        <th>HP</th>
        <th>アクセス</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><?php echo post_custom("ShopName")?></td>
        <td><?php echo post_custom("Holiday")?></td>
        <td><?php echo post_custom("Time")?></td>
        <td><?php echo post_custom("lunch")?></td>
        <td><?php echo post_custom("Address")?></td>
        <td><?php echo post_custom("tel")?></td>
        <td><?php echo post_custom("Hp")?></td>
        <td><?php echo post_custom("Access")?></td>
      </tr>
    </tbody>
  </table>
<h3>周辺地図</h3>
<?php
    $map = esc_html(get_post_meta($post->ID,"Address",true));
    
    echo do_shortcode('[map addr="'.$map.'"]');
 ?>
  <?php endif;?>

  <?php endwhile;   else: ?>
    <p>記事はありません</p>
  <?php endif; ?>


<?php get_footer(); ?>

カスタムフィールドの入力と反映

カスタムフィールドの入力と反映

カスタムフィールドを使えるようにしたので
現在テーブルに入力した値をこちらに入力します

今回は hello world の投稿の下に追加された
カスタムフィールドテンプレートに入力します

cus

入力ができたら
保存をクリックしておきます

次に、この入力内容を反映するために
single.php を編集します

    <tbody>
      <tr>
        <td>○○のお店</td>
        <td>日曜日</td>
        <td>11:00〜17:00</td>
        <td>ランチ1000円</td>
        <td>東京都</td>
        <td>000-000-0000</td>
        <td>http://example.co.jp</td>
        <td>○○駅○版口から徒歩</td>
      </tr>
    </tbody>

の部分を書き換えます

    <tbody>
      <tr>
        <td><?php echo post_custom("ShopName")?></td>
        <td><?php echo post_custom("Holiday")?></td>
        <td><?php echo post_custom("Time")?></td>
        <td><?php echo post_custom("lunch")?></td>
        <td><?php echo post_custom("Address")?></td>
        <td><?php echo post_custom("tel")?></td>
        <td><?php echo post_custom("Hp")?></td>
        <td><?php echo post_custom("Access")?></td>
      </tr>
    </tbody>

というように書き換えます

<?php echo post_custom("ShopName")?>

というように
post_custom() の引数は
カスタムフィールドで設定したIDになります

ShopName なら店名になります

これでプレビューを見ると
テーブルに内容が反映され表示されます

cus2

次に、お店の簡単な紹介文も
カスタムフィールドのものを反映するようにします

<h3>午後の緩やかな時間を本とともに</h3>

  <?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?>
  <h3><?php echo post_custom("Impression")?></h3>
  <?php endif;?>

というように修正します

これでほぼ内容を反映できるようになりました

今回のコードは以下のようになります

<?php get_header(); ?>

<?php if(have_posts()): while(have_posts()): the_post(); ?>

  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?>
  <h3><?php echo post_custom("Impression")?></h3>
  <?php endif;?>
  <?php the_content(); ?>
  <img src="image/tee.jpg" alt="" width="300" height="300">

<h3>ギャラリー</h3>
<img src="image/food.jpg" alt="" width="300" height="300">

  <h3>メニュー</h3>
  <div data-role="collapsible">
    <h3>私のおすすめメニュー</h3>
    <p>イカスミパスタ</p>
  </div>
<?php if ( get_post_meta($post->ID,'ShopName',TRUE) ): ?>
<h3>店舗情報</h3>
  <table data-role="table" data-mode="reflow" class="ui-responsive">
    <thead>
      <tr>
        <th>店名</th>
        <th>定休日</th>
        <th>営業時間</th>
        <th>ランチ価格</th>
        <th>住所</th>
        <th>電話番号</th>
        <th>HP</th>
        <th>アクセス</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><?php echo post_custom("ShopName")?></td>
        <td><?php echo post_custom("Holiday")?></td>
        <td><?php echo post_custom("Time")?></td>
        <td><?php echo post_custom("lunch")?></td>
        <td><?php echo post_custom("Address")?></td>
        <td><?php echo post_custom("tel")?></td>
        <td><?php echo post_custom("Hp")?></td>
        <td><?php echo post_custom("Access")?></td>
      </tr>
    </tbody>
  </table>
<h3>周辺地図</h3>
  <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3241.7481415393895!2d139.74543744402948!3d35.65857638920314!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x60188bbd9009ec09%3A0x481a93f0d2a409dd!2z5p2x5Lqs44K_44Ov44O8!5e0!3m2!1sja!2sjp!4v1436313888247" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>
  <?php endif;?>

  <?php endwhile;   else: ?>
    <p>記事はありません</p>
  <?php endif; ?>


<?php get_footer(); ?>

ディレクトリバックアップ

ディレクトリバックアップ

tar コマンドでディレクトリのしたを1つにまとめ
圧縮保存する

~/以下のファイルを
/home/

日付_home.tar.gz
で保存する

sudo tar zcvf /home/20150724.tar.gz ~/

/home に保存するには
sudo でやらないと権限の関係でできない

ちなみに、元のファイルはそのままなので心配ない

オプションの意味は
zで gzip圧縮
cで圧縮
vでログ表示
fでファイルにバックアップを作成
という意味

アーカイブはバックアップとも言える

圧縮するときには
tar zcvf バックアップファイル名 対象パス
と覚えておく

さらに圧縮率をあげるなら
jオプションをつかう
これは圧縮方法の違いで
zは gzip
jだと bzip2
になる

この場合
保存するときのファイル拡張子を
bz2 にする

圧縮のときには
tar jcvf バックアップファイル名 対象パス
となる

tar コマンドについては

【 tar 】 ファイルを書庫化・展開する(拡張子.tarなど

にオプションの意味なども載っているので
参考に

そして時間の違いを実験するため
time コマンドで実行時間を表示します
time コマンドについての詳細は
【 time 】 指定したコマンドの実行時間を表示する

を参考に

結果ですが

time sudo tar zcvf /home/20150724.tar.gz ~/

でzオプションによる gzip 圧縮の場合


real	0m20.856s
user	0m8.941s
sys	0m7.542s

となり

time sudo tar jcvf /home/20150724.tar.bz2 ~/

で j オプションによる bzip2 圧縮だと

real	0m44.561s
user	0m32.664s
sys	0m6.771s

そして、実際にサイズを ls コマンドで
測ってみます
ls コマンドの詳細は
【 ls 】 ファイルやディレクトリの情報を表示する

を参考に

今回つかうオプションは
a すべてのファイルを対象
h 単位を読みやすくする
l 詳細表示
t タイムスタンプ順にして並べる
r 逆順にして並べる

これらをつかって比較結果を表示します

ls -ahltr /home/20150724.tar.*

結果は


-rw-r--r--. 1 root root 83M  7月 24 07:34 2015 /home/20150724.tar.gz
-rw-r--r--. 1 root root 73M  7月 24 07:36 2015 /home/20150724.tar.bz2


となります

これにより
bzip2 圧縮のほうが73MBとなり
サイズが小さいということがわかります

Custom Field Template でテーブル作成

Custom Field Template でテーブル作成

WordPressとプラグインを使ってお店紹介サイトを作る方法
を参考に
Custom Field Templateプラグインを使い
テーブルを作成します

    <thead>
      <tr>
        <th>店名</th>
        <th>定休日</th>
        <th>価格</th>
        <th>住所</th>
        <th>電話番号</th>
        <th>HP</th>
        <th>アクセス</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>○○のお店</td>
        <td>日曜日</td>
        <td>ランチ1000円</td>
        <td>東京都</td>
        <td>000-000-0000</td>
        <td>http://example.co.jp</td>
        <td>○○駅○版口から徒歩</td>
      </tr>
    </tbody>

となっていますが
記事の内容を反映するようにします

まず、ダッシュボードから
プラグインで
Custom Field Template
で検索し、インストールします

cuf

インストールしたら有効化を忘れずに

次に、
設定 > カスタムフィールドテンプレート

デフォルトテンプレート
から
店舗情報へ変更します

cul2

次に
テンプレートコンテンツに
カスタムフィールドにしたいリストを書きます

今回は
[ShopName]
type=text
size = 35
label=店名

[Holiday]
type = text
size = 35
label = 定休日

[Time]
type = text
size = 35
label = 営業時間

[lunch]
type=text
size =35
label=ランチ価格

[Address]
type = text
size=35
label = 住所

[tel]
type=text
size=35
label=電話番号

[Hp]
type=text
size=35
label=HP

[Access]
type = textarea
cols = 80
rows = 3
label=お店への案内

[Impression]
type = textarea
cols = 80
rows = 3
label = お店のひとことメモ
としました

cul3

設定できたら、オプションを更新する
をクリックします

これで、投稿一覧から
hello world を選ぶと
カスタムフィールドテンプレートが
追加されているのがわかります

cul4

今回はプラグインの設定だけなので、ソース変更はありません

お店情報ページの作成 single.php の場合

お店情報ページの作成 single.php の場合

お店情報は、投稿画面で作成して増やしていくので
single.php を作成します

まず、以前作成したお店情報のページをコピーします

ただし、投稿表示画面のファイル名が
single.php
であることに注意してください

今回は、別のディレクトリからコピーしました

cp ../../../../shop.html single.php

画面の状態を確認するには
ダッシュボードから記事の投稿画面で
変更をプレビュー
でみるとわかります

ただし、今のソースのままだと
wordpress の内容は反映されません

このため、ソースを書き換えていきます

まず、wordpress のヘッダーとフッターになるように
テンプレに変更します

ヘッダー部分には

<?php get_header(); ?>

フッター部分には

<?php get_footer(); ?>

とします

次に、記事があるかないかを判定します
もし、ないのなら
記事がありません
と表示するようにします

このため、本文を判定で囲みます

<?php if(have_posts()): while(have_posts()): the_post(); ?>

<?php get_header(); ?>

の下へ書き

    <?php endwhile;   else: ?>
      <p>記事はありません</p>
    <?php endif; ?>

<?php get_footer(); ?>

の上に書きます

have_posts() は記事があるかを判定

the_post() は次の記事の取得です

この条件式の解説には
WordPressテーマの「while (have_posts()) : the_post();」の意味は?

がわかりやすいと思います

次にタイトルを wordpress の投稿で入力した
タイトルになるようにします

<h2>ティーセット</h2>

  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

へ変更します

これで投稿一覧の中から
hello world を選び
変更をプレビューすると

タイトルが Hello world になります

single2

the_title() は タイトルを表示します

the_permalink() は記事へのリンクになります

次に、お店のランチメニューの紹介を書く場所を
編集します

<p>忙しい喧騒を離れ、午後はゆっくりとお茶を飲みたいときにおすすめです</p>

の部分には投稿本文を反映できるようにします

投稿内容を表示するには

  <?php the_content(); ?>

とします

これで保存し、再度変更をプレビューすると
hello world の記事に内容が変わります

single3

今回の single.php のソースは以下のようになります

<?php get_header(); ?>

<?php if(have_posts()): while(have_posts()): the_post(); ?>

  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <h3>午後の緩やかな時間を本とともに</h3>
  <?php the_content(); ?>
  <img src="image/tee.jpg" alt="" width="300" height="300">

<h3>ギャラリー</h3>
<img src="image/food.jpg" alt="" width="300" height="300">

  <h3>メニュー</h3>
  <div data-role="collapsible">
    <h3>私のおすすめメニュー</h3>
    <p>イカスミパスタ</p>
  </div>

<h3>店舗情報</h3>
  <table data-role="table" data-mode="reflow" class="ui-responsive">
    <thead>
      <tr>
        <th>店名</th>
        <th>定休日</th>
        <th>価格</th>
        <th>住所</th>
        <th>電話番号</th>
        <th>HP</th>
        <th>アクセス</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>○○のお店</td>
        <td>日曜日</td>
        <td>ランチ1000円</td>
        <td>東京都</td>
        <td>000-000-0000</td>
        <td>http://example.co.jp</td>
        <td>○○駅○版口から徒歩</td>
      </tr>
    </tbody>
  </table>
<h3>周辺地図</h3>
  <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3241.7481415393895!2d139.74543744402948!3d35.65857638920314!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x60188bbd9009ec09%3A0x481a93f0d2a409dd!2z5p2x5Lqs44K_44Ov44O8!5e0!3m2!1sja!2sjp!4v1436313888247" width="600" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>
  <?php endwhile;   else: ?>
    <p>記事はありません</p>
  <?php endif; ?>


<?php get_footer(); ?>

タイトルの変更

タイトルの変更

現在
タイトルが
jQuery Mobile
となっているので、これをサイト名に変更します

header.php の

 <h1>jQuery Mobile</h1>

の部分を

<h1><?php bloginfo('name'); ?></h1>

にします

これでテンプレを使っているので
ダッシュボードでサイト名を変更しても自動更新してくれます

head

今回の変更で header.php のソースは以下のようになります

<!DOCTYPE html>
<html>
<head>
<meta charset="<?php bloginfo('charset'); ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title><?php if(!is_home()){ wp_title('|',true,'right'); } bloginfo('name'); ?></title>
<link rel="stylesheet"type="text/css" href="<?php echo get_stylesheet_uri(); ?>" >
<link rel="stylesheet"
href="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js">
</script>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div data-role="page" data-title="jQuery Mobile">
<div data-role="header">
 <h1><?php bloginfo('name'); ?></h1>
</div>
<div role="main" class="ui-content">

sed コマンドの区切り文字について

cat memo2 | sed 's; ;</td><td>;g’

で意味がわからなかったけど
調べた結果、
sed コマンドでの区切り文字は
/ /でなくても良いということ

つまり、この場合; ; が区切り文字になっている

また、sed の -i オプションで
ファイルを上書きできるけど
これができるのは GNU sed 限定だった
ということがわかった

home.php の編集

home.php の編集

まず、home.php のソースを
header.php
footer.php から読み込むように書き換えます

まず一番上から
GridView のソースまえまでの

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>jQuery Mobile</title>
<link rel="stylesheet"
href="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js">
</script>
<style>
  img.ic{
    width: 64px;
    height: 64px;
  }
</style>
</head>
<body>
<div data-role="page" data-title="jQuery Mobile">
<div data-role="header">
 <h1>jQuery Mobile</h1>
</div>
<div role="main" class="ui-content">

を削除します

代わりに

<?php get_header(); ?>

を書き込みます

次に、フッター部分もテンプレートに変えます


</div>
<div data-role="footer">
 フッター
</div>
</div>
</body>
</html>

を削除、代わりに

<?php get_footer(); ?>

を書き込みます

これで保存すると
フッター部分が変化しているのがわかります

homew

次に、画像が表示されていないので
画像を格納するフォルダを作ります

Ubuntu などの Linux なら

mkdir img

で作成できます

このフォルダへ画像をコピーします
Mac なら Cyberduck などでアップロードすることもできます

私の場合、すでに別のフォルダにあるものを
cp コマンドでコピーしました

cd img/
cp /var/www/html/wpt/icon/*.png .

. は現在のディレクトリを示します
*.png はすべての .png ファイルを意味します

そして、後は画像へのパスを変更します

<img src="icon/wa.png" 

となっている部分を
wordpress のテンプレに変えます

テンプレまでのパスは
get_template_directory_uri()
で取得できるので、
“icon/wa.png”
の部分を書き換えます

ファイル名はそのままですが、格納しているフォルダも違うので
これもまとめて変えます

PHP では echo で結果を出力できるので
パスを

<?php echo get_template_directory_uri(); ?>

とすれば取得できます

しかし、今回は対象となるものが

<div class="ui-grid-b">
  <div class="ui-block-a">

    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/wa.png" alt="" class="ic"><br>和食</a>
  </div>
  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/chu.png" alt="" class="ic"><br>中華</a>
  </div>
  <div class="ui-block-c">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/yo.png" alt="" class="ic"><br>洋食</a>
  </div>
</div>

<!-- 2行目 -->
<div class="ui-grid-a">
  <div class="ui-block-a">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/cake.png" alt="" class="ic"><br>スイーツ</a>
  </div>

  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/ramen.png" alt="" class="ic"><br>ラーメン</a>
  </div>
</div>

<!-- 3行目 -->
<div class="ui-grid-b">
  <div class="ui-block-a">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/camera.png" alt="" class="ic"><br>カメラ</a>
  </div>
  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/map.png" alt="" class="ic"><br>地図</a>
  </div>
  <div class="ui-block-c">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/tweet.png" alt="" class="ic"><br>ツイート</a>
  </div>
</div>

となっていて全部手打ちでやるのは大変ですし
ミスタイプで処理するのも面倒です

今回はvim の置き換えを使います
このため、一度

cd -

で1つ前のディレクトリに移動します

次に

vim home.php

でファイルを開き

:%s/icon\//<?php echo get_template_directory_uri\(\); \?>\/img\//g

とすることで
該当する場所をすべて置き換えることができます

もし、失敗してしまっても

:u

とすることで1つ前に戻すことができます

これで画像が表示されます

wpsh

が、大きすぎるので
CSSで設定をします

style.css を開き

  img.ic{
    width: 64px;
    height: 64px;
  }

を追記して保存します

これで程よい大きさになります

wph4
これで設定が反映されるはずですが
chrome を使っていると、反映されないときがあります

そんなときには
右上のメニューの履歴を開き、
閲覧履歴データの消去を行います

これで再度プレビューすると設定反映が確認できます

wph3

この修正方法については
【WordPress】CSS修正したけど反映されないときに確認すべきこと

を参考にさせていただきました

今回の編集したコードは home.php が

<?php get_header(); ?>

<!-- 1行目 -->
<div class="ui-grid-b">
  <div class="ui-block-a">

    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="<?php echo get_template_directory_uri(); ?>/img/wa.png" alt="" class="ic"><br>和食</a>
  </div>
  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="<?php echo get_template_directory_uri(); ?>/img/chu.png" alt="" class="ic"><br>中華</a>
  </div>
  <div class="ui-block-c">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="<?php echo get_template_directory_uri(); ?>/img/yo.png" alt="" class="ic"><br>洋食</a>
  </div>
</div>

<!-- 2行目 -->
<div class="ui-grid-a">
  <div class="ui-block-a">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="<?php echo get_template_directory_uri(); ?>/img/cake.png" alt="" class="ic"><br>スイーツ</a>
  </div>

  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="<?php echo get_template_directory_uri(); ?>/img/ramen.png" alt="" class="ic"><br>ラーメン</a>
  </div>
</div>

<!-- 3行目 -->
<div class="ui-grid-b">
  <div class="ui-block-a">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="<?php echo get_template_directory_uri(); ?>/img/camera.png" alt="" class="ic"><br>カメラ</a>
  </div>
  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="<?php echo get_template_directory_uri(); ?>/img/map.png" alt="" class="ic"><br>地図</a>
  </div>
  <div class="ui-block-c">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="<?php echo get_template_directory_uri(); ?>/img/tweet.png" alt="" class="ic"><br>ツイート</a>
  </div>
</div>
<?php get_footer(); ?>

style.css が

/*
Theme Name: test
Theme URI: http://example.com/
Description: jquey theme test
Author: Gen snowpool
Author URI: http://example.com/
Version: 1.0

*/
img.ic{
  width: 64px;
  height: 64px;
}

となっています

次回からタイトルの変更、投稿テンプレの作成にとりかかります

home.php の作成

home.php の作成

wordpressでは表示するときに優先順位があり
それに基づきトップページが表示されます

基本的には
font-page > home > index
という順番になります

以前、作成した home.html をここへ
home.php としてコピーすると
リロードしたときに
index.php ではなく home.php が表示されます

これは、この優先順位が適用されるためです

なお、コピーしたときには画像のパスなどが
違っているので、アイコンが表示されません

homep

次回以降、home.php を改造していきます

現在の home.php のソースは以下のようになります

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>jQuery Mobile</title>
<link rel="stylesheet"
href="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js">
</script>
<style>
  img.ic{
    width: 64px;
    height: 64px;
  }
</style>
</head>
<body>
<div data-role="page" data-title="jQuery Mobile">
<div data-role="header">
 <h1>jQuery Mobile</h1>
</div>
<div role="main" class="ui-content">

<!-- 1行目 -->
<div class="ui-grid-b">
  <div class="ui-block-a">

    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/wa.png" alt="" class="ic"><br>和食</a>
  </div>
  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/chu.png" alt="" class="ic"><br>中華</a>
  </div>
  <div class="ui-block-c">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/yo.png" alt="" class="ic"><br>洋食</a>
  </div>
</div>

<!-- 2行目 -->
<div class="ui-grid-a">
  <div class="ui-block-a">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/cake.png" alt="" class="ic"><br>スイーツ</a>
  </div>

  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/ramen.png" alt="" class="ic"><br>ラーメン</a>
  </div>
</div>

<!-- 3行目 -->
<div class="ui-grid-b">
  <div class="ui-block-a">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/camera.png" alt="" class="ic"><br>カメラ</a>
  </div>
  <div class="ui-block-b">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/map.png" alt="" class="ic"><br>地図</a>
  </div>
  <div class="ui-block-c">
    <a class="ui-btn ui-corner-all" href="sweet.html"><img src="icon/tweet.png" alt="" class="ic"><br>ツイート</a>
  </div>
</div>

</div>
<div data-role="footer">
 フッター
</div>
</div>
</body>
</html>