Chọn nhiều chuyên mục trong tùy biến giao diện wordpress
Đăng ngày: 11-08-2023
58 lượt xem
B1. Vào file functions.php => Đăng ký hàm tùy biến giao diện –> Sau đó viết nội dung code vào hàm đó
function PTL_customize_register($wp_customize){
}
add_action(‘customize_register’,’PTL_customize_register’);
B2. Tạo 1 file (Có thể đặt tên Checkbox_Multiple) => Viết function Chekbox_Multiple trong file vừa tạo
Nội dung function như sau <?php /** * Multiple checkbox customize control class. * * @since 1.0.0 * @access public */ class Checkbox_Multiple extends WP_Customize_Control { /** * The type of customize control being rendered. * * @since 1.0.0 * @access public * @var string */ public $type = 'checkbox-multiple'; /** * Displays the control content. * * @since 1.0.0 * @access public * @return void */ public function render_content() { echo '<script src="' . get_template_directory_uri() . '/assets/js/customize-controls.js"></script>'; if ( empty( $this->choices ) ) return; ?> <?php if ( !empty( $this->label ) ) : ?> <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span> <?php endif; ?> <?php if ( !empty( $this->description ) ) : ?> <span class="description customize-control-description"><?php echo $this->description; ?></span> <?php endif; ?> <?php $multi_values = !is_array( $this->value() ) ? explode( ',', $this->value() ) : $this->value(); ?> <ul> <?php foreach ( $this->choices as $value => $label ) : ?> <li> <label> <input type="checkbox" value="<?php echo esc_attr( $value ); ?>" <?php checked( in_array( $value, $multi_values ) ); ?> /> <?php echo esc_html( $label ); ?> </label> </li> <?php endforeach; ?> </ul> <input type="hidden" <?php $this->link(); ?> value="<?php echo esc_attr( implode( ',', $multi_values ) ); ?>" /> <?php } } function jt_sanitize_favorite_fruit( $values ) { $multi_values = !is_array( $values ) ? explode( ',', $values ) : $values; return !empty( $multi_values ) ? array_map( 'sanitize_text_field', $multi_values ) : array(); } function nt_cats() { $args = array( // 'taxonomy' => 'product_cat', // 'post_type' => 'product_cat' 'taxonomy' => 'category', 'post_type' => 'post' ); $cats = array(); //$cats[0] = "Chọn tất cả"; foreach ( get_categories($args) as $categories => $category ) { $cats[$category->term_id] = $category->name .' ('. $category -> count .')'; } return $cats; }
B3. Gọi tới file vừa tạo require_once(get_stylesheet_directory() .'/inc/Checkbox_Multiple.php'); B4. Đăng ký section $wp_customize->add_section("home", array( 'title' => 'Nội dung Trang chủ', 'priority' => 130, 'description' => __('Cấu hình nội dung trang chủ tại đây'), )); B5. Đăng ký các control // Chọn chuyên mục $wp_customize->add_setting( 'category', array( 'default' => 0, //'transport' => 'refresh', 'sanitize_callback' => 'jt_sanitize_favorite_fruit' )); $wp_customize->add_control( new Checkbox_Multiple ( $wp_customize, 'category', array( 'settings' => 'category', 'label' => 'Hiển thị bài viết theo chuyên mục', 'section' => 'home', //'type' => 'multiple-select', // The $type in our class 'choices' => nt_cats() ) )); Như vậy kho web đã hoàn thành các bước thiết lặp nhiều chuyên mục trong tùy biến giao diện. Sau đâu là code hoàn chỉnh
// Tùy chỉnh giao diện function PTL_customize_register($wp_customize){ require_once(get_stylesheet_directory() .'/inc/Checkbox_Multiple.php'); $wp_customize->add_section("home", array( 'title' => 'Nội dung Trang chủ', 'priority' => 130, 'description' => __('Cấu hình nội dung trang chủ tại đây'), )); // Chọn chuyên mục $wp_customize->add_setting( 'category', array( 'default' => 0, //'transport' => 'refresh', 'sanitize_callback' => 'jt_sanitize_favorite_fruit' )); $wp_customize->add_control( new Checkbox_Multiple ( $wp_customize, 'category', array( 'settings' => 'category', 'label' => 'Hiển thị bài viết theo chuyên mục', 'section' => 'home', //'type' => 'multiple-select', // The $type in our class 'choices' => nt_cats() ) )); } add_action('customize_register','PTL_customize_register'); // End Tùy chỉnh giao diện
Trả lời