【WordPress】よく使う関数の一覧10選
WordPressには多くの独自関数が用意されています。
同じ機能を持つ関数でも様々なものがあります。
今回の記事ではWordPressでWEBサイト制作をする際によく使う関数を10選としてご紹介します。
この記事の目次を表示
投稿記事の出力で使う関数
①タイトルを出力する
投稿記事のタイトルを出力するための関数は以下のものがあります。
- the_title()
- get_the_title()
the_title()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; endif; ?>
get_the_title()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo get_the_title(); ?>
<?php endwhile; endif; ?>
その他、詳しい使い方は以下の記事でご紹介しています。
②日付を出力する
投稿記事の投稿日を出力する関数は以下があります。
- the_date()
- get_the_date()
- the_time()
- get_the_time()
the_date()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_date('Y.m.d'); ?>
<?php endwhile; endif; ?>
get_the_date()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo get_the_date('Y.m.d'); ?>
<?php endwhile; endif; ?>
the_time()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_time('Y.m.d'); ?>
<?php endwhile; endif; ?>
get_the_time()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo get_the_time('Y.m.d'); ?>
<?php endwhile; endif; ?>
③コンテンツを出力する
投稿記事のコンテンツを出力するための関数は以下のものがあります。
- the_content()
- get_the_content()
the_content()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
get_the_content()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo get_the_content(); ?>
<?php endwhile; endif; ?>
その他、詳しい使い方以下の記事でご紹介しています。
④パーマリンクを出力する
投稿記事のパーマリンクを出力するための関数は以下のものがあります。
- the_permalink()
- get_permalink()
- get_the_permalink()
the_permalink()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>
get_permalink()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>
get_the_permalink()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a href="<?php echo get_the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; endif; ?>
その他、詳しい使い方は以下の記事でご紹介しています。
⑤サムネイル画像を出力する
投稿記事のアイキャッチ画像を出力するための関数は以下のものがあります。
- the_post_thumbnail()
- get_the_post_thumbnail()
the_post_thumbnail()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail('thumbnail'); ?>
<?php endwhile; endif; ?>
get_the_post_thumbnail()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo get_the_post_thumbnail('thumbnail'); ?>
<?php endwhile; endif; ?>
⑥投稿をIDする
投稿記事のIDを出力するための関数は以下のものがあります。
- the_ID()
- get_the_ID()
the_ID()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_ID(); ?>
<?php endwhile; endif; ?>
get_the_ID()
関数の基本的な使用方法は以下の通りです。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php echo get_the_ID(); ?>
<?php endwhile; endif; ?>
その他、詳しい使い方は以下の記事でご紹介しています。
テンプレートファイルの呼び出しで使う関数
⑦ヘッダーを呼び出す
テーマ内にあるheader.phpを呼び出すためには以下の関数を使用します。
- get_header()
get_header()
関数の基本的な使用方法は以下の通りです。
<?php get_header(); ?>
その他、詳しい使い方は以下の記事でご紹介しています。
⑧フッターを呼び出す
テーマ内にあるfooter.phpを呼び出すためには以下の関数を使用します。
- get_footer()
get_footer()
関数の基本的な使用方法は以下の通りです。
<?php get_footer(); ?>
その他、詳しい使い方は以下の記事でご紹介しています。
⑨サイドバーを呼び出す
テーマ内にあるsidebar.phpを呼び出すためには以下の関数を使用します。
- get_sidebar()
get_sidebar()
関数の基本的な使用方法は以下の通りです。
<?php get_sidebar(); ?>
⑩独自テンプレートファイルを呼び出す
テーマ内にある独自ファイルを呼び出すためには以下の関数を使用します。
- get_template_part()
get_template_part()
関数の基本的な使用方法は以下の通りです。
<?php get_template_part('original-part'); ?>
上の例ではテーマ内にあるoriginal-part.phpを呼び出しています。
まとめ
WordPressにはこの他にも様々な独自関数が用意されています。独自関数はwp-include
ディレクトリに格納されています。
これらを使用して、効率的にサイト制作を進めていきたいなと思いました。