【WP】phpコードをショートコード化できるプラグイン「Post Snippets」

前回の記事で、
function.phpでショートコードを自作する方法をご紹介しました。
今回は、ショートコードを作成できるプラグイン
「Post Snippets」をご紹介します!
プラグインのインストール・有効化

ショートコードを登録
WordPress管理画面 > 設定 > Post Snippets
と進みます。

①「Add New Snippets」ボタンをクリック
②ショートコード名を入力
③コードを入力
④「Shortcode」にチェックを入れる
phpコードをショートコード化する場合は「PHP Code」にもチェックを入れる
⑤「Update Snippets」ボタンをクリックして登録完了!
<phpコードを入力する際の注意点>
phpコードの
最初の <? と最後の ?> は消す!
例:投稿一覧をショートコードとして登録する場合
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$loop = array( 'post_type' => 'post', 'posts_per_page' => 5 ); $the_query = new WP_Query( $loop ); if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();?> <article> <a href="<?php the_permalink();?>"> <div><img src="<?php echo catch_that_image_thumb(); ?>" alt="<?php the_title(); ?>" /></div> <time><?php the_time(('Y.m.d')); ?></time> <h3><?php the_title(); ?></h3> </a> </article> <?php endwhile;?> <?php else: ?> <p>記事がありません</p> <?php endif; |
ショートコードを呼び出す
投稿、固定ページに移動します。
ショートコードを呼び出したい位置にカーソルを合わせ、エディタの以下のメニューをクリック
・ビジュアルエディタの場合

・テキストエディタの場合


ショートコードを選択し「Insert」ボタンをクリック

ショートコードの読み込み完了!
以上となります。
定型文や簡素な関数をショートコード化したい時は
前回ご紹介したfunction.phpでショートコードを自作する方法で登録、
少し複雑なコードを使いたい時やfunction.phpに多数の記述を追記したくない場合は今回紹介したプラグイン「Post Snippets」を使うなど、状況に応じて使いわけてみてください。
![MARKLEAPS[マークリープス]](https://markleaps.com/blog/wp-content/themes/mkl/images/00_logo.png)

![[WordPress] プラグイン「WP ALL Import」で既存記事を一括更新(CSV上書き)する方法](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-all-import_update-500x254.jpg)
![[WordPress] CSVで記事を一括インポート-プラグイン「WP ALL Import」の使い方](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-all-import-500x254.jpg)
![[WordPress] 記事への画像挿入方法](https://markleaps.com/blog/wp-content/uploads/2025/10/pic_manual_mainimg-500x254.png)
![[WordPress] カテゴリーとタグの違いと設定方法](https://markleaps.com/blog/wp-content/uploads/2025/10/wp-category-tag-500x254.jpg)