분류별로 최신글 출력
게시판에 여러 분류가 있을 때 특정 분류별로 최신글을 출력하는 방법입니다.
수정파일
lib/latest.lib.php
기본 함수에서 마지막에 있는 옵션 값을 활용하는 방법입니다.
function latest($skin_dir='', $bo_table, $rows=10, $subject_len=40, $cache_time=1, $options='')
최신글 함수 내용 중에서..
$sql = " select * from {$tmp_write_table} where wr_is_comment = 0 order by wr_num limit 0, {$rows} ";
위 내용을 이렇게 수정합니다.
if ($options) {
$sql = " select * from {$tmp_write_table} where ca_name = '$options' and wr_is_comment = 0".$where." order by wr_num limit 0, {$rows} ";
} else {
$sql = " select * from {$tmp_write_table} where wr_is_comment = 0 order by wr_num limit 0, {$rows} ";
}
그리고 최신글 출력은..
<?php echo latest("basic", "notice", 5, 25) ?>
원래는 이렇게 출력하는 게 일반적인 방법이지만 아래 내용처럼 뒷부분을 더 추가합니다.
<?php echo latest("basic", "notice", 5, 25, '', "분류명") ?>
그리고 이 방법의 단점이 있는데 캐시를 꺼야 합니다.
config.php 파일에서 최신글 설정을 false로 변경
define('G5_USE_CACHE', false); // 최신글등에 cache 기능 사용 여부
캐시를 꼭 사용해야 하는 홈페이지라면 분류별 최신글용 함수를 별도로 만들어서
그 함수에서만 캐시를 삭제하고 사용하면 다른 최신글은 정상적으로 캐시 기능을 사용할 수 있을 겁니다.
예를 들어서 위 내용은 수정하지 말고 원본 그대로 두고
lib/latest.lib.php 파일 마지막 부분 } 여기 바로 밑에 함수명을 다른 이름으로 해서 하나 더 추가합니다.
아래 예제에서는 함수 이름을 latest_cate 로 했고 캐시 관련 코드를 삭제했습니다.
// 분류별 최신글 추출
function latest_cate($skin_dir='', $bo_table, $rows=10, $subject_len=40, $options='')
{
global $g5;
if (!$skin_dir) $skin_dir = 'basic';
if(preg_match('#^theme/(.+)$#', $skin_dir, $match)) {
if (G5_IS_MOBILE) {
$latest_skin_path = G5_THEME_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
if(!is_dir($latest_skin_path))
$latest_skin_path = G5_THEME_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
$latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
} else {
$latest_skin_path = G5_THEME_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1];
$latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
}
$skin_dir = $match[1];
} else {
if(G5_IS_MOBILE) {
$latest_skin_path = G5_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
$latest_skin_url = G5_MOBILE_URL.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir;
} else {
$latest_skin_path = G5_SKIN_PATH.'/latest/'.$skin_dir;
$latest_skin_url = G5_SKIN_URL.'/latest/'.$skin_dir;
}
}
$list = array();
$sql = " select * from {$g5['board_table']} where bo_table = '{$bo_table}' ";
$board = sql_fetch($sql);
$bo_subject = get_text($board['bo_subject']);
$tmp_write_table = $g5['write_prefix'] . $bo_table; // 게시판 테이블 전체이름
$sql = " select * from {$tmp_write_table} where ca_name = '$options' and wr_is_comment = 0 order by wr_num limit 0, {$rows} ";
$result = sql_query($sql);
for ($i=0; $row = sql_fetch_array($result); $i++) {
try {
unset($row['wr_password']);
} catch (Exception $e) {
}
$list[$i] = get_list($row, $board, $latest_skin_url, $subject_len);
}
ob_start();
include $latest_skin_path.'/latest.skin.php';
$content = ob_get_contents();
ob_end_clean();
return $content;
}
그리고 최신글 출력 부분은 새로 생성한 함수로 출력합니다.
<?php echo latest_cate("스킨명", "게시판id", 출력개수, 제목글자수, "분류명") ?>
예를 들어..
<?php echo latest_cate("basic", "notice", 5, 25, "질문") ?>
이렇게 하면 다른 최신글에는 정상적으로 캐시 기능을 사용할 수가 있습니다.
수정된 latest.lib.php 파일 첨부했습니다.
첨부파일
댓글목록 +1
댓글목록

이준수님의 댓글
이준수 작성일초보자 한번시도해보겠습니다.