[워드프레스 코드 조각] 목록 보기

워드프레스엔 기본적으로 제공하는 목록 버튼이 없다. 블로그로 출발해서 그런지 모르겠지만, 여튼간에 간단하게 목록 보기를 구현해 보자.

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);
        }
    }
}

카테고리 글 목록 👉

대표글

“[워드프레스 코드 조각] 목록 보기”에 대한 3개의 응답

  1. 안녕하세요. 검색 검색 하다가 들어왔습니다. 내용도 좋고 블로그도 멋지네요.

    제가 지금 카테고리 목록 보기를 하고싶어서 무척 헤메고 있는 사람입니다.
    딱 녹풍님 메인 메뉴들처럼 누르면 요약본이 아닌 목록이 좌르륵 나오게 하고 싶습니다.

    전문 지식은 전혀 없지만 위의 방법을 참고 하면 저도 가능할지 궁금합니다.
    물론 저 코드들을 어디다 어떻게 넣어야 하는지도 모르지만 말입니다.
    도움 주시면 정말 감사할것 같습니다.

    1. 전문지식 없이 소스를 고칠 수는 없습니다. 플러그인 쪽을 찾아 보시거나, 그런 테마를 고르시는 게 나을 것 같습니다. 아니면 PHP와 CSS 공부를 시작하시는 게 낫습니다.

      1. 답변 감사합니다.
        html과 css는 할 줄 알고요, JS는 공부 중이며 php는 아직입니다.
        번거롭게 해드리고 있지만, 설명해 주시면 제가 어떻게든 해보겠습니다. +_+

안형우 에 응답 남기기응답 취소