アトリエロワ

functions.phpにショートコードを登録するwordpress

functions.phpに下記を記載

//TOPページ
add_shortcode('hurl', 'shortcode_hurl');
function shortcode_hurl() {
return home_url( '/' );
}
 
//月別アーカイブ
add_shortcode('arcive', 'shortcode_arcive');
function shortcod_arcive() {
return wp_get_archives( $args );
}
 
//カテゴリーアーカイブ
add_shortcode('cat_$id', 'shortcode_cat_$id');
function shortcod_cat_$id() {
return get_category_link($id);
}
 
//投稿タイプのアーカイブ
add_shortcode('post_$post_type', 'shortcode_post_$post_type');
function shortcod_post_$post_type() {
return get_post_type_archive_link( $post_type );
}
 
//親テーマ
add_shortcode('gtdu', 'shortcode_gtdu');
function shortcode_gtdu() {
return get_template_directory_uri();
}
 
//子テーマ
add_shortcode('gsdu', 'shortcod_gsdu');
function shortcod_gsdu() {
return get_stylesheet_directory_uri();
}

テンプレートファイルで使う

<img src="<?php echo do_shortcode( '[gtdu]' ); ?>/images/sample.jpg" alt="">

wpの管理画面の投稿・固定ページで使う

<a href="[hurl]">
<img src="[gsdu]/images/top.jpg" alt="">
</a>

トップページ

function shortcode_hurl() {
return home_url( '/' );
}
add_shortcode('hurl', 'shortcode_hurl');
<h3>テンプレート内の画像</h3>
function my_img_shortcode() {
  return get_template_directory_uri().'/images/';
}
add_shortcode('himg', 'my_img_shortcode');

HOME > wordpress設計術 > functions.phpにショートコードを登録するwordpress

wordpress設計術

wordpress設計術