参考
http://s2727786.epressd.jp/
手順
テーマフォルダのfunctions/customの下に、
以下のphpファイルを作る
1. ファイル名
正直何でもいいが、ファイル名で
関連スラッグがわかるのが好ましい
shortcode-【スラッグ】.php
2. ファイル内容
<?php
add_shortcode( ‘【スラッグ】‘, function($atts) {
extract(shortcode_atts( array(
‘slug‘ => ‘【適用したいカテゴリのスラッグ】‘,
‘per‘ => ‘10‘,
), $atts ));
$paged = get_query_var(‘paged‘);
$args = array(
‘posts_per_page‘ => $per,
‘paged‘ => $paged,
‘category_name‘ => $slug
);
$the_query = new WP_Query( $args );
if($the_query->have_posts()) {
$html = ‘<div class=”sp-slides”>‘;
while ( $the_query->have_posts() ) : $the_query->the_post();
$image_id = get_post_thumbnail_id ();
$image_url = wp_get_attachment_image_src ($image_id, true);
$html .= ‘<div class=”sp-slide”>
<a href=”‘.get_the_permalink().‘“><img src=”‘.$image_url[0].‘” alt=”WORKS施工事例” class=”sp-image” /></a>
<div class=”sp-caption”>
<h3 class=”notstyle”><a href=”‘.get_the_permalink().‘” style=”color: #000;”>‘.get_the_title().‘</a></h3>
<p>‘.get_the_excerpt().‘</p>
</div>
</div>‘;
endwhile; wp_reset_postdata();
$html .= ‘</div>‘;
return $html;
}
} );
※【適用したいカテゴリのスラッグ】の箇所には
以下のように書くと複数カテゴリが登録可能
extract(shortcode_atts( array(
‘slug’ => ‘new, reform, construction’,
‘per’ => ’10’,
), $atts ));
3. ショートコード埋め込み
トップなどのコンテンツエディタ内で以下のように書くだけ
<section>
<div class=“slider-pro“ data-type=“contents“ data-width=“450“ data-height=“300“>
[【スラッグ】]
</div>
</section>