記事IDをショートコードで指定して、アイキャッチ、タイトル、抜粋を表示します
ショートコードの学習かねて作成したものです。
記事IDと記事抜粋の長さを指定します。
[adsense]
ショートコードと表示例
[[sbh_recmd id=2055 length=114]]
id:記事ID
length:抜粋の長さ(本文から抜き出し)
[sbh_recmd id=2055 length=114]
使い方
以下のソースをfunctions.phpに追加します。
表示したい記事IDと抜粋の長さをそれぞれid、lengthに指定します。
ソース
function sbh_recommend($atts) {
extract(shortcode_atts(array(
'id' => get_the_ID(),
'length' => 114,
), $atts));
/* Get Permlink */
$permalink = get_permalink( $id );
if ( ! $permalink ) {
return false;
}
/* Get Thumblink */
$thumbnailink = get_the_post_thumbnail( $id,'large');
if ( ! $thumbnailink ) {
return false;
}
/* Get Title */
$title = get_the_title( $id );
/* Get content & Make description */
$descript = get_post_field( 'post_content', $id );
$descript = wp_strip_all_tags( $descript );
$descript = strip_tags( strip_shortcodes( $descript ) );
$descript = str_replace(" ","",$descript);
$descript = mb_substr($descript,0,$length);
return '<style>.sbhbox{border:1px solid #666666;padding:10px;margin:5px;}.sbhtitle{font-size:17px;font-weight:600;text-align:center;padding:0;margin-bottom:5px;}.sbhdescription{padding:8px;text-align:left;}</style><div class="sbhbox"><a href="'.$permalink.'">'.$thumbnailink.'</a>'.'<p class="sbhtitle"><a href="'.$permalink.'">'.$title.'</a></p><p class="sbhdescription">'.$descript.'...</p></div>';
}
add_shortcode('sbh_recmd', 'sbh_recommend');


コメント