0

I am creating one page wordpress theme - i am new to wordpress dev. The one page website have sections which represents pages. So every section on front page is page created via admin panel.

There is page.php and page-home.

Front-page needs to display all pages as a sections.

So I started with loop inside front-page.php:

<?php get_header(); ?>

<?php 

    $query = new WP_Query('pagename=home');
    if ( $query->have_posts () ) : while ( $query->have_posts() ) : $query->the_post();

            get_template_part("page-home");

        endwhile; endif;
        wp_reset_postdata();

 ?>

<?php get_footer(); ?>

First problem is: get_template_part not display the_content() from page-home.php, only displays html markup.


Here is the page-home.php

<?php
/*
Template Name: Home
*/
?>

<?php get_header(); ?>
        <section id="first_section">
        <img class="jumbo wow bounceInDown animated" data-wow-delay="2s" src="<?php echo get_template_directory_uri(); ?>/images/jumbo-illustration.png" alt="Webdesigner and Developer" />
        <div class="tagline-wrapper wow bounceInLeft animated" data-wow-delay="2.5s">
        <!--  IF the_content() is placed here, it displays the content on front-page -->
        <h3 class="tagline"><?php the_content(); ?></h3>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <h3 class="tagline"><?php the_content(); ?></h3>
    <?php endwhile; ?>
    </div>
    <?php   else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?> 


    </section>


<?php get_footer(); ?>

Notice <h3 class="tagline"><?php the_content(); ?></h3> Front page wont render the_content if is in the loop of , but if is before loop it displays normaly.

What I am missing?

Tnx in advance

1

The problem is that you are running a loop from secondary query in front-page.php and the loop from main in page-home.php. So, the post data in page-home.php won't be the data from the WP_Query in front-page.php.

Additionally, you are trygin to get full template files as template parts, which is not correct. I mean, front-page.php is a full template will header, content and footer and page-fome.php is also a full template with header, content and footer. You should re-think the logic to split template parts.

A basic (and silly) example to show you a correct approach:

front-page.php:

<?php get_header(); ?>

<?php 

    $query = new WP_Query('pagename=home');
    if ( $query->have_posts () ) : while ( $query->have_posts() ) : $query->the_post();

            get_template_part("content");

        endwhile; endif;
        wp_reset_postdata();

 ?>

<?php get_footer(); ?>

page-home.php:

<?php
/*
Template Name: Home
*/
?>

<?php get_header(); ?>
        <section id="first_section">
        <img class="jumbo wow bounceInDown animated" data-wow-delay="2s" src="<?php echo get_template_directory_uri(); ?>/images/jumbo-illustration.png" alt="Webdesigner and Developer" />
        <div class="tagline-wrapper wow bounceInLeft animated" data-wow-delay="2.5s">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        get_template_part("content");

    <?php endwhile; ?>
    </div>
    <?php   else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?> 


    </section>


<?php get_footer(); ?>

content.php:

<h3 class="tagline"><?php the_content(); ?></h3>
  • Thank you @cybmeta! Yes, it is logic. I was created this <pre><?php get_header(); ?> <?php // 1 SECTION // HOME get_template_part('loop', 'home'); // 2 SECTION // ABOUT get_template_part('loop', 'about'); // 3 SECTION // SERVICES get_template_part('loop', 'services'); // 4 SECTION // SKILL get_template_part('loop', 'skillset'); // 5 SECTION // CONTACT get_template_part('loop', 'contact'); ?> <?php get_footer(); ?></pre> – user200260 Aug 9 '15 at 8:44
  • Please, if this answer help you and answers your question, mark it as accepted answer so other users can find it easily. – cybmeta Aug 9 '15 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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