admin管理员组

文章数量:1313081

I am trying to make a page to be persistent as the home page for my custom theme. In which I can download from Github and setup the Wordpress and this page appears as the default one. I created a page within the theme as front-page.php and added the following code within it:

<?php
/* Template Name: front-page */

get_header();
?>


<div id="primary" class="content-area">
        <main id="main" class="site-main">

        <?php
        if ( have_posts() ) {

            // Load posts loop.
            while ( have_posts() ) {
                the_post();
                get_template_part( 'template-parts/content/content' );
            }

            // Previous/next page navigation.
            twentynineteen_the_posts_navigation();

        } else {

            // If no content, include the "No posts found" template.
            get_template_part( 'template-parts/content/content', 'none' );

        }
        ?>

        </main><!-- .site-main -->
    </div><!-- .content-area -->

<?php
get_footer();
?>

However, it just appears as normal template. The same as if I created a books.php page and setup it as the template name as well. I know it can be done from setthings>reading but I just would like to set it as the default one.

I have also added the following code into the functions.php to display the front-page.php template as the default but it makes the homepage disappear.

/*
* Add custom template as default page
*/ 

add_filter( 'template_include', 'breed', 99 );

function breed( $template ) {

    if ( is_singular( 'page' )  ) {
        $default_template = locate_template( array( 'breed.php' ) );
        if ( '' != $default_template ) {
            return $default_template ;
        }
    }

    return $template;
}

I am trying to make a page to be persistent as the home page for my custom theme. In which I can download from Github and setup the Wordpress and this page appears as the default one. I created a page within the theme as front-page.php and added the following code within it:

<?php
/* Template Name: front-page */

get_header();
?>


<div id="primary" class="content-area">
        <main id="main" class="site-main">

        <?php
        if ( have_posts() ) {

            // Load posts loop.
            while ( have_posts() ) {
                the_post();
                get_template_part( 'template-parts/content/content' );
            }

            // Previous/next page navigation.
            twentynineteen_the_posts_navigation();

        } else {

            // If no content, include the "No posts found" template.
            get_template_part( 'template-parts/content/content', 'none' );

        }
        ?>

        </main><!-- .site-main -->
    </div><!-- .content-area -->

<?php
get_footer();
?>

However, it just appears as normal template. The same as if I created a books.php page and setup it as the template name as well. I know it can be done from setthings>reading but I just would like to set it as the default one.

I have also added the following code into the functions.php to display the front-page.php template as the default but it makes the homepage disappear.

/*
* Add custom template as default page
*/ 

add_filter( 'template_include', 'breed', 99 );

function breed( $template ) {

    if ( is_singular( 'page' )  ) {
        $default_template = locate_template( array( 'breed.php' ) );
        if ( '' != $default_template ) {
            return $default_template ;
        }
    }

    return $template;
}
Share Improve this question asked Dec 13, 2020 at 18:50 Elias PradoElias Prado 1135 bronze badges 1
  • "On the site front page, WordPress will always use the front-page.php template file, if it exists." - more details here. – Sally CJ Commented Dec 14, 2020 at 1:45
Add a comment  | 

1 Answer 1

Reset to default 0

this is done without any code, in the dashboard using the template presented or any other and has no need for the function added to functions.php. try this steps:

  • Pages > add new: create new page with any name adding the title and publish;
  • Pages > all pages > quick-edit(on the page created): on templates choose the page template of choice, for example "front-page" and update;
  • Appearance > customize > homepage settings: on the " Your homepage displays" choose "a static page", on "home page" choose your page created that now displays your template and update.

this should set your home page.

本文标签: How to make a page as default page for a theme without a plugin