WordPress에서 사용빈도를 반영한 태그 위젯 만들어 보기

참고링크: https://www.wpbeginner.com/plugins/how-to-display-most-popular-tags-in-wordpress

기본으로 제공되는 Tags 위젯은 사용된 Tag를 순서대로 모두다 보여줘서 좀 번거로운 면이 있다. 사용 빈도를 반영하여 많이 사용된 Tag를 크게 보여주고, 또 원하는 순위까지만 보여주는 것이 좀 괜찮을 것 같은데, 이를 위해선 해당 기능을 포함하는 플러그인을 설치해서 사용하거나, 간단히 functions.php를 수정하면 된다.

사용하는 테마의 디렉토리로 가서, functions.php 파일을 열고, 다음의 구문을 추가한다.

function wpb_tag_cloud() {
$tags = get_tags();
$args = array(
    'smallest'                  => 12,
    'largest'                   => 24,
    'unit'                      => 'px',
    'number'                    => 15,
    'format'                    => 'flat',
    'separator'                 => " ",
    'orderby'                   => 'count',
    'order'                     => 'DESC',
    'show_count'                => 1,
    'echo'                      => false
);

$tag_string = wp_generate_tag_cloud( $tags, $args );

return $tag_string;

}
// Add a shortcode so that we can use it in widgets, posts, and pages
add_shortcode('wpb_popular_tags', 'wpb_tag_cloud');

// Enable shortcode execution in text widget
add_filter ('widget_text', 'do_shortcode');

여기에서 설정할 옵션은

  • smallest: 가장 낮은 순위의 Tag 폰트 크기
  • largest: 가장 높은 순위의 Tag 폰트 크기
  • number: 15 순위까지 보여주기
  • seperator: Tag들 보여줄때 구분자

이와 같이 설정하고, Widget 설정에서 Text 위젯을 추가하고, 내용에 wpb_popular_tags을 입력한다. 결과를 보면,

와 같이 깔끔하게 보인다.