この記事は最後に更新してから1年以上経過しています。
説明
投稿記事の中で画像パーツを使いたいときがある。画像パーツの配置場所は、/imgディレクトリのようなテーマ外の場合と、/wp-content/themes/mytheme/imagesディレクトリのようなテーマ内の場合があり、今回は後者のケース。ショートコードを使って投稿記事の画像ファイルのURLを記述する方法を紹介したい。 通常、投稿記事にテーマ内の画像ファイルを入れる場合、<img src="/wp-content/themes/mytheme/images/logo.gif" />
<img src="http://localhost/wp-content/themes/mytheme/images/logo.gif" />
<img src="[tp /]/images/logo.gif" />
<img src="[tp]/images/logo.gif[/tp]" />
add_shortcode( 'tp', 'shortcode_tp' );
function shortcode_tp( $atts, $content = '' ) {
return get_template_directory_uri().$content;
}
最終更新 : 2018年05月27日 10:53
関連
お勧め
wp_print_inline_script_tag(2021年4月19日 更新)
void wp_print_inline_script_tag( string $javascript [ , array $attributes = array() ] )
インラインJavaScriptを含むscript要素を出力する。
register_rest_route(2022年8月17日 更新)
bool register_rest_route( string $namespace, string $route [ , array $args = array() [ , bool $override = false ] ] )
REST APIのルートを登録する。
wp_determine_option_autoload_value(2024年7月22日 更新)
string wp_determine_option_autoload_value( string $option, mixed $value, mixed $serialized_value, boo l |string $autoload )
自動ロードのトリガー値を取得する。
single_tag_title(2018年5月27日 更新)
string single_tag_title( [ string $prefix = '' [ , bool $display = true ] ] )
投稿タグアーカイブページの投稿タグ名を取得し、パラメータ$displayがtrueならば表示する。$displayがfalseの場合は、文字列として返す。
home_url(2023年3月31日 更新)
string home_url( [ string $path = '' [ , string $scheme = null ] ] )
現在のブログ(サイト)のホームURLを取得する。ホームURLは、管理者ページの「設定」-「一般」の「サイトのアドレス(URL)」のこと。