ランキングプラグイン「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-->
参考資料 ※このまま貼り付けるとwpデバッグモードでエラーが出る
function.php
function my_custom_single_popular_post( $post_html, $p, $instance ){
$attachment_id = get_field('field_name',$p->id);//カスタムフィールド画像取得
$custom_img = $attachment_id['sizes']['画像名'];//カスタムフィールド画像出力
$custom_name = get_post_meta($p->id, 'field_name', true);//カスタムフィールド
$taxonomy_station = get_the_term_list( $p->id, 'taxonomy_name1', '', '', '');//ターム
$taxonomy_genre = get_the_term_list( $p->id, 'taxonomy_name2', '', '', '');//ターム
$taxonomy_budget = get_the_term_list( $p->id, 'taxonomy_name3', '', '', '');//ターム
$taxonomy_mood = get_the_term_list( $p->id, 'taxonomy_name4', '', '', '');//ターム
$time = get_the_date( $format, $p->id );
$custom_id= $p->id;
//$excerpt = get_post_field( post_content, $p->id, display );
$output = '
<div class="article_li">
<a href="' . get_the_permalink($p->id) . '">
<span class="article_li_imgbox">
<div class="imgbox"><img class="object-fit-img" src="'. $custom_img .'" alt=""></div>
</span>
<span class="article_li_date">'. $time .'</span><span class="article_li_icon">イタリアン</span>
<h2 class="article_li_title">' . esc_attr($p->title) . '</h2>
<span class="store_name">'. $custom_name .'</span>
</a>
<div class="hashtag_box">
<li class="hashtag">'.$taxonomy_station.'</li>
<li class="hashtag">'.$taxonomy_genre.'</li>
<li class="hashtag">'.$taxonomy_budget.'</li>
<li class="hashtag">'.$taxonomy_mood.'</li>
</div>
</div>
';
return $output;
}
add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );
front-page、archive
<?php
if (function_exists('wpp_get_mostpopular')){
$arg = array (
'range' => 'monthly',
'order_by' => 'views',
'stats_views' => '0',
'post_type' => 'article',
// 'taxonomy' => 'article_station',
// 'term_id' => ' 28',
'limit' => 12,
'wpp_start' => '<div class="article autoplay">',
'wpp_end' => '</div>',
);
wpp_get_mostpopular($arg);
}
?>