글에서 관련글 보여주기 > 팁

글에서 관련글 보여주기 정보

글에서 관련글 보여주기

본문

지블로그를 잘 쓰고 있습니다.

아쉬운 기능이 있어서 스스로 만들어 봤습니다.


구현한 것은 블로그에서 현재 읽고 있는 글의 관련글을 보여주는 기능입니다.


2038512346_372a9405_1.png


blog의 스킨에 index.skin.php 에서

<!--//////// 내용출력 요이~ 땅! ///////-->
                    <div class="article">
                        <div><?=$current['content_head']?></div>
                        <?=$post[$i]['content']?>
                        <div><?=$current['content_tail']?></div>
                    </div>

여기 밑에

<!-- 태그 관련글 표시 시작 -->
<?
if($post[$i]['tag'])
{
 $taglist = get_post_tag($post[$i]['id']);
    // 태그 앞 뒤 공백 제거
    $firsttag = revision_charset($taglist[0]);

    // 태그 고유번호를 가져 온다.
    $row = sql_fetch("select id from $gb4[tag_table] where tag = '$firsttag'");

    // 태그 고유번호가 존재할 경우에만 검색이 가능하다.
    if (!empty($row))
    {
        // 태그 고유번호로 해당 태그를 사용한 글을 찾아내 검색 쿼리를 만든다.
        $firsttag = "and p.id in(''";

        $qry = sql_query("select post_id from $gb4[taglog_table] where tag_id='$row[id]' ");

        while($row = sql_fetch_array($qry))
            $firsttag .= ",".$row[post_id];

        $firsttag .= ")";
    }
   
 // 현재 페이지의 글 정보를 DB 에서 불러온다.
 $sql = "select
             p.*,
             c.category_name
         from
             $gb4[post_table] p left join $gb4[category_table] c on p.category_id = c.id
         where
             p.blog_id='$current[id]'
             $firsttag
             $sql_secret
         order by
             p.post_date desc
         limit
             1, 5";
 $qry = sql_query($sql); 
 
 // 글 정보를 담을 $post 변수 초기화
 $tagpost = array();
 $index = 0;
 
 while ($row = sql_fetch_array($qry))
 { 
     // 일단 DB 에서 가져온 글 정보를 전부 $post 변수에 담는다.
     $tagpost[$index] = $row;
 
     // 글의 고유주소를 추가로 저장한다.
     $tagpost[$index][url] = get_post_url($row[id]);
 
     // 글에 분류 설정 되어있지 않을경우 '분류없음' 로 기본 설정해준다.
     if (empty($row[category_name]))
         $tagpost[$index][category_name] = '분류없음';
 
     $index++;
 } 
 
 $tagpostcount = count($tagpost);
 
 if($tagpostcount > 0)
 {
?>
<div id="entry_box">
<table border=0 cellpadding=0 cellspacing=0 width=100%>
    <tr>
        <td width=16 height=16><img src="<?=$blog_skin_url?>/img/cont_top_left.gif" alt="" width="16" height="16"></td>
        <td width=100% height=16 style="background: url(<?=$blog_skin_url?>/img/cont_top.gif) no-repeat right top; padding-left:16px;"></td>
    </tr>
    <tr>
        <td width=16 style="background:url(<?=$blog_skin_url?>/img/cont_left.gif) repeat-y left top;"></td>
        <td width=100% bgcolor="#FFFFFF" style="background:url(<?=$blog_skin_url?>/img/cont_bg_01.gif) repeat-y right top; padding-right:16px;">

    <div class="search_result">관련글 총 <?=$tagpostcount?>개</div>

<? // 블로그 글 출력 루프 시작
    for($tagindex=0; $tagindex<$tagpostcount; $tagindex++) {
?>
    <div id="search_entry">
        <span class="search_title">
            <a href="<?=$tagpost[$tagindex]['url']?>">[<?=$tagpost[$tagindex]['category_name']?>]  <?=$tagpost[$tagindex]['title']?></a>
            <? if( $tagpost[$tagindex]['secret'] != 1 ) echo "<span class=\"secret\">(비공개)</span>"; ?>
            <span class="count_info">(<?=$tagpost[$tagindex]['comment_count']?>)</span>
        </span>
    </div>
<? }  // 블로그 글 출력 루프 끝 ?>
        </td>
    </tr>
    <tr>
        <td width=16 height=16><img src="<?=$blog_skin_url?>/img/cont_btm_left.gif" alt="" width="16" height="16"></td>
        <td width=100% height=16 style="background:url(<?=$blog_skin_url?>/img/cont_btm.gif) no-repeat right bottom;"></td>
    </tr>
</table>
</div>
<?
 }
}
?>
<!-- 태그 관련글 표시 끝 -->

를 넣어주면 됩니다.

관련글은 해당글의 첫번째 태그값으로 검색해서 보여줍니다.

  • 복사

댓글 전체

© SIRSOFT
현재 페이지 제일 처음으로