我正在创建我的第一个wordpress模板,我想将自定义小部件区域添加到我的页脚或主页。php文件(两种方法都尝试了,没有任何效果)。我已经在函数中注册了widget区域。php文件和Customize > Widgets 是:
我的代码是从教程网站复制粘贴的
谁能告诉我我做错了什么?如何添加任何小部件区域?感谢您提前提供的任何提示。
我尝试通过将代码放入函数并调用来注册它们:
add_action(\'widgets_init\',\'smth)
但是通过使用这种样式,我的wordpress不会在customize>widgets
FUNCTIONS.PHP CODE(widgets at the bottom):
<?php
add_theme_support(\'post-thumbnails\');
add_theme_support(\'menus\');
/**
* Enqueue scripts
*
* @param string $handle Script name
* @param string $src Script url
* @param array $deps (optional) Array of script names on which this script depends
* @param string|bool $ver (optional) Script version (used for cache busting), set to null to disable
* @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
*/
function theme_styles() {
wp_enqueue_style(\'bootstrap_css\',get_template_directory_uri().\'/css/bootstrap.min.css\');
wp_enqueue_style( \'main_css\', get_template_directory_uri().\'/style.css\');
wp_enqueue_style(\'owl_carousel_css\',
get_template_directory_uri().\'/owl_carousel/dist/assets/owl.carousel.min.css\');
wp_enqueue_style(\'owl_carousel_theme_css\',
get_template_directory_uri().\'/owl_carousel/dist/assets/owl.theme.default.min.css\');
wp_enqueue_style(\'font-awesome\',
get_template_directory_uri().\'/font-awesome47/css/font-awesome.min.css\');
}
add_action( \'wp_enqueue_scripts\', \'theme_styles\' );
/**
* Enqueue scripts
*
* @param string $handle Script name
* @param string $src Script url
* @param array $deps (optional) Array of script names on which this script depends
* @param string|bool $ver (optional) Script version (used for cache busting), set to null to disable
* @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
*/
function theme_js() {
global $wp_scripts;
wp_enqueue_script(\'jquery\',\'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js\', array(), null, true);
wp_enqueue_script( \'bootstrap_js\', get_template_directory_uri().\'/js/bootstrap.min.js\', array( \'jquery\' ), false, false);
wp_enqueue_script(\'owl_carousel\',
get_template_directory_uri().\'/owl_carousel/dist/owl.carousel.min.js\', array(\'jquery\'),false,true);
}
add_action( \'wp_enqueue_scripts\', \'theme_js\' );
add_action( \'init\', \'create_post_type\' );
function create_post_type(){
register_post_type(\'slider\',
array(
\'labels\'=>array(
\'name\'=>__(\'Slider\'),
\'signular_name\'=>__(\'Slider\')
),
\'public\'=>true,
\'has_archive\'=>false,
\'supports\'=> array(
\'title\',
\'editor\',
\'excerpt\',
\'thumbnail\',
\'custom-fields\',
\'revisions\'
)
)
);
}
function mytheme_customize_register( $wp_customize ) {
$wp_customize->add_section( \'themeslug_logo_section\' , array(
\'title\' => __( \'Logo\', \'themeslug\' ),
\'priority\' => 30,
\'description\' => \'Upload a logo to replace the default site name and
description in the header\',
) );
$wp_customize->add_setting( \'themeslug_logo\' );
$wp_customize->add_control( new WP_Customize_Image_Control(
$wp_customize, \'themeslug_logo\', array(
\'label\' => __( \'Logo\', \'themeslug\' ),
\'section\' => \'themeslug_logo_section\',
\'settings\' => \'themeslug_logo\',
) ) );
}
add_action( \'customize_register\', \'mytheme_customize_register\' );
add_action( \'customize_register\', \'genesischild_register_theme_customizer\' );
/*
* Register Our Customizer Stuff Here
*/
function genesischild_register_theme_customizer( $wp_customize ) {
// Create custom panel.
$wp_customize->add_panel( \'text_blocks\', array(
\'priority\' => 500,
\'theme_supports\' => \'\',
\'title\' => __( \'Text Blocks\', \'agrowinnica\' ),
\'description\' => __( \'Set editable text for certain content.\', \'agrowinnica\' ),
) );
// Add Footer Text
// Add section.
$wp_customize->add_section( \'custom_footer_text\' , array(
\'title\' => __(\'Change Footer Text\',\'agrowinnica\'),
\'panel\' => \'text_blocks\',
\'priority\' => 10
) );
$wp_customize->add_section(\'big_text\',array(
\'title\'=>__(\'Duży tekst\',\'agrowinnica\'),
));
// Add setting
$wp_customize->add_setting( \'footer_text_block\', array(
\'default\' => __( \'default text\', \'agrowinnica\' ),
\'sanitize_callback\' => \'sanitize_text\'
) );
$wp_customize->add_setting( \'big_text_block\', array(
\'default\' => __( \'default text\', \'agrowinnica\' ),
\'sanitize_callback\' => \'sanitize_text\'
) );
// Add control
$wp_customize->add_control( new WP_Customize_Control(
$wp_customize,
\'custom_footer_text\',
array(
\'label\' => __( \'Footer Text\', \'agrowinnica\' ),
\'section\' => \'custom_footer_text\',
\'settings\' => \'footer_text_block\',
\'type\' => \'text\'
)
)
);
$wp_customize->add_control(new WP_Customize_Control(
$wp_customize,
\'big_text\',
array(
\'label\'=>__(\'Duży tekst\',\'agrowinnica\'),
\'section\'=>\'big_text\',
\'settings\'=>\'big_text_block\',
\'type\' =>\'text\'
)
)
);
register_sidebar( array(
\'name\' => \'Footer Sidebar 1\',
\'id\' => \'footer-sidebar-1\',
\'description\' => \'Appears in the footer area\',
\'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</aside>\',
\'before_title\' => \'<h3 class="widget-title">\',
\'after_title\' => \'</h3>\',
));
// Sanitize text
function sanitize_text( $text ) {
return sanitize_text_field( $text );
}
register_sidebar(array(
\'name\' => \'O nas\',
\'id\' => \'o-nas\',
\'description\' => \'O nas\',
\'before_widget\' => \'<div id="one" class="two">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2>\',
\'after_title\' => \'</h2>\'
));
register_sidebar(array(
\'name\' => \'O naso\',
\'id\' => \'studio-text\',
\'description\' => \'Studia\',
\'before_widget\' => \'<div id="one" class="two">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2>\',
\'after_title\' => \'</h2>\'
));
function wpb_widgets_init() {
register_sidebar( array(
\'name\' => \'Custom Header Widget Area\',
\'id\' => \'custom-header-widget\',
\'before_widget\' => \'<div class="chw-widget">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2 class="chw-title">\',
\'after_title\' => \'</h2>\',
) );
}
add_action( \'widgets_init\', \'wpb_widgets_init\',0 );
}
FOOTER.PHP
<?php wp_footer(); ?>
<footer>
<p class="footer-text">
<?php echo get_theme_mod( \'footer_text_block\'); ?>
<?php
if(is_active_sidebar(\'footer-sidebar-1\')){
dynamic_sidebar(\'footer-sidebar-1\');
}
?>
</p>
</footer>
</body>
</html>
最合适的回答,由SO网友:David Lee 整理而成
我修复了你的functions.php
代码:
<?php
add_theme_support(\'post-thumbnails\');
add_theme_support(\'menus\');
/**
* Enqueue scripts
*
* @param string $handle Script name
* @param string $src Script url
* @param array $deps (optional) Array of script names on which this script depends
* @param string|bool $ver (optional) Script version (used for cache busting), set to null to disable
* @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
*/
function theme_styles() {
wp_enqueue_style(\'bootstrap_css\', get_template_directory_uri() . \'/css/bootstrap.min.css\');
wp_enqueue_style(\'main_css\', get_template_directory_uri() . \'/style.css\');
wp_enqueue_style(\'owl_carousel_css\', get_template_directory_uri() . \'/owl_carousel/dist/assets/owl.carousel.min.css\');
wp_enqueue_style(\'owl_carousel_theme_css\', get_template_directory_uri() . \'/owl_carousel/dist/assets/owl.theme.default.min.css\');
wp_enqueue_style(\'font-awesome\', get_template_directory_uri() . \'/font-awesome47/css/font-awesome.min.css\');
}
add_action(\'wp_enqueue_scripts\', \'theme_styles\');
/**
* Enqueue scripts
*
* @param string $handle Script name
* @param string $src Script url
* @param array $deps (optional) Array of script names on which this script depends
* @param string|bool $ver (optional) Script version (used for cache busting), set to null to disable
* @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
*/
function theme_js() {
global $wp_scripts;
wp_enqueue_script(\'jquery\', \'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js\', array(), null, true);
wp_enqueue_script(\'bootstrap_js\', get_template_directory_uri() . \'/js/bootstrap.min.js\', array(\'jquery\'), false, false);
wp_enqueue_script(\'owl_carousel\', get_template_directory_uri() . \'/owl_carousel/dist/owl.carousel.min.js\', array(\'jquery\'), false, true);
}
add_action(\'wp_enqueue_scripts\', \'theme_js\');
function create_post_type() {
register_post_type(\'slider\', array(
\'labels\' => array(
\'name\' => __(\'Slider\'),
\'signular_name\' => __(\'Slider\')
),
\'public\' => true,
\'has_archive\' => false,
\'supports\' => array(
\'title\',
\'editor\',
\'excerpt\',
\'thumbnail\',
\'custom-fields\',
\'revisions\'
)
)
);
}
add_action(\'init\', \'create_post_type\');
function mytheme_customize_register($wp_customize) {
$wp_customize->add_section(\'themeslug_logo_section\', array(
\'title\' => __(\'Logo\', \'themeslug\'),
\'priority\' => 30,
\'description\' => \'Upload a logo to replace the default site name and
description in the header\',
));
$wp_customize->add_setting(\'themeslug_logo\');
$wp_customize->add_control(new WP_Customize_Image_Control(
$wp_customize, \'themeslug_logo\', array(
\'label\' => __(\'Logo\', \'themeslug\'),
\'section\' => \'themeslug_logo_section\',
\'settings\' => \'themeslug_logo\',
)));
}
add_action(\'customize_register\', \'mytheme_customize_register\');
/*
* Register Our Customizer Stuff Here
*/
function genesischild_register_theme_customizer($wp_customize) {
// Create custom panel.
$wp_customize->add_panel(\'text_blocks\', array(
\'priority\' => 500,
\'theme_supports\' => \'\',
\'title\' => __(\'Text Blocks\', \'agrowinnica\'),
\'description\' => __(\'Set editable text for certain content.\', \'agrowinnica\'),
));
// Add Footer Text
// Add section.
$wp_customize->add_section(\'custom_footer_text\', array(
\'title\' => __(\'Change Footer Text\', \'agrowinnica\'),
\'panel\' => \'text_blocks\',
\'priority\' => 10
));
$wp_customize->add_section(\'big_text\', array(
\'title\' => __(\'Duży tekst\', \'agrowinnica\'),
));
// Add setting
$wp_customize->add_setting(\'footer_text_block\', array(
\'default\' => __(\'default text\', \'agrowinnica\'),
\'sanitize_callback\' => \'sanitize_text\'
));
$wp_customize->add_setting(\'big_text_block\', array(
\'default\' => __(\'default text\', \'agrowinnica\'),
\'sanitize_callback\' => \'sanitize_text\'
));
// Add control
$wp_customize->add_control(new WP_Customize_Control(
$wp_customize, \'custom_footer_text\', array(
\'label\' => __(\'Footer Text\', \'agrowinnica\'),
\'section\' => \'custom_footer_text\',
\'settings\' => \'footer_text_block\',
\'type\' => \'text\'
)
)
);
$wp_customize->add_control(new WP_Customize_Control(
$wp_customize, \'big_text\', array(
\'label\' => __(\'Duży tekst\', \'agrowinnica\'),
\'section\' => \'big_text\',
\'settings\' => \'big_text_block\',
\'type\' => \'text\'
)
)
);
// Sanitize text
function sanitize_text($text) {
return sanitize_text_field($text);
}
}
add_action(\'customize_register\', \'genesischild_register_theme_customizer\');
function wpb_widgets_init() {
register_sidebar(array(
\'name\' => \'Custom Header Widget Area\',
\'id\' => \'custom-header-widget\',
\'before_widget\' => \'<div class="chw-widget">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2 class="chw-title">\',
\'after_title\' => \'</h2>\',
));
register_sidebar(array(
\'name\' => \'Footer Sidebar 1\',
\'id\' => \'footer-sidebar-1\',
\'description\' => \'Appears in the footer area\',
\'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</aside>\',
\'before_title\' => \'<h3 class="widget-title">\',
\'after_title\' => \'</h3>\',
));
register_sidebar(array(
\'name\' => \'O nas\',
\'id\' => \'o-nas\',
\'description\' => \'O nas\',
\'before_widget\' => \'<div id="one" class="two">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2>\',
\'after_title\' => \'</h2>\'
));
register_sidebar(array(
\'name\' => \'O naso\',
\'id\' => \'studio-text\',
\'description\' => \'Studia\',
\'before_widget\' => \'<div id="one" class="two">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2>\',
\'after_title\' => \'</h2>\'
));
}
add_action(\'widgets_init\', \'wpb_widgets_init\', 0);
您的
wpb_widgets_init
小部件区域的功能位于内部
genesischild_register_theme_customizer
外面还有一个区域。