1

I have been looking around and I'm a bit confused and was not able to find the answer for this question so far.

I have created a plain new child skin. My child skin has only 2 files style.css

/*
 Theme Name:   My theme child colormag
 Description:  My child modification from Colormag theme
 Author:       PalaNolho
 Template:     colormag
 Version:      1.0.0
*/

function.php

<?php
/* 
 * Child theme functions file
 * 
 * Your functions php code starts here just before ?>
 */

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


function my_register_widget() {
    /* Register the 'primary' sidebar. */
    register_sidebar(
        array(
            'id' => 'headerimagewidget',
            'name' => __( '(my) Widget over Header Image' ),
            'description' => __( 'My widget to be displayed over the header image.' ),
            'before_widget' => '<div id="%1$s" class="widget %2$s">',
            'after_widget' => '</div>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>'
        )
    );
    /* Repeat register_sidebar() code for additional sidebars. */
}

add_action( 'widgets_init', 'my_register_widget' );

The widget seems to be correctly configured and I can configure it in the "widgets" section of the admin site so, what I need now is to add the code to make it appear on the page.

Now, here is my problem.... I know what code I need to add. For example, I could add this:

<div id="sidebar-headerimagewidget" class="sidebar">
    <?php dynamic_sidebar( 'headerimagewidget' ); ?>
</div>

However, I have no idea where to actually add it in the child theme, since I only have the function.php file.

If I was doing these changes on the parent theme, I would look at the theme files, look at the place I want to add it, and add it. But since this is child theme...

Do I need to copy the file from the parent to the child and then make the modifications I want? Is there any other way to add the widget to the code without "directly" changing the code of the parent theme?

1

You should copy the file you want to edit from the parent to your child theme. This way you can customize the template without touching the actual theme. Paste the code you already have to this template file and you're good to go.

The following link contains the codex page about creating child themes and modifying the template files: https://codex.wordpress.org/Child_Themes#Template_Files

Hope this helps.

  • if I copy the corresponding file, do I need to add something to my function.php to make it work and WP will use it automatically? – Miguel Pinto Mar 1 '17 at 15:52
  • You don't need to add additional code to your functions.php. Just copy the file and edit it to your needs. Wordpress will automatically detect these files. I probably don't need to tell you this, but make sure you've activated your child theme in your dashboard. – Sjoerd van Hout Mar 1 '17 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.