テンプレートタグ まとめ

bloginfo(); ブログの各種情報

get_header(); ヘッダーテンプレートを読み込む

get_footer();  フッターテンプレートを読み込む

get_sidebar(); サイドバーテンプレートを読み込む

the_title(); 投稿のタイトル

<?php the_title(‘<h2>’,’</h2>’); ?>

出力せずに受け取るには、<?php $title=the_title(”,”,false);  ?>

the_content(); 投稿の内容

続きを読むの設定

<?php the_content(‘投稿”‘.the_title(”,”,’false’).”の続きを読む”); ?>

the_time(); 投稿の日時

the_permalink(); 投稿のアドレス

タイトルにリンクを貼る場合

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

the_category(); 投稿のカテゴリー

the_tags(); タグの情報を出力

<?php the_tags(‘タグ:<ul class=”post-tags”><li>’,’</li><li>’,’</li></ul>’); ?>

前後のページへリンクを貼る

<?php previous_post_link(); ?><?php next_post_link(); ?>

the_author(); ユーザー

wp_list_categories(); カテゴリーの一覧

wp_get_archives(); アーカイブの一覧

wp_list_pages(); 固定ページの一覧

 

ワードプレス・ループ

投稿があるか判断

have_posts();

パラメータ無し。

投稿を読み込み、出力準備を整える

the_post();

パラメータ無し。

※$post という変数

現在操作対象となっている投稿は「$post」という変数に格納されます。the_post();はこの$postを準備してくれる必須関数なのです。

基本的なループ処理

<?php if(have_posts()): ?>

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

<?php  the_title(‘<h2>’,’</h2>’); ?>

<?php the_content(); ?>

<?php endwhile; ?>

<?php else:  ?>

投稿がありませーん。

<?php endif; ?>

テンプレートの組込関数

ヘッダー部分を組み込む

get_header();

複数のヘッダー部分の使い分け

get_header(‘接尾語’);

例:archive.php → header-archive.php

get_header(‘archive’);

フッターを組み込む

get_footer();

同じく、get_footer(‘archive’);でarchive.phpのみに特定のフッターをつけることができる。

サイドバーを組み込む

get_sidebar();

検索フォームテンプレートを組み込む

get_search_form();

search.phpを読み込みます。

任意のテンプレートを組み込む

get_template_part(”);

common.phpを読み込むには

get_template_part(‘common’);