【问题标题】:how to change parent theme directory url in child theme pointing to child theme directory如何更改子主题中的父主题目录url指向子主题目录
【发布时间】:2020-04-10 20:49:27
【问题描述】:

我是 Wordpress 方面的新手。 我刚刚创建了一个子主题,需要您在 php 编码和 functions.php 相关方面的帮助。

我已经从父主题 (assets/inc/header-related.php) 复制了它,并将其放入子主题文件夹中,保持文件夹的结构。

在 header-related.php 中有一个我要编辑的徽标部分。这是下面的代码

* Logo */
function thb_logo( $section = false ) {
    $logo = ot_get_option( 'logo', Thb_Theme_Admin::$thb_theme_directory_uri. 'assets/img/logo.png' );
    $loading = 'auto';
    $classes[] = 'logo-holder';
    if ($section == 'fixed-logo') {
        $logo = ot_get_option( 'logo_fixed', $logo );
        $classes[] = 'fixed-logo-holder';
        $loading = 'lazy';
    } elseif ($section == 'mobile-logo') {
        $logo = ot_get_option( 'mobile_logo', $logo );
        $classes[] = 'mobile-logo-holder';
        $loading = 'lazy';
    } elseif ($section == 'logo_mobilemenu') {
        $logo = ot_get_option( 'logo_mobilemenu', $logo );
        $classes[] = 'mobilemenu-logo-holder';
        $loading = 'lazy';
    }
    ?>
    <div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
        <a href="<?php echo esc_url( home_url('/') ); ?>" class="logolink" title="<?php bloginfo('name'); ?>">
            <img src="<?php echo esc_url( $logo ); ?>" loading="<?php echo esc_attr( $loading ); ?>" class="logoimg logo-dark" alt="<?php bloginfo('name'); ?>" />
        </a>
    </div>
    <?php
}
add_action( 'thb_logo', 'thb_logo', 2, 1 );

我想更换主题目录

$logo = ot_get_option( 'logo', Thb_Theme_Admin::$thb_theme_directory_uri. 'assets/img/logo.png' 

这样它就从子主题而不是父主题中指向 assets/img/logo.png,就像它现在正在做的那样。我也将其更改为 logo.svg 并将 svg 添加到文件夹中,但它似乎再次将其从父目录中取出。

现在,我需要做什么才能使其指向子主题文件夹?我应该将它添加/排队到我的子主题功能中吗?如果是的话,你知道怎么做吗?

我不确定是否应该在我的子主题中上传文件夹结构,或者只是在 functions.php 中添加代码

如果有人可以指导我,我将不胜感激,如果听起来令人困惑,我会道歉,但希望我的问题可以理解。谢谢!

P.S 这是我的functions.php 的样子:

    <?php /*

  This file is part of a child theme called rekuin2020.
  Functions in this file will be loaded before the parent theme's functions.
  For more information, please read
  https://developer.wordpress.org/themes/advanced-topics/child-themes/

*/

// this code loads the parent's stylesheet (leave it in place unless you know what you're doing)

function your_theme_enqueue_styles() {

    $parent_style = 'parent-style';

    wp_enqueue_style( $parent_style, 
      get_template_directory_uri() . '/style.css'); 

    wp_enqueue_style( 'child-style', 
      get_stylesheet_directory_uri() . '/style.css', 
      array($parent_style), 
      wp_get_theme()->get('Version') 
    );
}

add_action('wp_enqueue_scripts', 'your_theme_enqueue_styles');

/*  Add your own functions below this line.
    ======================================== */ 

【问题讨论】:

  • 这是我的函数的样子:

标签: php css wordpress


【解决方案1】:
$logo = ot_get_option( 'logo', Thb_Theme_Admin::$thb_theme_directory_uri. 'assets/img/logo.png' 

这是一个主题选项,它指向父主题文件夹中的图像文件

为了能够从您的子主题目录中获取图像,您需要将 $thb_theme_directory_uri 更改为 $thb_stylesheet_directory_uri。此外,您还必须将带有图像的资产文件夹复制到子主题。

我不确定,但请尝试一下。

此外,如果您想对您的主题进行自定义,您应该始终从子主题中执行此操作。 如果你想添加 CSS,你应该在子主题目录中创建 CSS 文件并将其加入你的函数 PHP 文件中

wp_enqueue_style( 'style', get_stylesheet_directory_uri() . '/style.css' );

喜欢这个并将其包装在函数中并添加动作挂钩“wp_enqueue_scripts”

如果您想编辑主题 PHP 文件,您应该将其复制到子主题并在那里进行更改。小心并跟进文件夹结构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2010-10-17
    • 2016-02-24
    相关资源
    最近更新 更多