• I have made a child theme to modify the Casper theme, I need to override some code in ‘functions.php’ so it points to the correct file inside the child theme ‘inc’ subfolder.

    The solution seems be simple: to copy ‘functions.php’ from the parent folder to the child so the references to the ‘inc’ folder will be read as from the child folder. There is not even need to modify functions.php.

    When I do this I get the following error in WP:

    Fatal error: Cannot redeclare casper_widgets_init() (previously declared in [wp’s absolute path]/wp-content/themes/casper-child/functions.php:76) in [wp’s absolute path]/wp-content/themes/casper/functions.php on line 83

    It’s like WP is now executing both ‘functions.php’: the parent and the child, this causes the ‘redeclaration’ mentioned in the error message.

    I like the idea of having a child theme but this is confusing. How should I do it?

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Your second thought was correct: WordPress loads the child theme’s functions.php and then the parent theme’s functions.php. Unfortunately, your first thought would not have worked anyway: Casper’s functions.php uses get_template_directory, which uses the parent theme’s directory when used in a child theme.

    What specifically are you trying to do? There may be a way to do it without having to override files in the inc/ directory.

    Thread Starter naio

    (@naio)

    It’s quite simple: I need to translate the the literal ‘by’ in the post meta data section, there should be a simpler way, sure…

    However I made changes to header.php, footer.php and style.css into the child theme’s folder and it worked well.

    Thanks for your help.

    Thread Starter naio

    (@naio)

    And now I would like to customize the Read More link text, as described here. The same problem…

    You actually can override casper_posted_on() from your child theme’s functions.php even though it’s called from the parent theme’s inc/template-tags.php. You can customize the Read More text in the same way:

    function casper_posted_on() {
      ... your HTML/PHP code here will override the parent theme's function ...
    }
    
    function modify_read_more_link() {
    return '<a class="more-link" href="' . get_permalink() . '">Your Read More Link Text</a>';
    }
    add_filter( 'the_content_more_link', 'modify_read_more_link' );
    Thread Starter naio

    (@naio)

    I can’t have functions.php in my child folder, so if I modify the casper-child/inc/template-tags.php changes don’t take effect. The only way to make the changes you suggest is to edit casper-parent/inc/template-tags.php but this breaks the purpose of having a child theme…

    Why can’t you have functions.php in your child theme? You don’t need to copy the entire parent’s functions.php into your child theme’s functions.php; what I posted should be sufficient. Don’t forget the opening <?php tag.

    Thread Starter naio

    (@naio)

    You are right, it works!

    Thanks Stephen.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘inc folder in child theme’ is closed to new replies.