Last active
December 14, 2015 01:28
-
-
Save lunaluna/5006075 to your computer and use it in GitHub Desktop.
【WordPress】テンプレートファイルへ記述するテンプレートタグ【超基礎編・だが紛らわしいもの】
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1.home_url - 現在のブログのホームURLを取得する | |
<?php $home = home_url( '/' ); ?> | |
現在のブログ(サイト)のホームURLを取得する。ホームURLは、管理者ページの「設定」-「一般」の「サイトのアドレス(URL)」のこと。 | |
WordPress 3.0より前のバージョンではget_option( 'home' )やget_bloginfo( 'home' )を使用していたが、3.0以降ではこの関数を使うほうが望ましい(たぶん)。 | |
2.site_url - 現在のブログのサイトURLを取得する | |
<?php $site = site_url( '/' ); ?> | |
現在のブログ(サイト)のサイトURLを取得する。サイトURLは、管理者ページの「設定」-「一般」の「WordPressのアドレス(URL)」のこと。 | |
3.get_bloginfo - サイト情報を『取得する』 | |
<?php $blogname = get_bloginfo( 'name' ); /*ブログタイトルを取得する。 */ ?> | |
<?php $blogdescription = get_bloginfo( 'description' ); /* キャッチフレーズを取得する。 */ ?> | |
4.bloginfo - サイト情報を『表示する』 | |
<?php bloginfo( 'name' ); /* ブログタイトルを表示する。 */ ?> | |
5.get_option - オプションの値を取得する | |
<?php $home = get_option( 'home' ); /* サイトのホームURLを取得する。 */ ?> | |
<?php $ppp = get_option( 'posts_per_page' ); /* 管理画面の表示設定で設定した1ページ内の投稿数を表示する。 */ ?> | |
6.get_template_directory_uri - 『テンプレート』のパス名(URI)を取得する | |
<?php $template_uri = get_template_directory_uri(); /* 現在のアップロードディレクトリ(パス名)を取得する。 */ ?> | |
get_bloginfo( 'template_directory' )で取得できるURIと同じ。 | |
7.get_template_directory - 『テーマ』のパス名を取得する | |
<?php $path = get_template_directory( ); /* テーマのパス名を取得する。 */ ?> | |
get_bloginfo( 'template_url' )で取得できるURIと同じ。 | |
親テーマを定義している場合、使用中の『親テーマの』ディレクトリパス。 | |
8.get_stylesheet_directory_uri - 現在の『テーマのURL』を取得する | |
<?php $style_path = get_stylesheet_directory_uri( ); ?> | |
末尾に'/'は付与されない。 | |
標準的なテーマの場合、このget_stylesheet_directory_uri関数で取得できる値と、get_template_directory_uri関数で取得できる値は同じになるが、現在のテーマが子テーマの場合は異なるURLになる。 | |
そのため、確実に現在のテーマのURLを取得する場合は、get_template_directory_uri関数ではなく、get_stylesheet_directory_uri関数を使う。 | |
9.get_stylesheet_directory - 現在の『テーマのパス名』を取得する | |
<?php $style_path = get_stylesheet_directory( ); ?> | |
末尾に'/'は付与されない。 | |
標準的なテーマの場合、このget_stylesheet_directory関数で取得できる値と、get_template_directory関数で取得できる値は同じになるが、現在のテーマが子テーマの場合は異なるパスになる。 | |
そのため、確実に現在のテーマのパス名を取得する場合は、get_template_directory関数ではなく、get_stylesheet_directory関数を使う。 | |
10.get_stylesheet - 現在のテーマ名を取得する | |
<?php $theme = get_stylesheet( ); ?> | |
基本的にはget_option( 'stylesheet' )で取得した値となる。 | |
11.get_stylesheet_uri() - 現在の『テーマのスタイルシートのURI』を取得する | |
<?php $uri = get_stylesheet_uri(); ?> | |
URLを出力するには <?php echo get_stylesheet_uri(); ?> と記述。 | |
基本的にはget_bloginfo( 'stylesheet_url' )で取得した値となる。 | |
テーマが子テーマであってもスタイルシートのURLを正しく返す。 |
【参照】
get_stylesheet_uri – WordPress ファンクションタグのリファレンス
http://lovelog.eternal-tears.com/function-tag/theme-related/f-tother/get_stylesheet_uri/
【参照】
WordPressでURLを取得する関数あれこれをまとめた。
http://firegoby.jp/archives/3037
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
【参照】
WordPress私的マニュアル : 関数
http://elearn.jp/wpman/function