get_template_part() with underscore from core folder get_template_part() with underscore from core folder wordpress wordpress

get_template_part() with underscore from core folder


I created a child theme of the twentyninteeen theme to reproduce your scenario. The folder structure of the child theme is like this.

Folder Structure

functions.php

<?phpadd_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

You need to use get_template_part( 'core/widgets/listings_recently' ); to include files with underscore. If you have used the get_template_part() function in the functions.php, the output seems to be above the HTML tag.

enter image description here


I don`t know your theme. But when you want to edit the functions in listings_recently.php, then you can try to copy these functions to your child-themes functions.php and edit them there. Most of the time it works.

Regards Tom

Edit: try require(); in your child themes functions.php:

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

Edit 2 (see comment, the theme itself has its own get_template_directory())

get_template_directory() 

returns the path to your parent template directory. Use

get_stylesheet_directory() 

to return the path of your child-themes directory


I believe I see now what you're trying to do here. It appears you are trying to add a copy of the file to your child theme, so that you can edit that file, and have those changes overwrite the parent, without changing the parent theme's file. This is not a natively supported action on arbitrary php files, only on theme template files.

You need to move it in to the primary theme directory, same as where single.php is, and give it an appropriate theme hierarchy filename. That is why your changes for single.php file work, and for this particular file do not. Single.php is a known template, your file is just a php file you added.

Alternatively, you can make changes to your listings_recently.php file, and replace the current version in the parent theme. If you're worried about losing the old version, save it to your desktop or use a version control system like GitHub. This is probably the best way to do it.

Third option is to make hard coded changes to the functions.php file of the child theme.