1

I just started building a custom theme so I only have the core files with a header.php and footer.php file.
So far all I've done in my functions.php file is enqueue my css and js so I haven't added any theme support for anything.
So far my files look like this:

header.php

<!doctype html>
<html>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>

    <?php wp_body_open(); ?>
    
    
    <header class="testB">
        <p>header section</p>
    </header>

footer.php

    <footer>
        <p>footer</p>
        
    </footer>

    <?php wp_footer(); ?>
</body>
</html>

index.php

<?php get_header(); ?>

<p class="test">body</p>

<?php get_footer(); ?>

So far the only markup that displays is the <p>body</p> in the index.php file.
I studied the code to the twentytwentyone theme and don't see anything that particularly appears to enable the ability for our markup to render.
I haven't created any other template parts yet besides the header and footer.
The only thing I'm doing differently is instead of having the header and footer in the root directory I have them in a folder called framework and in their respective folders.
So for example the path to the header from the root directory is framework/header/header.php.

Is there a WP standard that says that has to be in the root directory?

I come from an Angular background so I prefer to sort every tiny chunk of code into their own folders and sub folders.
Can any of you spot what I'm doing wrong here?

1
  • 1
    You would need to use require or get_template_part to get the header.php if its inside a folder/sub-folder. WordPress searches header.php, footer.php and a few others in the root of your theme and no where else. Aug 19, 2021 at 7:05

1 Answer 1

-1

get_header() will load wp-content/themes/YOUR-THEME/header.php by default.
If you want to grab other php file use

<?php require_once 'framework/header/header.php'; ?>

Other way is using get_template_part

Then use following code anywhere you want

<?= get_template_part('framework/header/header') ?>
5
  • There's no reason to use require_once. Aug 19, 2021 at 9:18
  • @JacobPeattie I have to disagree. It would be better to have a wordpress error if, for some reason header.php is not available than seeing a broken site. Sense header.php contains the opening doctype and wp_head() and many other meta, script, styles pixles etc... there would be no point showing the site without them. Aug 19, 2021 at 10:00
  • The correct way to load templates in WordPress is get_template_part(). require_once, as used in this answer is also not compatible with child themes. Not to mention that header.php being missing would be very obvious without needing to use the wrong function. There's no circumstances where header.php should go missing anyway. See wordpress.stackexchange.com/a/5195/39152 Aug 19, 2021 at 10:06
  • @JacobPeattie why not? i've never used child theme so what's wrong with it?
    – farhangQ
    Aug 20, 2021 at 8:39
  • Because get_template_part() uses functions to check child themes for templates first. require_once is a PHP expression that doesn't take any of that into account. You could combine require_once with functions for finding templates, but this answer doesn't do that, and there's no point if get_template_part() already does it. Additionally get_template_part() and require_once handle variable scoping differently which could lead to unexpected results. Aug 20, 2021 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.