워드프레스 중급 - functions.php 파악하기

워드프레스 functions.php 파악하기 – 워드프레스 강좌

이번 시간에는 워드프레스에서 중요한 기능을 담당하는 functions.php 파일에 대해 알아볼게요.

워드프레스 functions.php 파일은 워드프레스 웹사이트를 기능적으로 만드는 중요한 PHP 기능을 담고 있는 강력한 운영 파일입니다. WordPress에는 함수 파일의 위치는 설치한 테마 내의 루트에 functions.php라는 자체 파일이 있습니다. 만약 잘못 입력하게 되면, 웹사이트가 동작하지 않기 때문에 충분히 공부하고 백업해둔 이후 수정하는 것을 추천드립니다.

functions.php 파일 위치

functions.php 파일은 설치한 테마의 루트 디렉토리에 위치합니다. 예를 들어, wp-content/themes/supernormal/functions.php입니다. 이 파일을 통해 테마의 기능을 확장할 수 있습니다.

주의사항

wp-includes 디렉토리에 있는 functions.php 파일은 절대 수정하지 마세요. 이 파일은 워드프레스 코어 파일로, 수정하면 웹사이트가 제대로 작동하지 않을 수 있습니다. 항상 테마 디렉토리 내의 functions.php 파일을 수정해야 합니다.

기본 설정

테마 지원 기능 추가

테마에서 사용할 기능을 추가할 수 있습니다. 예를 들어, 썸네일 사이즈를 추가하는 등의 작업을 할 수 있습니다.

function supernormal_setup() {
    add_theme_support('automatic-feed-links');
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_image_size('post-thumbnail-img', 345);

    add_theme_support('html5', array(
        'search-form', 'comment-form', 'comment-list', 'gallery', 'caption',
    ));
}
add_action('after_setup_theme', 'supernormal_setup');

스타일 추가

function supernormal_style_sheet() {
    wp_enqueue_style('supernormal-style', get_stylesheet_directory_uri() . '/style.min.css');
}
add_action('wp_enqueue_scripts', 'supernormal_style_sheet');

자바스크립트 추가

자바스크립트를 추가하는 방법입니다. 다음 시간에 스타일 추가와 함께 자세히 다루겠습니다.

function supernormal_script_enqueue() {
    // jQuery
    wp_enqueue_script('jquery-js', get_template_directory_uri() . '/js/jquery-3.6.0.min.js', array('jquery'));

    wp_enqueue_script('custom-script', get_stylesheet_directory_uri() . '/js/custom.min.js', array(), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'supernormal_script_enqueue');

커스텀 메뉴 추가

사이트 메뉴를 추가할 수 있습니다. 예를 들어, 헤더, 푸터, 사이드바 메뉴를 추가할 수 있습니다.

function supernormal_custom_menu() {
    register_nav_menus(
        array(
            'primary-menu' => __('Primary Menu', 'supernormal'),
            'footer-menu'  => __('Footer Menu', 'supernormal'),
            'sidebar-menu' => __('Sidebar Menu', 'supernormal')
        )
    );
}
add_action('init', 'supernormal_custom_menu');

커스텀 사이드바 위젯 추가

사이드바에 위젯을 추가할 수 있습니다.

function supernormal_widgets_sidebar_init() {
    register_sidebar(
        array(
            'name'          => __('Sidebar', 'supernormal'),
            'id'            => 'sidebar-widget',
            'description'   => '',
            'before_widget' => '<aside id="%1$s" class="widget %2$s">',
            'after_widget'  => '</aside>',
            'before_title'  => '<h2 class="widget-title">',
            'after_title'   => '</h2>',
        )
    );
}
add_action('widgets_init', 'supernormal_widgets_sidebar_init');

타이틀 변경

아카이브 페이지의 타이틀을 변경하는 코드입니다. 이 코드를 추가하면 각 페이지의 타이틀을 커스터마이징할 수 있습니다.

add_filter('get_the_archive_title', function ($title) {
    if (is_category()) {
        $title = single_cat_title('', false);
    } elseif (is_tag()) {
        $title = single_tag_title('', false);
    } elseif (is_author()) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    } elseif (is_year()) {
        $title = get_the_date(_x('Y', 'yearly archives date format'));
    } elseif (is_month()) {
        $title = get_the_date(_x('F Y', 'monthly archives date format'));
    } elseif (is_day()) {
        $title = get_the_date(_x('F j, Y', 'daily archives date format'));
    } elseif (is_tax('post_format')) {
        if (is_tax('post_format', 'post-format-aside')) {
            $title = _x('Asides', 'post format archive title');
        } elseif (is_tax('post_format', 'post-format-gallery')) {
            $title = _x('Galleries', 'post format archive title');
        } elseif (is_tax('post_format', 'post-format-image')) {
            $title = _x('Images', 'post format archive title');
        } elseif (is_tax('post_format', 'post-format-video')) {
            $title = _x('Videos', 'post format archive title');
        } elseif (is_tax('post_format', 'post-format-quote')) {
            $title = _x('Quotes', 'post format archive title');
        } elseif (is_tax('post_format', 'post-format-link')) {
            $title = _x('Links', 'post format archive title');
        } elseif (is_tax('post_format', 'post-format-status')) {
            $title = _x('Statuses', 'post format archive title');
        } elseif (is_tax('post_format', 'post-format-audio')) {
            $title = _x('Audio', 'post format archive title');
        } elseif (is_tax('post_format', 'post-format-chat')) {
            $title = _x('Chats', 'post format archive title');
        }
    } elseif (is_post_type_archive()) {
        $title = post_type_archive_title('', false);
    } elseif (is_tax()) {
        $title = single_term_title('', false);
    } else {
        $title = __('Archives');
    }
    return $title;
});

이 코드를 추가하면 각 아카이브 페이지의 타이틀을 더 정확하게 설정할 수 있습니다.

이제 functions.php 파일을 사용하여 다양한 기능을 추가하고 커스터마이징하는 방법을 배웠습니다. 이 파일을 올바르게 사용하면 워드프레스 웹사이트의 기능을 확장하고, 사이트를 더욱 유연하게 관리할 수 있습니다.