wordpressで「今見ている記事の著者が書いた次の記事」のリンクが欲しかったのだけど
そんなテンプレートタグは無かった。
外人さんがやってた。
https://wordpress.org/support/topic/previous-next-post-navigation-by-author/
※$wpdb->prepareの仕様が変わったので、そのままは使えない。
ので、作った。
//↓ここから function.phpにコピペ /////////////////////////////////////
function prenex_by_author($prenex,$str=""){
global $wpdb, $post;
if($prenex == "pre"){
$target = $wpdb->get_row($wpdb->prepare("
SELECT ID, post_title
FROM $wpdb->posts
WHERE post_type='post'
AND post_status='publish'
AND post_author= %s
AND post_date post_author
,$post->post_date
));
}elseif($prenex == "nex"){
$target = $wpdb->get_row($wpdb->prepare("
SELECT ID, post_title
FROM $wpdb->posts
WHERE post_type='post'
AND post_status='publish'
AND post_author= %s
AND post_date > %s
ORDER BY post_date ASC
LIMIT 1;"
,$post->post_author
,$post->post_date
));
}
if($target){
if(!$str){$str = $target->post_title;}
echo 'ID).'">'.$str.'';
}
}
//↑ここまで function.phpにコピペ /////////////////////////////////////
//使い方
//prenex_by_author($prenex,$str)
// $prenex 前か次を「pre」か「nex」で指定。省略時は何も出力しない。
// $str リンクを貼る文字列。省略時は記事タイトル。
//直接aタグを出力する。次や前の記事が無い場合は何も出力しない。
//使用例
prenex_by_author("pre");
prenex_by_author("nex",'次の記事');
