0
1

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-20

はじめに

・WordPres初心者〜中級者。
・WordPressのカスタムテンプレートを作成したい方。
・WordPressのテンプレートタグについて、よく分からない方

テンプレートタグとは

テンプレートタグとは**「blgoinfo('パラメータ')」**のような記述をし、管理画面で作成した値など(サイトのタイトルや、サイトのキャッチフレーズ)をパラメータに応じて表示するための、WordPessで使用できる関数の一種です。

テンプレートタグのパラメータ

テンプレートタグには、あらかじめパラメータが用意されています。パラメータを設定することで、そのパラメータに応じた値を表示できます。

※以下は、パラメータをまとめたものです。

パラメータ 詳細
name サイトのタイトル
description サイトのキャッチフレーズ
サンプルコード(サイトのタイトルを表示)
index.php
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>WordPressテンプレートタグ</title>
	<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css">
</head>
<body>
	<div class="header_name">

		<!-- パラメータをnameに設定し、サイトのタイトルを表示 -->
		<h1><?php bloginfo('name')?></h1>
	</div>
	<img src="<?php echo get_template_directory_uri(); ?>/images/wordpress.jpg">
</body>
</html>
style.css
/*
Theme Name : WordPress
*/

.header_name {
	text-align: center;
}

img {
	width: 100%;
}

※実行結果
スクリーンショット 2021-04-20 19.07.46.png

サンプルコード(サイトのキャッチフレーズを表示)
index.php
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>WordPressテンプレートタグ</title>
	<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css">
</head>
<body>
	<div class="header_description">

		<!-- パラメータをdescriptionに設定し、サイトのキャッチフレーズを表示 -->
		<h1><?php bloginfo('description')?></h1>
	</div>
	<img src="<?php echo get_template_directory_uri(); ?>/images/wordpress.jpg">
</body>
</html>
style.css
/*
Theme Name : WordPress
*/

.header_description {
	text-align: center;
}

img {
	width: 100%;
}

※実行結果
スクリーンショット 2021-04-20 19.10.07.png

その他のテンプレートタグ

その他の主なテンプレートは以下になります。

テンプレートタグ パラメータ 詳細
home_ur path, scheme(pathは、ホームURLからの相対パス(省略可)。schemeは、http,httpsなど(省略可)) トップページのリンクを取得
body_class クラス名の文字列など。(省略可) bodyタグと異なるクラスを、WordPressのルールに基づいて割り当てる
サンプルコード(トップページのリンクを取得)
index.php
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>WordPressテンプレートタグ</title>
	<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css">
</head>
<body>
	<div class="header_home_url">

		<!-- home_urlを使用し、トップページのリンクを表示 -->
		<h1><a href="<?php echo home_url();?>">トップページのリンク</a></h1>
	</div>
	<img src="<?php echo get_template_directory_uri(); ?>/images/wordpress.jpg">
</body>
</html>	
style.css
/*
Theme Name : WordPress
*/

.header_home_url {
	text-align: center;
}

img {
	width: 100%;
}

※実行結果
スクリーンショット 2021-04-20 19.02.10.png

サンプルコード(bodyタグのclass属性を表示)
index.php
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>WordPressテンプレートタグ</title>
    <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css">
</head>

<!-- body_classを使用し、bodyタグのclass属性を表示 -->
<body><?php body_class(); ?>>
    <img src="<?php echo get_template_directory_uri(); ?>/images/wordpress.jpg">
</body>
</html> 
style.css
/*
Theme Name : WordPress
*/

body {
    text-align: center;
    font-size: 45px;
}

img {
    width: 100%;
}

※実行結果
スクリーンショット 2021-04-20 22.01.14.png

まとめ

・テンプレートタグとはWordPress関数の一種で、パラメータに応じて値を表示したり、取得することができる。

・**「blgoinfo('パラメータ')」は、サイトのタイトルやキャッチフレーズを表示する。パラメータは、namedescription**になる。

・**home_url**は、トップページのリンクを取得できる。

・**body_class**はbodyタグと異なるクラスを、WordPressのルールに基づいて割り当てることができる。

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?