Products
GG网络技术分享 2025-03-18 16:12 0
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
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我正在尝试使用自定义模板为我的Wordpress设置自定义静态首页。</ p>
我在 wp-content / themes / my_theme </ code>中创建
landingpage.php </ code>,然后在我的wordpress管理员中创建一个新页面并选择了LandingPage模型。< / p>
如果我转到 my_website.com/landing-page / </ code>我的自定义页面工作和\'Hello World!\' 出现,但是如果我将自定义页面设置为静态首页并转到
my_website.com / </ code>我的\'Hello World!\' 消息不显示。</ p>
我的 landingpage.php </ code>代码:</ p>
&lt;?php / * 模板名称:LandingPage * /?&gt; &lt;?php get_header(); ?&gt;
&lt; div class =“wrap”&gt;
&lt; div id =“primary”class =“content-area”&gt;
&lt; main id =“main”class =“site-main” role =“main”&gt;
&lt;?php
while(have_posts()):the_post();
get_template_part(\'template-parts / page / content\',\'page\');
if(comments_open ()|| get_comments_number()):
comments_template();
endif;
endwhile;
?&gt;
&lt; h1&gt; Hello World!&lt; / h1&gt;
&lt; / main&gt; \\ n&lt; / div&gt;
&lt; / div&gt;
&lt;?php get_footer();
</ code> </ pre>
任何想法? thx </ p>
</ div>
网友观点:
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/
Demand feedback