【问题标题】:get_template_part() with underscore from core folder核心文件夹中带有下划线的 get_template_part()
【发布时间】:2020-03-05 18:46:53
【问题描述】:

我需要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)

【问题讨论】:

  • 您打算在其他地方使用此代码吗?通常你不会使用这个函数向functions.php添加东西,你会使用require()。 get_template_part 在将代码片段应用到多个其他页面模板时,或者在多个地方使用时更频繁地使用,因此您不必每次都重写代码。我对您要完成的工作感到有些困惑。
  • 这个文件在哪里? /themes/listingeasy/core/widgets/listings_recently.php 是在子主题还是父主题或其他主题?
  • @Sagar Bahadur Tamang,我已经用文件夹架构编辑了这个问题,以防万一。
  • @Dubvader 我已经使用文件夹架构编辑了问题,以防万一。我想对我的主题进行更改。我需要修改的文件是listings_recently.php,但没有考虑更改。
  • @Flowen 我认为这会有所帮助:stackoverflow.com/questions/40961370/…

标签: wordpress templates wordpress-theming themes parent-child


【解决方案1】:

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

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标签的上方。

【讨论】:

  • 这对我不起作用。我用文件夹模式编辑了问题,以防万一。
【解决方案2】:

我不知道你的主题。但是当您想编辑listings_recently.php 中的函数时,您可以尝试将这些函数复制到您的child-themes functions.php 并在那里进行编辑。大多数时候它都有效。

问候汤姆

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

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

编辑2(见评论,主题本身有自己的get_template_directory())

get_template_directory() 

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

get_stylesheet_directory() 

返回子主题目录的路径

【讨论】:

  • 嗨,汤姆。我不是要编辑函数。我正在尝试将listings_recently.php 文件复制到我的子主题中,以便进行自定义修改。问题是,一旦复制,我需要指定我正在使用该文件,我不知道具体是怎么回事,因为它有一个下划线并且它显然不起作用。
  • 不工作。主题本身有他自己的require_once(get_template_directory() . "/core/widgets/listings_recently.php");我正在编辑这个问题,因为我尝试了更多的东西。
  • get_template_directory() 返回父模板目录的路径。使用 get_stylesheet_directory() 返回子主题目录的路径
【解决方案3】:

我相信我现在明白了你在这里想要做什么。您似乎正在尝试将文件的副本添加到您的子主题中,以便您可以编辑该文件,并让这些更改覆盖父主题,而无需更改父主题的文件。 这不是对任意 php 文件的原生支持,仅对主题模板文件提供支持

您需要将其移入主主题目录,与 single.php 所在的位置相同,并为其指定适当的主题层次结构文件名。这就是为什么您对 single.php 文件所做的更改有效,而对于这个特定文件则无效。 Single.php是一个已知的模板,你的文件只是你添加的一个php文件。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 2017-04-13
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多