카테고리 출력 함수 Hack

사이드바의 카테고리에 출력되는 카테고리 내의 포스트 갯수 등이 링크 밖에 놓이게 되어 경우에 따라 불편을 초래한다. 예를 들면 링크에 block 속성을 부여하면, 링크 밖에 있는 엘리먼트는 다음 줄로 밀려나 시각적으로 흐트러진 느낌을 주게된다. 스타일만으로 이를 해결하기 위해서 여러가지 시도를 해 보았으나 썩 만족스럽지 못했다. 결국은 코어 함수를 건드릴 수 밖에 없었다. 링크가 갯수나 현재 선택된 카테고리 표시 등까지 모드 포함하게 하려면, wp-includes/classes.php 580라인 정도에 있는 start_el 함수를 다음과 같이 수정해 주면 된다.

  1. function start_el($output, $category, $depth, $args) {
  2.   extract($args);   $cat_name = attribute_escape( $category->name);
  3.   $cat_name = apply_filters( ‘list_cats’, $cat_name, $category );
  4.   $link = ‘<a href="’ . get_category_link( $category->term_id ) . ‘" ‘;
  5.   if ( $use_desc_for_title == 0 || empty($category->description) )
  6.     $link .= ‘title="’ . sprintf(__( ‘View all posts filed under %s’ ), $cat_name) . ‘"’;
  7.   else
  8.     $link .= ‘title="’ . attribute_escape( apply_filters( ‘category_description’, $category->description, $category )) . ‘"’;
  9.   $link .= ‘>’;
  10.   $link .= $cat_name;
  11.   if ( isset($show_count) &amp;&amp; $show_count )
  12.     $link .= ‘ (‘ . intval($category->count) . ‘)’;
  13.   if ( isset($show_date) &amp;&amp; $show_date ) {
  14.     $link .= ‘ ‘ . gmdate(‘Y-m-d’, $category->last_update_timestamp);
  15.   }
  16.   $link .= ‘</a>’;
  17.   if ( (! empty($feed_image)) || (! empty($feed)) ) {
  18.     $link .= ‘ ‘;
  19.   if ( empty($feed_image) )
  20.     $link .= ‘(‘;
  21.   $link .= ‘<a href="’ . get_category_rss_link( 0, $category->term_id, $category->slug ) . ‘"’;
  22.   if ( empty($feed) )
  23.     $alt = ‘ alt="’ . sprintf(__( ‘Feed for all posts filed under %s’ ), $cat_name ) . ‘"’;
  24.   else {
  25.     $title = ‘ title="’ . $feed . ‘"’;
  26.     $alt = ‘ alt="’ . $feed . ‘"’;
  27.     $name = $feed;
  28.     $link .= $title;
  29.   }
  30.   $link .= ‘>’;
  31.   if ( empty($feed_image) )
  32.     $link .= $name;
  33.   else
  34.     $link .= "<img src=’$feed_image’$alt$title" . ‘ />’;
  35.   $link .= ‘</a>’;
  36.   if ( empty($feed_image) )
  37.     $link .= ‘)’;
  38.   }
  39.   if ( $current_category )
  40.     $_current_category = get_category( $current_category );
  41.   if ( ‘list’ == $args[‘style’] ) {
  42.     $output .= "\t<li";
  43.     $class = ‘cat-item cat-item-’.$category->term_id;
  44.     if ( $current_category &amp;&amp; ($category->term_id == $current_category) )
  45.       $class .=  ‘ current-cat’;
  46.     elseif ( $_current_category &amp;&amp; ($category->term_id == $_current_category->parent) )
  47.       $class .=  ‘ current-cat-parent’;
  48.     $output .=  ‘ class="’.$class.‘"’;
  49.     $output .= ">$link\n";
  50.   } else {
  51.     $output .= "\t$link<br />\n";
  52.   }
  53.   return $output;
  54. }

핵심은 갯수를 <a> 태그 안에 포함시키는 것이다.

Leave a Comment

Quote selected text in this page