get_template_part()的下划线来自核心文件夹

我需要listings_recently.php文件的子副本才能对其进行更改。我需要将此文件包含在我的子主题中:/themes/listingeasy/core/widgets/listings_recently.php

据我所知,我需要告诉主题来寻找该文件。那是我的孩子的主题functions.php做成的吗?

这是我的functions.php

get_template_part('get_stylesheet_directory() . "/core/widgets/listings"','recently');
get_template_part('get_stylesheet_directory() . "/core/widgets/listings_recently.php"');

两行都不行。

我该如何进行?这是文件夹架构:

themes
    listingeasy
        single.php
        core
            widgets
                listing_recently.php
    listingeasy-child
        functions.php
        single.php (I made changes here and its working)
        core
            widgets
                listing_recently.php (Changes here aren't working)
kkaren 回答:get_template_part()的下划线来自核心文件夹

我不知道你的主题。但是,当您要编辑listings_recently.php中的功能时,可以尝试将这些功能复制到子主题functions.php中并在其中进行编辑。大部分时间都可以。

关于汤姆

编辑:尝试require();在您的子主题themes.php中:

require('core/widgets/listings_recently.php');

编辑2 (请参阅注释,主题本身具有自己的get_template_directory())

get_template_directory() 

将路径返回到您的父模板目录。使用

get_stylesheet_directory() 

返回您的子主题目录的路径

,

我创建了二十一丁主题的子主题来重现您的场景。子主题的文件夹结构是这样的。

Folder Structure

  

functions.php

<?php
add_action( 'wp_enqueue_scripts','enqueue_child_theme_styles',PHP_INT_MAX);
function enqueue_child_theme_styles() {
  wp_enqueue_style( 'parent-style',get_template_directory_uri().'/style.css' );
}

get_template_part( 'core/widgets/listings_recently' );
  

listings_recently.php

<h1>Sagar Tamang</h1>;

<?php

您需要使用get_template_part( 'core/widgets/listings_recently' );包含带下划线的文件。如果您在functions.php中使用了get_template_part()函数,则输出似乎在HTML标记上方。

enter image description here

,

我相信我现在知道您正在尝试在这里做什么。看来您正在尝试将文件的副本添加到子主题中,以便可以编辑该文件,并使这些更改覆盖父主题,而不更改父主题的文件。 这不是对任意php文件的本地支持操作,仅对主题模板文件

您需要将其移动到主主题目录(与single.php相同),并为其指定适当的主题层次结构文件名。这就是为什么您对single.php文件所做的更改起作用的原因,而对于此特定文件却无效。 Single.php是已知的模板,您的文件只是您添加的php文件。

或者,您可以更改您的listings_recently.php文件,并在父主题中替换当前版本。如果您担心丢失旧版本,请将其保存到桌面或使用GitHub之类的版本控制系统。这可能是最好的方法。

第三种选择是对子主题的functions.php文件进行硬编码更改。

本文链接:https://www.f2er.com/3133262.html

大家都在问