• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

インクルードタグ

提供: WordPress Codex 日本語版
2008年3月8日 (土) 17:59時点におけるBono (トーク | 投稿記録)による版 (en:Include Tags 2007年2月16日 (金) 00:34 Raddaqii 版)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

The Template include tags are used within one Template file (for example index.php) to execute the HTML and PHP found in another template file (for example header.php). PHP has a built in include() statement for this purpose, but these WordPress template tags make including certain specific files much easier.

See Using Themes and Theme Development for more information about Templates and Themes.

The Header Template

<?php get_header(); ?>

This tag includes the file header.php from your current theme's directory. If that file is not found, it will instead include wp-content/themes/default/header.php.

The Footer Template

<?php get_footer(); ?>

This tag includes the file footer.php from your current theme's directory. If that file is not found, it will instead include wp-content/themes/default/footer.php.

The Sidebar Template

<?php get_sidebar(); ?>

This tag includes the file sidebar.php from your current theme's directory. If that file is not found, it will instead include wp-content/themes/default/sidebar.php.

<?php get_sidebar('right'); ?>

Causes the template TEMPLATEPATH . 'sidebar-right.php' to be included. Note: the ability to specify a particular sidebar, meaning more than one sidebar template can be used in a theme, was added with Version 2.5.

The Comments Template

<?php comments_template(); ?>

This tag includes the file comments.php from your current theme's directory. If that file is not found, it will instead include wp-content/themes/default/comments.php. To display comments on the main index or archive pages, you'll need to set the $withcomments variable to "1" before calling this tag.

Including Any Template

WordPress offers the above tags for including those specific Templates, but there is also a convenient way to include any file. To do so, you will need to use the include PHP function, and a constant WordPress conveniently defines for you to make things easy: TEMPLATEPATH.

Suppose you want to include a file called header2.php. Just insert the following line in your template where you want that file's information to appear.

<?php include (TEMPLATEPATH . '/header2.php'); ?>

You could, for example, use this as a means of including a different header instead of the normal header.php which would be included with get_header().

NOTE: TEMPLATEPATH is a reference to the absolute path to the current theme directory (without the / at the end). For information on referencing URIs rather than including files, see Referencing Files From a Template.

Example

The following is a very simple example of a template for an "HTTP 404: Not Found" error (which you could include in your Theme as 404.php).

<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Parameters

These tags do not accept any parameters.


関数リファレンステンプレートタグ目次もご覧ください。