본문 바로가기
IT&Guide

그누보드 최근글, 최신글 게시판별로 선택한 게시물만 출력하기 | 원하는 게시판 최신글 선택 출력하기

by Jigton GAL 2025. 6. 9.
반응형

그누보드를 이용해 제작중에 전체 게시판 또는 원하는 게시판의 최신글(최근글)을 뽑아오는 것은 sir 사이트에서 찾을 수 있으나, 관리자가 원하는 게시물만 출력하는 것은 특정 게시판은 가능하나 전체 게시판에서 하는 기능은 찾기 어려웠다.

 

따라서, all_latest 기능과 특정 게시판의 선택게시물 출력기능(wr_1 이용) 을 조합하여, GPT의 도움으로 만들어 보았다. ( 3번 내용 )

 

아래는 참고하기 바란다. 

 

1. 원하는 게시판만 선택해서 출력하기

[최신글]원하는 게시판만 선택해서하기 > 그누보드5 팁자료실 

 

[최신글]원하는 게시판만 선택해서하기 > SIR

latest.lib.php 하단에 아래 함수를 추가하시면됩니다. <br/> <br/>[code] <br/>

sir.kr

 

2. 단일 게시판의 원하는 게시물만 선택해서 출력하기

여분필드를 활용해서 특정 게시물만 최신글로 추출하기 - 최신글스킨강좌, 그누보드5강좌

 

여분필드를 활용해서 특정 게시물만 최신글로 추출하기 - 최신글스킨강좌, 그누보드5강좌

게시판에 글을 등록할 때 별도로 체크한 게시물만 최신글로 출력하는 방법입니다.수정파일1 : lib/latest.lib.php기존에 있던 최신글 함수를 복사해서 함수명을 다른 이름으로 변경해서 같은 파일 하

gnustudy.com

 

 

 

 

 

3. 원하는 게시판의 원하는 게시물만 선택해서 출력하기 

 

1)  /lib/latest.lib.php 에 아래의 내용을 추가한다.

<?php
function latest_selected($skin_dir='', $bo_tables, $rows=10, $subject_len=40, $cache_time=1, $options='') {
    global $g5;

    if (!$skin_dir) $skin_dir = 'basic';

    // 스킨 경로 설정
    if (preg_match('#^theme/(.+)$#', $skin_dir, $match)) {
        $latest_skin_path = (G5_IS_MOBILE) ? G5_THEME_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$match[1] : 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 {
        $latest_skin_path = (G5_IS_MOBILE) ? G5_MOBILE_PATH.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir : G5_SKIN_PATH.'/latest/'.$skin_dir;
        $latest_skin_url = (G5_IS_MOBILE) ? G5_MOBILE_URL.'/'.G5_SKIN_DIR.'/latest/'.$skin_dir : G5_SKIN_URL.'/latest/'.$skin_dir;
    }

    $list_all = array();
    $bo_table_arr = explode(',', $bo_tables);

    foreach ($bo_table_arr as $bo_table) {
        $bo_table = trim($bo_table);
        $board = sql_fetch("SELECT * FROM {$g5['board_table']} WHERE bo_table = '{$bo_table}'");
        if (!$board) continue;

        $write_table = $g5['write_prefix'] . $bo_table;

        $sql = "SELECT * FROM {$write_table} 
                WHERE wr_1 = 1 
                AND wr_id = wr_parent 
                ORDER BY wr_datetime DESC 
                LIMIT 100"; // 넉넉히 가져오고 나중에 정렬해서 자름

        $result = sql_query($sql);

        while ($row = sql_fetch_array($result)) {
            $tmp = get_list($row, $board, $latest_skin_url, $subject_len);
            $tmp['bo_subject'] = $board['bo_subject'];
            $tmp['bo_table'] = $bo_table;
            $tmp['wr_datetime'] = $row['wr_datetime'];
            $tmp['wr_1'] = $row['wr_1'];
            $list_all[] = $tmp;
        }
    }

    // 최신순 정렬 (wr_datetime 기준)
    usort($list_all, function($a, $b) {
        return strcmp($b['wr_datetime'], $a['wr_datetime']);
    });

    // 상위 $rows 개만 자르기
    $list = array_slice($list_all, 0, $rows);

    ob_start();
    include $latest_skin_path.'/latest.skin.php';
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}


?>

 

 

 

2) 사용하고자 하는 리스트 스킨파일 lastest.skin.php 의 상단 <?php  ?> 부분에 아래의 내용을 추가한다. 

// 기존 리스트에서 wr_1 값이 1인 게시물만 필터링
$list_filtered = array_filter($list, function($item) {
    return isset($item['wr_1']) && $item['wr_1'] == 1;
});
$list_count = count($list_filtered);
$is_partial = false;

 

 

 

3) 출력하고자 하는 index 또는 기타 페이지에서 아래와 같이 불러온다. 

<?php echo latest_selected("스킨명", "게시판1, 게시판2, 게시판3, 게시판4", 5, 20 ); ?>

 

 

 

4) 게시판의 write.skin.php 에 아래의 내용을 추가해서 메인출력을 정해준다.

<?php if ($is_admin) { ?>
    <div class="write_div">
        <label for="wr_1">메인 출력</label>
        <input type="checkbox" name="wr_1" id="wr_1" value="1" <?php echo ($wr_1 == 1) ? 'checked' : ''; ?>> 선택된 게시물만 출력
    </div>
<?php } ?>

 

 

 

자, 이제 원하는 게시판의 원하는 게시물을 선택해서 메인에 출력이 가능하다. ^^

 

5) 만일 리스트에서 링크 오류가 나면, 아래와 같이  본문소스의 출력부분을 수정해 주시면 됩니다.

array_filter()로 필터링한 $list_filtered 배열을 사용하고 싶다면 foreach로 쓰는 게 좋습니다.

 

<?php foreach ($list_filtered as $item): ?>
    <?php
        $thumb = get_list_thumbnail($item['bo_table'], $item['wr_id'], $thumb_width, $thumb_height, false, true);
        $img = $thumb['src'] ?? G5_IMG_URL.'/no_img.png';
        $thumb['alt'] = $thumb['alt'] ?? '이미지가 없습니다.';
        $img_content = '<img width="100%" height="100%" src="'.$img.'" alt="'.$thumb['alt'].'" >';
        $wr_href = get_pretty_url($item['bo_table'], $item['wr_id']);
    ?>
    <li class="">
        <section class="<?php echo $is_partial ? 'reduced' : 'no-preference'; ?>">
                <a href="<?php echo $wr_href; ?>">
                    <?php echo run_replace('thumb_image_tag', $img_content, $thumb); ?>
                </a>
                    <div class="">
                        <a href="<?php echo $wr_href; ?>"><?php echo $item['ca_name'];?></a>
                    </div>
                    <h2><a href="<?php echo $wr_href; ?>"><?php echo $item['subject'];?></a></h2>
                    <p><a href="<?php echo $wr_href; ?>"><?php echo utf8_strcut(strip_tags($item['wr_content']), 30, '..'); ?></a></p>
        </section>
    </li>
<?php endforeach; ?>
<?php if (count($list_filtered) == 0): ?>
    <li class="empty_li">게시물이 없습니다.</li>
<?php endif; ?>

 

 

 

 

 

 

 

 

 

 

반응형

댓글