ランキングプラグイン「WordPress Popular posts」の出力にカスタムフィールド(Advanced Custom Fields)の値や画像を含めたい。
function.phpの記述
/* ---------------------------------------------------------------------------------------
◆◆◆ WordPress Popular posts カスタマイズ ###########################################
------------------------------------------------------------------------------------------*/
function my_custom_single_popular_post( $post_html, $p, $instance ){
$attachment_id = get_field('02item-image1',$p->id);//カスタムフィールド画像取得
$size = "img320"; //画像サイズ (thumbnail, medium, large, full or custom size) img320はカスタムサイズ
$image = wp_get_attachment_image_src( $attachment_id, $size );
$acf1 = get_post_meta($p->id, 'field_name', true);//カスタムフィールドその1
$acf2 = get_post_meta($p->id, 'field_name', true);//カスタムフィールドその2
$custom_id= $p->id;//id番号出力
$output = '
<li><a href="' . get_the_permalink($p->id) . '"><img src="'. $image[0] .'" alt=""/><br />
' . esc_attr($p->title) . '<br />'. $acf1 .'</a>
</li>
';
return $output;
}
add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );
ランキング表示
例:カスタム投稿タイプ items カスタムタクソノミー items_category タームid 28
<div id="rankingS" class="inner clearfix">
<?php
if (function_exists('wpp_get_mostpopular')){
$arg = array (
'range' => 'all',//集計期間
'order_by' => 'views',
'stats_views' => '0',
'post_type' => 'items',//投稿タイプ
'taxonomy' => 'items_category',//カスタムタクソノミー
'term_id' => ' 28',タームid
'limit' => 4,//表示件数
'wpp_start' => '<ul class="ranking">',//開始タグ
'wpp_end' => '</ul>',//終了タグ
);
wpp_get_mostpopular($arg);
}
?>
</div><!--/rankingS-->
(続きを読む…)