投稿ページにカテゴリごとのページネーションを追加する方法とカテゴリ内のページ総数と何番目の記事かを出力する方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
function get_post_number( $previous = false, $same_term = false, $taxonomy = 'category' ) { global $wpdb; if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) ) return null; $current_post_date = $post->post_date; $join = ''; $where = ''; if ( $same_term ) { $join .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id"; $where .= $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy ); if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) return null; $terms = get_the_terms( $post->ID, $taxonomy ); if ( $terms ) { $terms = wp_list_sort( $terms, array( 'term_id' => 'ASC' ) ); $term = $terms[0]; $where .= " AND tt.term_id = {$term->term_id}"; } } if ( is_user_logged_in() ) { $user_id = get_current_user_id(); $post_type_object = get_post_type_object( $post->post_type ); if ( empty( $post_type_object ) ) { $post_type_cap = $post->post_type; $read_private_cap = 'read_private_' . $post_type_cap . 's'; } else { $read_private_cap = $post_type_object->cap->read_private_posts; } $private_states = get_post_stati( array( 'private' => true ) ); $where .= " AND ( p.post_status = 'publish'"; foreach ( (array) $private_states as $state ) { if ( current_user_can( $read_private_cap ) ) { $where .= $wpdb->prepare( " OR p.post_status = %s", $state ); } else { $where .= $wpdb->prepare( " OR (p.post_author = %d AND p.post_status = %s)", $user_id, $state ); } } $where .= " )"; } else { $where .= " AND p.post_status = 'publish'"; } $op = $previous ? '<=' : '>='; $order = $previous ? 'ASC' : 'DESC'; $where = $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ); $sql = "SELECT COUNT(*) FROM $wpdb->posts AS p $join $where ORDER BY p.post_date $order"; $number = (int)$wpdb->get_var( $sql ); return $number; } |
1 2 3 4 |
<?php $chosen_id = $catid; // カテゴリID $thisCat = get_category($chosen_id); ?> |
1 2 3 |
<?php $number = get_post_number( false, true, 'category' ); ?> |
1 2 3 4 5 6 7 |
<div class="pagination"> <div class="prev"><?php next_post_link('%link', 'PREV', true) ?></div> <div class="page_num_wrap"><span class="page_num"><?php echo "$number"; ?>/<?php echo $thisCat->count; ?></span></div> <div class="next"> <?php previous_post_link('%link', 'NEXT', true) ?> </div> </div> |