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 3 years have passed since last update.

wordpress学習中

Last updated at Posted at 2021-04-06

この記事の概要

wordpressを学習のアウトプットとして記述していきます。

学習方法

2021/03現在
参考書:wordpressの教科書を用いて学習中

環境構築

virtualBOXをインストール
kusanagiを利用

hostsファイル

ローカルPCの環境で
ホストネームとIPアドレスを指定して、紐付ける設定ファイル
hostsファイルでの設定はDNSファイルより優先される

DNS

Domain Name System

ドメインネームとIPアドレスを紐付ける機能

FTP

File Transfar Protocol
ファイル転送の約束事

wordpressのテーマとして認識されるには

index.phpファイルとstyle.cssファイルが必要

テンプレートファイルの分割

index.phpだけでもテンプレートとして機能するが、header部、footer部など、部分ごとにテンプレートファイルを分割できる。
可読性、保守性を向上させることができる。

index.phpファイルとは別にheader.phpファイル、footer.phpファイルなどを用意して、
header.phpならヘッダー部の記述、footer.phpならフッター部の記述をする。
index.phpで上記ファイルを読み込みたい箇所に、
<?php get_header(); ?>
<?php get_footer(); ?>
のように取得する関数を記述する。

※テンプレートはwordpressで定められているもので、上記のヘッダー、フッター以外にもたくさんある。
※テンプレートを取得する関数はwordpressで用意しているもの。

テンプレートファイルの呼び出し方法

テンプレートタグの記述方法は数種類に置き換えが可能。

例)headerの場合
パターン1 (テンプレートごとの決まったテンプレートタグ)
<?php get_header(); ?>

パターン2 (引数にテンプレートファイル名を与えて読み込む)
<?php get_template_part('header'); ?>

例)footer-topの場合
パターン1 (テンプレートが分岐している時、引数に"-"以降を与える)
<?php get_footer('top'); ?>

パターン2 (引数にテンプレートファイル名を与えて読み込む)
<?php get_template_part('footer-top'); ?>

子テーマの作成、編集

他テンプレート親として利用して、別テーマを作成編集する方法

・親と同じ階層に新規ディレクトリを作成
・作成したディレクトリにindex.phpファイル、style.cssファイル、functions.phpファイルを作成
index.phpファイルの内容は親テンプレートのものをそのまま代用

style.cssファイルには以下を記述
/* 
Theme Name: Twenty Nineteen Child
Author: the WordPress team
Template: twentynineteen
Description: Twenty Nineteenの子テーマです。
Version: 1.0
*/

- style.cssファイルの内容 -
Theme Name: テーマ名
Author: テーマの著者
Template: 親テンプレート名
Description: テーマの説明
Version: テーマのバージョン


functions.phpファイルには以下を記述

<?php
add_action( 'wp_enqueue_scripts','theme_enqueue_styles');
function theme_enqueue_styles(){
  wp_enqueue_style('parent-style',get_template_directory_uri(). '/style.css');
  wp_enqueue_style('child-style',get_stylesheet_directory_uri(). '/style.css',array('parent-style'));
}

- functions.phpの内容 -
wp_enqueue_scriptsをフックに


投稿と固定ページ

ファイル名の優先順位

ファイル名には優先順位があり、index.phpファイルは最後に呼び出される。

トップページなら、front-page.php
固定ページなら、page.php

など、各表示箇所によってindex.phpより優先されるファイル名がある。

カスタムメニュー

カスタムメニューという機能を使う事で、固定ページやカテゴリーで用意したページ情報をメニュー部に利用できる。
グローバル、フッターなどにメニューとして挿入できる。

利用には、header.phpファイルのメニューに該当する部分
<nav></nav>等の部分を以下のように代替する。

globalメニューの場合.

<?php 
wp_nav_menu(
  array(
    'theme_location' => 'place_global',
    'container' => false,
  )
); 
?>

footerメニューの場合.
<?php
wp_nav_menu(
  array(
    'theme_location' => 'place_footer',
    'container' => false,
  )
);
?>

投稿ページを表示させる

single.phpファイルは投稿ページを表示させるために必要
single.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?