Solarized
HOME > WEBプログラム覚書 > [WordPress] 次の投稿と前の投稿を取得する。
[WordPress] 次の投稿と前の投稿を取得する。
次の投稿とか前の投稿を取得するget_adjacent_post()が、どうもループ内じゃないと利用できないようになっている模様。
get_adjacent_post()はループの際に作成されるグローバル変数の$postを使うようになっているので、
どの投稿を使うかこちらで決めれるようにちょっとだけ変更してみる。
functions.php
- <?php
-
- @since
- @see
- @param
- @param
- @param
- @param
- @return
- function my_get_adjacent_post($postdata = null, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
- global $wpdb;
-
- if (is_null($postdata)) {
- global $post;
- } else {
- if (is_numeric($postdata)) {
- $post = get_post($postdata = (int) $postdata, OBJECT);
- } elseif ($postdata instanceof stdClass && is_propety('post_type', $postdata) && $postdata->post_type === 'post') {
- $post = $postdata;
- }
- }
-
- if ( empty( $post ) )
- return null;
-
- $current_post_date = $post->post_date;
-
- $join = '';
- $posts_in_ex_cats_sql = '';
- if ( $in_same_cat || ! empty( $excluded_categories ) ) {
- $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";
-
- if ( $in_same_cat ) {
- $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
- $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
- }
-
- $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
- if ( ! empty( $excluded_categories ) ) {
- if ( ! is_array( $excluded_categories ) ) {
-
- if ( strpos( $excluded_categories, ' and ' ) !== false ) {
- _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
- $excluded_categories = explode( ' and ', $excluded_categories );
- } else {
- $excluded_categories = explode( ',', $excluded_categories );
- }
- }
-
- $excluded_categories = array_map( 'intval', $excluded_categories );
-
- if ( ! empty( $cat_array ) ) {
- $excluded_categories = array_diff($excluded_categories, $cat_array);
- $posts_in_ex_cats_sql = '';
- }
-
- if ( !empty($excluded_categories) ) {
- $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
- }
- }
- }
-
- $adjacent = $previous ? 'previous' : 'next';
- $op = $previous ? '<' : '>';
- $order = $previous ? 'DESC' : 'ASC';
-
- $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
- $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
- $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
-
- $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
- $query_key = 'adjacent_post_' . md5($query);
- $result = wp_cache_get($query_key, 'counts');
- if ( false !== $result )
- return $result;
-
- $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
- if ( null === $result )
- $result = '';
-
- wp_cache_set($query_key, $result, 'counts');
- return $result;
- }
- ?>
これでループ作らなくても利用できる。
functions.php
- <?php
- $next = my_get_adjacent_post(5, false, false, false);
- $prev = my_get_adjacent_post(5);
-
- echo $next->title;
- echo $prev->title;
- ?>
それにしてもループ内でしか使えないとかおかしいので、実はちゃんととれる関数があるかもしれない。
| 投稿日 |
2012年1月12日 01:03 |
| カテゴリ |
PHP |
| タグ |
WordPress | カスタマイズ | 関数 |
| トラックバック URL |
http://www.kantenna.com/cgi-bin/mt504/mt-tb.cgi/1252 |
2012年1月14日 05:55
テンプレートのごちゃごちゃが限界にきたのでラッパー書いてみた。でも僕なんかではなくもっとデキる人が書いたものを使いたいw続きを読む
2012年2月 1日 03:57
会員制サイトなどで不特定多数の人にログインさせる場合、フッタのバージョン情報は隠しておきたかったり、
こじゃれたメッセージを表示したいときとか使うアクショ...続きを読む