Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

[WP] 현재 포스트의 카테고리내의 이전 포스트 추출하기

2013-10-06 by 편리

지금 사용 중인 테마인 Minimal Xpert에는 아래 이미지와 같이 관련글을 보여주는 부분이 있다. 원래 테마에 있던 기능은 동일 카테고리 내의 3개의 포스트를 랜덤하게 보여주는 것이었다. 그런데 내가 원한 것은 동일 카테고리내의 포스트 중에서 현재 글 바로 전에 발행된 3개의 포스트를 보여주는 것이었다.

related-posts

아래는 이 기능을 위해 만들어본 WordPress 3.5.2에서 테스트된 코드이다.

<?php
$category = get_the_category();
$count = count($category);
$cat_id = array();
for($i=0; $i<$count; $i++) {
    if($category[$i]->cat_ID)
        $cat_id[] = $category[$i]->cat_ID;
}
$limit = 3;
$posts = $wpdb->get_results( $wpdb->prepare(
    " SELECT $wpdb->posts.*
        FROM $wpdb->posts
          LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
          LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
        WHERE $wpdb->term_taxonomy.taxonomy = 'category'
          AND $wpdb->term_relationships.term_taxonomy_id IN (".implode(',', $cat_id).")
          AND $wpdb->posts.post_date < '%s'
          AND $wpdb->posts.post_type = 'post'
          AND $wpdb->posts.post_status = 'publish'
        ORDER BY $wpdb->posts.post_date DESC LIMIT $limit", $post->post_date ) );
?>

포스트가 발행된 시간을 기준으로 동일 카테고리내에 있는 이전에 발행된 포스트를 3개 가져오는 것이다. 이 내용을 출력하기 위해서 아래와 같은 간단한 코드를 작성할 수 있다.

<?php
foreach($posts as $post) {
?>
    <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php } ?>

 

참고포스트
http://codex.wordpress.org/Function_Reference/get_the_category
http://wordpress.org/support/topic/actual-query-to-get-posts-from-one-category-general-sql-question
http://wordpress.org/support/topic/how-to-show-last-5-posts

Post navigation

Previous Post:

Apache 2.4.6 + PHP 5.5.1 + MariaDB 5.5.32 설치

Next Post:

졸음이 몰려오는 시간

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • SK 세븐모바일 유심 셀프교체
  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes