この記事は最後に更新してから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
関連
お勧め
get_post_type_archive_template(2013年10月29日 更新)
string get_post_type_archive_template()
投稿タイプアーカイブのテンプレートを取得する。
get_comments(2018年5月27日 更新)
array get_comments( [ mixed $args = '' ] )
コメント情報を取得する。
wp_delete_file_from_directory(2018年7月6日 更新)
bool wp_delete_file_from_directory( string $file, string $directory )
パス名を確認した後でファイルを削除する。
get_post_mime_type(2014年11月16日 更新)
mixed get_post_mime_type( [ mixed $ID = '' ] )
添付ファイルのMIMEタイプを取得する。
remove_all_shortcodes(2018年5月27日 更新)
void remove_all_shortcodes( )
すべてのショートコードを削除する。