我正在尝试在我的主页上显示小部件区域。我的结果是"您的主题有1个小部件区域,但此特定页面不显示它。"。这是我目前的尝试,我不知道为什么这不起作用。
这是在functions.php文件
中 register_sidebar(array(
'name' => __('FirstPage Widget Area', 'jobify'),
'id' => 'widget-area-first-page',
'description' => __('Choose what should display on the custom static homepage.', 'jobify'),
'before_widget' => '<section id="%1$s" class="widget widget--home %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3 class="widget-title widget-title--home">',
'after_title' => '</h3>',
));
这是主页的模板文件home.php
<?php /* Template Name: Home */
declare(strict_types=1);
get_header();
dynamic_sidebar('widget-area-first-page');
get_footer();
这是我的整个functions.php文件
<?php
declare(strict_types=1);
// Register plugin helpers.
require template_path('includes/plugins/plate.php');
// Register post types
require template_path('post-types/index.php');
// Register custom fields
require template_path('custom-fields/index.php');
// Set theme defaults.
add_action('after_setup_theme', function () {
// Show the admin bar.
show_admin_bar(false);
// Add post thumbnails support.
add_theme_support('post-thumbnails');
// Add title tag theme support.
add_theme_support('title-tag');
// Add HTML5 support.
add_theme_support('html5', [
'caption',
'comment-form',
'comment-list',
'gallery',
'search-form',
'widgets',
]);
// Add primary WordPress menu.
register_nav_menu('primary-menu', __('Primary Menu', 'wordplate'));
});
// Enqueue and register scripts the right way.
add_action('wp_enqueue_scripts', function () {
wp_deregister_script('jquery');
wp_enqueue_style('wordplate', mix('styles/app.css'));
wp_register_script('wordplate', mix('scripts/app.js'), '', '', true);
wp_enqueue_script('wordplate');
});
// Remove JPEG compression.
add_filter('jpeg_quality', function () {
return 100;
}, 10, 2);
// Set custom excerpt more.
add_filter('excerpt_more', function () {
return '...';
});
// Set custom excerpt length.
add_filter('excerpt_length', function () {
return 101;
});
// Functions for translating theme
load_theme_textdomain('Iplay', '/languages');
$locale = get_locale();
$locale_file = "/languages/$locale.php";
if (is_readable($locale_file)) {
require_once($locale_file);
}
// function includes()
// {
// $this->files = array(
// 'widgets.php',
// );
//
// foreach ($this->files as $file) {
// require_once(get_template_directory() . '/widgets/' . $file);
// }
// }
/**
* Register our sidebars and widgetized areas.
*
*/
register_sidebar(array(
'name' => __('FirstPage Widget Area', 'iplay'),
'id' => 'widget-area-first-page_new',
'description' => __('Choose what should display on the custom static homepage.', 'iplay'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3 class="widget-title widget-title--home">',
'after_title' => '</h3>',
));
答案 0 :(得分:1)
在funtions.php文件中添加。
function jobify_widgets_init() {
register_sidebar( array(
'name' => __( 'FirstPage Widget Area', 'jobify' ),
'id' => 'widget-area-first-page',
'description' => __( 'Choose what should display on the custom static homepage.', 'jobify' ),
'before_widget' => '<section id="%1$s" class="widget widget--home %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3 class="widget-title widget-title--home">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', 'jobify_widgets_init' );
在home.php中添加此内容
<?php if ( is_active_sidebar( 'widget-area-first-page' ) ) : ?>
<?php dynamic_sidebar( 'widget-area-first-page' ); ?>
<?php endif; ?>
答案 1 :(得分:0)
尝试此代码:
register_sidebar(array(
'name' => __('FirstPage Widget Area', 'jobify'),
'id' => 'widget-area-first-page_new',
'description' => __('Choose what should display on the custom static homepage.', 'jobify'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3 class="widget-title widget-title--home">',
'after_title' => '</h3>',
));
希望这对你有用。