最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Loading Widgets Via Child Theme

programmeradmin0浏览0评论

I'm using the very popular Elementor theme to build my WordPress site, but unfortunately there is not a Widgets option when I go to Appearance in the Dashboard:

I need it in order to create a sidebar.

So this has caused me to install a Child Theme, so that I can add some code to functions.php in order to get the widgets option to show.

This is what I have in my functions.php file for the child theme:

<?php
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );

function enqueue_parent_styles() {
   wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}

add_action('widgets_init', 'outWidgetsInit');

function ourWidgetsInit() {
    register_sidebar( array (
        'name' => 'Sidebar',
        'id' => 'sidebar1',
    ));
}

And this is causing errors to show at the top of the theme while developing:

Why is the code showing at the top of the screen? What should I do differently here?

I'm using the very popular Elementor theme to build my WordPress site, but unfortunately there is not a Widgets option when I go to Appearance in the Dashboard:

I need it in order to create a sidebar.

So this has caused me to install a Child Theme, so that I can add some code to functions.php in order to get the widgets option to show.

This is what I have in my functions.php file for the child theme:

<?php
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );

function enqueue_parent_styles() {
   wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}

add_action('widgets_init', 'outWidgetsInit');

function ourWidgetsInit() {
    register_sidebar( array (
        'name' => 'Sidebar',
        'id' => 'sidebar1',
    ));
}

And this is causing errors to show at the top of the theme while developing:

Why is the code showing at the top of the screen? What should I do differently here?

Share Improve this question asked Jan 28, 2021 at 21:47 HappyHands31HappyHands31 13510 bronze badges 4
  • That does not look like an error, it seems to be simply printing the code you have added, so something - perhaps another plugins - is causing this issue, you might need to disable all other plugins / themes to try to resolve. – Q Studio Commented Jan 28, 2021 at 22:25
  • 1 Is that your entire functions.php file? Or just what you added? – Tom J Nowell Commented Jan 28, 2021 at 23:15
  • @TomJNowell That's the entire functions.php file for the child theme, yes. – HappyHands31 Commented Jan 29, 2021 at 2:05
  • And you've confirmed this issue happens everywhere that you try to do this? Not just on your specific site or local setup? There is nothing in your functions.php that could do this, the problem is elsewhere – Tom J Nowell Commented Jan 30, 2021 at 14:18
Add a comment  | 

1 Answer 1

Reset to default 0

For whatever reason, maybe it was caching-related, but waiting a day and then adding this code at the bottom of functions.php solved the issue:

if (function_exists("register_sidebar")) {
    register_sidebar();
}

According to this article - see the 'How do I add the Widgets menu option'

https://elementor/help/hello-theme-tips/

So then the full code for my functions.php file within the child theme is:

<?php
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );

function enqueue_parent_styles() {
   wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}

add_action('widgets_init', 'outWidgetsInit');

function ourWidgetsInit() {
    register_sidebar( array (
        'name' => 'Sidebar',
        'id' => 'sidebar1',
    ));
}

if (function_exists("register_sidebar")) {
    register_sidebar();
}
发布评论

评论列表(0)

  1. 暂无评论