WordPress Snippet

ワードプレスをカスタマイズしよう

シングルページのナビゲーションリンク prev next を著者に限定する

functions.php

function get_prev_post_by_author( $link= "%link", $title= "%title" ) {
 global $wpdb, $post;
 $prev= $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 DESC LIMIT 1;", $post->post_author, $post->post_date ) );
 if ( $prev ) {
 $title= preg_replace( '/%title/', $prev->post_title, $title );
 echo preg_replace( '/%link/', '<span class="alignleft"><a href="' . esc_url( get_permalink( $prev->ID ) ) . '" rel="prev" class="prev-post-by-author alignleft">&laquo; ' . wp_kses( $title, array() ) . '</a></span>', $link );
 }
}
 
function get_next_post_by_author( $link= "%link", $title= "%title" ) {
 global $wpdb, $post;
 $next= $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 ( $next ) {
 $title= preg_replace( '/%title/', $next->post_title, $title );
 echo preg_replace( '/%link/', '<span class="alignright"><a href="' . esc_url( get_permalink( $next->ID ) ). '" rel="next" class="next-post-by-author">' . wp_kses( $title, array()) . ' &raquo;</a></span>', $link );
 }
}

single.phpに

	get_prev_post_by_author();
	get_next_post_by_author();

[emulsion_relate_posts]