简体   繁体   中英

Loading custom pages with get_template_part()

I came across this function the get_template_part by talk to other person about what easy why to load custom page on front-end page he told me the get_template_part and i think its awesome for reuse of code. But him use it to loaded all my custom pages on front-page but when I view the site some content loads and some does not load on the site here screenshot.

在此处输入图片说明

    <?php
/**
 * Template Name: Front-Page
 */

get_header(); ?>

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

            <?php while (have_posts()) : the_post(); ?>
                <?php get_template_part('templates/header'); ?>
                <?php get_template_part('templates/page-about'); ?>
                <?php get_template_part('templates/page-services'); ?>
                <?php get_template_part('templates/page-photography'); ?>
                <?php get_template_part('templates/page-portfolio'); ?>
                <?php get_template_part('templates/page-contact'); ?>
                <?php get_template_part('templates/footer'); ?>
            <?php endwhile; ?>

    </main><!-- #main -->
</div><!-- #primary -->

The problem is most likely either in the template files themselves, you're missing the template files, or your file names don't match your function call. For example to show your About page this code

<?php get_template_part('templates/page-about'); ?>

Means you should have a directory in your theme folder named 'templates' and a file in that directory called page-about.php. If all your files are present and correctly named you need to look in the template files themselves and check the code there

Based on the additional info you've posted on Linkedin, about the site being a one-page website, your problem is most likely because you're using the wrong file to load the homepage.

To solve this, do the following in the theme's folder:

  1. Create a file called "home.php"
  2. Move the contents of "page-home.php" to "home.php"
  3. Remove the while loop so that home.php will look like this:

 <?php get_header(); ?> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php get_template_part('templates/header'); ?> <?php get_template_part('templates/page-about'); ?> <?php get_template_part('templates/page-services'); ?> <?php get_template_part('templates/page-photography'); ?> <?php get_template_part('templates/page-portfolio'); ?> <?php get_template_part('templates/page-contact'); ?> <?php get_template_part('templates/footer'); ?> </main><!-- #main --> </div><!-- #primary --> <?php get_footer(); ?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM