この記事は最後に更新してから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
関連
お勧め
add_magic_quotes(2020年8月13日 更新)
array add_magic_quotes( array $array )
配列の文字列をスラッシュでクォートする。
wp_parse_id_list(2019年2月23日 更新)
array wp_parse_id_list( array | string $list )
IDのリストをパースする。
wp_mkdir_p(2014年11月27日 更新)
bool wp_mkdir_p( string $target )
ディレクトリを再帰的に作成する。
get_object_taxonomies(2018年5月27日 更新)
array get_object_taxonomies( mixed $object [ , string $output = 'names' ] )
投稿タイプのタクソノミー情報を取得する。
add_media_page(2022年6月27日 更新)
mixed add_media_page( string $page_title, string $menu_title, mixed string $capability, string $menu_slug [ , mixed $function = '' [, int $position = null ] ] )
メディアメニューにサブメニューを登録する。