|
<?php |
|
//関連記事表示 |
|
|
|
function unset_private_post($plist,$post_id){ |
|
//非公開だけでなくリンク元と同じ記事IDやアーカイブページなども一応削除 |
|
foreach($plist as $rpost){ |
|
if(get_post_status($rpost) != 'publish' || $rpost == 0 || $rpost == $post_id){ |
|
unset($plist[$rpost]); |
|
} |
|
} |
|
$plist = array_values($plist); |
|
return $plist; |
|
} |
|
|
|
/* |
|
* 現段階ではとりあえず、 |
|
* 1.手動でどうしても表示させたい記事を表示 |
|
* 2.ランダムである程度関連する記事を表示(カテゴリなどで絞る) |
|
* 3.ランダム表示した中でクリック実績があるものは2.よりも優先的に表示 |
|
* という設計。 |
|
* ただ1.と3.で欄が埋まると表示内容が固定されがちなので、 |
|
* 最低1枠はランダムにするなどを考え中。 |
|
* 2.の処理を本文を解析してできる様になれば完璧 |
|
*/ |
|
|
|
function make_related_posts($post_id){ |
|
$max_num = 5; |
|
$count = 0; |
|
$plist = array(); |
|
$manual = get_post_meta($post_id,'related'); |
|
if($manual){ |
|
$plist = explode(',',implode($manual)); //implodeがないとexplodeでエラーが出る |
|
$plist = unset_private_post($plist,$post_id); |
|
$count = count($plist); |
|
} |
|
if($count < $max_num){ |
|
//データの保存の時点でどうにかしないとここの処理が遅くなる可能性 |
|
$clicked = explode(PHP_EOL,file_get_contents(ABSPATH.'lib/google/related_data.php')); |
|
for($j=0;$j<$data_count;++$j){ |
|
$ids = explode('-',$clicked[$j]); |
|
//違う記事のデータと、既にリストにある場合は飛ばす |
|
if($ids[0] != $post_id || in_array($ids[1],$plist)){continue;} |
|
array_push($plist,$ids[1]); |
|
} |
|
$plist = unset_private_post($plist,$post_id); |
|
$count = count($plist); |
|
} |
|
if($count < $max_num){ |
|
$year = date('Y'); |
|
$wp_query = new WP_Query(); |
|
$param = array( |
|
'posts_per_page' => $max_num, |
|
'post_type' => array('post','blog'), |
|
'post_status' => 'publish', |
|
'year' => $year.','.$year - 1, //今年or昨年の記事のみ表示 |
|
'orderby' => 'rand' |
|
); |
|
$wp_query->query($param); |
|
if($wp_query->have_posts()): while($wp_query->have_posts() && $count < $max_num) : $wp_query->the_post(); |
|
$id = get_the_ID(); |
|
if($id == $post_id || in_array($id,$plist)){continue;} |
|
array_push($plist,$id); |
|
endwhile; endif; wp_reset_postdata(); |
|
} |
|
|
|
$html = '<h3 id="relp">'.__('Related Posts',get_bloginfo('name')).'</h3><div class="list-group">'; |
|
//relpって何処かで使ってたか? |
|
$count = 0; |
|
foreach($plist as $rpost){ |
|
if($count >= $max_num){break;} |
|
$img = post_main_img($rpost,null,'medium',false); |
|
$html .= post_list_small($rpost,'RelatedPosts',"'".$post_id."-".$rpost."'",$img); |
|
++$count; |
|
} |
|
$html .= '</div>'; |
|
return $html; |
|
} |
|
?> |