LoginSignup
2
2

More than 5 years have passed since last update.

[WordPress]ショートコードの中でget_template_partを使ってテンプレート呼び出す

Last updated at Posted at 2015-09-24

ショートコードの中で

echo "<div>hogehoge</div>"

みたいなのをたくさん書くのはイマイチなので

functions.php
add_shortcode('items', function(){
ob_start(); //これがないと投稿の一番上に表示されてしまう
get_template_part('item_template');
return ob_get_clean(); //これがないと投稿の一番上に表示されてしまう
});
item_template.php
<div class="item">
</div>

ob_start()とob_get_clearn()使うのは少し行儀悪いが
このほうがロジックとHTMLを分離できてよい。

参考

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2