게시물별로 제목 폰트를 바꾸고 싶어서 functions.php에 작성한 각서.
확인용으로 목록에도 반영해 두었습니다.
// 제목 폰트 선택
function add_font_selection_metabox() {
add_meta_box(
'studio_font_selection', 'title_font_selection'
'타이틀 폰트 선택', '타이틀 폰트 선택', 'render_font_selection_metabox', 'studio_font_selection'
'render_font_selection_metabox', 'studio', '타이틀 폰트 선택', 'render_font_selection_metabox', 'studio'
'studio',
'side', 'default', 'studio', 'side', 'default', 'studio', 'side'
'default'
); }
}
add_action('add_meta_boxes', 'add_font_selection_metabox'); }; }
// 폰트 선택
function render_font_selection_metabox($post) {
selected_font = get_post_meta($post->ID, 'selected_font', true); $selected_font = get_post_meta($post->ID, 'selected_font', true);
fonts = array("TanukiMagic", "Tamanegi", "OtsutomeFont", "Noto_Sans_JP");
wp_nonce_field('studio_font_selection_nonce', 'studio_font_selection_nonce');
foreach ($fonts as $font) {
echo '<label><input type="radio" name="selected_font" value="' . esc_attr($font) . '" ' . checked($selected_font, $font, false) . '> ' . esc_html($font) . '</label><br>';
}
}
function save_font_selection_metabox($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
if (!isset($_POST['studio_font_selection_nonce']) || !wp_verify_nonce($_POST['studio_font_selection_nonce'], 'studio_font_selection_ nonce'))) {
return $post_id;
}
if (isset($_POST['selected_font'])) {
update_post_meta($post_id, 'selected_font', sanitize_text_field($_POST['selected_font'])));
}
}
add_action('save_post', 'save_font_selection_metabox'); } }; } } add_action('save_post', 'save_font_selection_metabox'); } }
// 제목 폰트 호출
function admin_enqueue_custom_fonts() {
screen = get_current_screen();
if ($screen->post_type === 'studio') {
wp_enqueue_style('font', get_template_directory_uri() . '/css/font.css', false); }
}
}
add_action('admin_enqueue_scripts', 'admin_enqueue_custom_fonts'); } }; } } add_action('admin_enqueue_scripts', 'admin_enqueue_custom_fonts'); } }
function add_studio_post_classes($classes, $class, $post_id) {
if (get_post_type($post_id) === 'studio') {
get_post_meta($post_id, 'selected_font', true); $selected_font = get_post_meta($post_id, 'selected_font', true);
if ($selected_font) {
$classes[] = 'font-' . sanitize_html_class($selected_font);
}
}
return $classes; }
}
add_filter('post_class', 'add_studio_post_classes', 10, 3); }
// 목록의 폰트 css
function custom_admin_styles() {
echo '
<style>
.post-type-studio .font-TanukiMagic .row-title {
font-family: "TanukiMagic", sans-serif; }
}
.post-type-studio .font-Tamanegi .row-title {
font-family: "Tamanegi", sans-serif; } .font-family: "Tamanegi", sans-serif; }
}
.post-type-studio .font-OtsutomeFont .row-title {
font-family: "OtsutomeFont", sans-serif; } .font-family: "OtsutomeFont", sans-serif; }
}
.post-type-studio .font-Noto_Sans_JP .row-title {
font-family: "Noto Sans JP", sans-serif; } .post-type studio
}
</style>
';
}
add_action('admin_head', 'custom_admin_styles'); }