admin管理员组

文章数量:1296841

I'm using the catch-base theme as a parent theme and have the following code in functions.php

<?php
function my_theme_enqueue_styles() {

$parent_style = 'catch-base';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-theme',
    get_stylesheet_directory_uri() . '/style.css',
    array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

I for-the-life-of-me cannot figure out how to overwrite any of the template files through the child theme. I have tried the suggestion on the catchthemes website, / but just using the same directory structure isn't working.

I've also tried adding this code after the "add_action" function, but it breaks the site and gives an "access denied" error.

require_once( get_stylesheet_directory() . '/inc/catchbase-structure.php' ); 

What am I missing?

I'm using the catch-base theme as a parent theme and have the following code in functions.php

<?php
function my_theme_enqueue_styles() {

$parent_style = 'catch-base';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-theme',
    get_stylesheet_directory_uri() . '/style.css',
    array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

I for-the-life-of-me cannot figure out how to overwrite any of the template files through the child theme. I have tried the suggestion on the catchthemes website, https://catchthemes/blog/create-child-theme-wordpress/ but just using the same directory structure isn't working.

I've also tried adding this code after the "add_action" function, but it breaks the site and gives an "access denied" error.

require_once( get_stylesheet_directory() . '/inc/catchbase-structure.php' ); 

What am I missing?

Share Improve this question asked Sep 8, 2016 at 18:46 BrandonBrandon 251 silver badge5 bronze badges 2
  • What am I missing is the producer of this "access denied error". it's PHP or the web server ? And wich file do you want to overwrite with the child theme ? the built-in overwriting workk only with parent theme template files – mmm Commented Sep 8, 2016 at 19:22
  • I was able to workaround actually replacing the file – Brandon Commented Sep 8, 2016 at 19:51
Add a comment  | 

2 Answers 2

Reset to default 2

The catch-base theme wraps every function in an if statement to make it easy to modify if necessary.

Example....

if (! function_exists('function_name')) { 
    /* some code */
}

So what I ended up doing was adding this to my functions.php file in the child theme and it overwrote the function and the extra divs that I added showed up!

function function_name() {
    /* some modified code */
}

Child themes let you override template files that are loaded via the template hierarchy or get_template_part such as index.php or archive.php.

Child themes cannot be used this way to replace arbitrary files such as CSS files, functions.php, files included ussing require or include, etc

If the parent theme contains functions you want to remove or replace, these are your options:

  • unhook the filter or action if it's added in the parent theme ( be mindful that the order things happen in is important, you can't unhook something if it hasn't been added yet! )
  • redefine it if the parent theme has wrapped the function in conditional checks ( not always possible, inspect parent theme code to check )
  • turn the functionality off if the parent theme provides a setting
  • dequeue any enqueued scripts or styles you do not want ( be mindful of order/priority, you can't dequeue something that hasn't been enqueued yet! Order matters! )
  • contact parent theme author
  • fork parent theme

Sadly there is no generic PHP/WP method to remove and replace an arbitrary PHP function. If there is no mechanism provided for doing it then it can't be done.

本文标签: Cannot figure out how to overwrite files in child theme