I'm trying to set a custom static front page for my Wordpress, with a custom template.
I create landingpage.php
in wp-content/themes/my_theme
and I create a new page in my wordpress admin with LandingPage model selected.
If I go to my_website.com/landing-page/
my custom page works and 'Hello World!' appears, but if I set my custom page as a static front page and go to my_website.com/
my 'Hello World!' message doesn't display.
My landingpage.php
code:
<?php /* Template Name: LandingPage */ ?>
<?php get_header(); ?>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/page/content', 'page' );
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile;
?>
<h1>Hello World!</h1>
</main>
</div>
</div>
<?php get_footer();
Any idea? thx
front-page.php
has precedence over your custom template.
When that post is viewed as a page (e.g. by using its permalink url, /landing-page/), the custom template you defined (landingpage.php
) will be used.
But if the same page is accessed as a front-page, the template for static front page is used, and that one is called front-page.php
(if it exists).
Either delete your front-page.php
template, or modify it so it mimics the behavior your wanted in landingpage.php
.
The corollary for this is that for a static front page, you don't need to assign a specific custom template if you have a front-page.php
template. You can use any page whatsoever, and that template will apply.
You should name your file as front-page.php
instead, wordpress will automatically detect that and will load that instead. https://developer.wordpress.org/themes/basics/template-hierarchy/