0

我想在我当前使用的主要 WP 主题中的 php 文件中添加 cutom html 代码。所以我决定使用子主题来做到这一点,但我看不出我的代码哪里错了,为什么这不起作用?

这是我的functions.php代码:

<?php
add_action( 'wp_enqueue_scripts', 'boo_child_theme_style', 99 );
add_action( 'wp_enqueue_scripts', 'boo_child_portfolio_style', 99 );

function boo_parent_theme_scripts() {
    wp_enqueue_style( 'base', get_template_directory_uri() . '/style.css' );
}
function boo_child_theme_style(){
    wp_enqueue_style( 'child-boo-style', get_stylesheet_directory_uri() . '/style.css' );   
}

function boo_parent_portfolio_scripts() {
    wp_enqueue_style( 'base', get_template_directory_uri() . '/templates/portfolio/tmpl-grid.php' );
}
function boo_child_portfolio_style(){
    wp_enqueue_style( 'child-boo-style', get_stylesheet_directory_uri() . '/tmpl-grid.php' );   
}

所以对于 style.css 它可以工作,但对于 php 文件它不起作用,我不知道为什么......有人可以解释并帮助我吗?

提前致谢 !

4

2 回答 2

0

您不能通过脚本/样式系统将 PHP 加入队列。

要将部分/全部页面替换为子主题,您需要替换该页面的模板。

有关WordPress 如何为页面选择正确模板的详细信息,请参阅模板层次结构。

如果您只想更改页面的一小部分,这取决于父主题开发人员,这将是多么容易。

一些主题实现了过滤器来帮助子主题修改页面,但正如我所说,他们不必这样做,所以它可能不是您可以使用的东西。

于 2018-05-25T10:58:51.920 回答
0

@arcath 是对的,您不能使用 Enqueue 函数添加 php 文件。它们仅用于添加/覆盖 .css 和 .js 文件。对于使用 wp_enqueue_style 的样式表和使用 wp_enqueue_scripts 的 Javascript,这也是两种不同的方法。

不要一次又一次地调用方法 调用 enqueue 方法的最佳方法是在子目录示例中的 function.php 中只调用一次。

function adding_scripts_and_styles() {
wp_enqueue_script('unique_child_custom_js', get_stylesheet_directory_uri() . '/directory_path_if_any/custom.js', array('jquery'), true, true );
wp_enqueue_script('unique_child_custom_css', get_stylesheet_directory_uri() . '/directory_path_if_any/custom.css'); 
}

add_action( 'wp_enqueue_scripts', 'adding_scripts_and_styles');

对于覆盖 wordpress 模板,在您的子主题 wordpress 目录中创建一个具有相同名称的 php 文件。Wordpress 在加载时首先读取子主题模板文件。

例如,如果您想覆盖archive.php 页面模板,请在您的子主题中创建一个archive.php,然后wordpress 将使用您子主题中的archive.php 文件而忽略父主题archive.php。

希望这有帮助!快乐编码:)

于 2018-05-25T11:34:22.807 回答