ワードプレスには組み込まれているリストボタンがない。ブログで出発したためか分からないが、とにかく大間で簡単にリスト表示を実装してみましょう。

postの場合とcustom post typeの両方に対応できるように組んでみた。

if( ! function_exists('fu_get_list_url')){
    /**
     * post形式文のsingleページに月のリストボタンのリンクを返します。
     * 1ページあたりの文の数は、ワードプレスを読むの設定と仮定して計算する。
     * 自動的にtaxonomyとtermを取得してリンクを作成します。
     * 複数のtaxonomyと、複数のtermに属している文である場合にはワードプレスが返した配列の最初のやつを選択してリンクを返す。
     * @param null $taxonomy
     * @param null $term
     * @return string|void
     */
    function fu_get_list_url($taxonomy = NULL, $term_slug = NULL){
        global $post, $table_prefix;

        if( ! $taxonomy){
            $taxonomies = get_post_taxonomies();
            $taxonomy = $taxonomies[0];
        }

        if( ! $term_slug){
            $terms = wp_get_post_terms($post->ID, $taxonomy);
            $term = $terms[0];
        }else{
            $term = get_term_by('slug', $term_slug, $taxonomy);
        }

        if($term){
            $sql = "SELECT SQL_CALC_FOUND_ROWS {$table_prefix}posts.ID
                FROM {$table_prefix}posts
                INNER JOIN {$table_prefix}term_relationships ON ( {$table_prefix}posts.ID = {$table_prefix}term_relationships.object_id )
                WHERE 1 =1
                AND (
                {$table_prefix}term_relationships.term_taxonomy_id
                IN ( $term->term_id )
                )
                AND {$table_prefix}posts.post_type = '{$post->post_type}'
                AND (
                {$table_prefix}posts.post_status = 'publish'
                OR {$table_prefix}posts.post_status = 'private'
                )
                GROUP BY {$table_prefix}posts.ID
                ORDER BY {$table_prefix}posts.post_date DESC";
            $result = mysql_query($sql);
            while($row = mysql_fetch_array($result)){
                $ids[] = $row['ID'];
            }

            $current_index = 0;
            foreach ($ids as $index => $ID) {
                if($ID == $post->ID){
                    $current_index = $index + 1;
                }
            }
            $curr_page = ceil($current_index / get_option('posts_per_page'));

            // term 로드 결과 있으면
            return get_term_link($term) . "/page/" . $curr_page;
        }else{

            $sql = "SELECT SQL_CALC_FOUND_ROWS {$table_prefix}posts.ID
                FROM {$table_prefix}posts
                WHERE 1 =1
                AND {$table_prefix}posts.post_type = '{$post->post_type}'
                AND (
                {$table_prefix}posts.post_status = 'publish'
                OR {$table_prefix}posts.post_status = 'private'
                )
                ORDER BY {$table_prefix}posts.post_date DESC";
            $result = mysql_query($sql);
            while($row = mysql_fetch_array($result)){
                $ids[] = $row['ID'];
            }

            $current_index = 0;
            foreach ($ids as $index => $ID) {
                if($ID == $post->ID){
                    $current_index = $index + 1;
                }
            }
            $curr_page = ceil($current_index / get_option('posts_per_page'));

            // ない。(custom post typeの場合、termがまったくないことができる。)
            return home_url('/page/' . $curr_page . '/?post_type=' . $post->post_type);
        }
    }
}

- コメント機能はありません。コメントの代わりに[email protected]にメールを送ってください。