Ask your WordPress questions! Pay money and get answers fast! (more info)

Content repeating on page WordPress

  • SOLVED

If I create a custom home.php file on my site using the Automatic Underscores theme, the blog page repeats all content above the footer twice.

I've built with Underscores before and not seen this. Have disabled all plugins...can't figure it out.

Here's the complete code for the home.php file:



<?php
/**
* The header for our theme.
*
* Displays all of the <head> section and everything up till <div id="content">
*
* @package Test Site
*/

?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">

<?php $post_id = 34;echo get_post_meta($post_id, 'google_font_one', true);?>
<?php $post_id = 34;echo get_post_meta($post_id, 'google_font_two', true);?>
<?php $post_id = 34;echo get_post_meta($post_id, 'google_font_three', true);?>
<?php $post_id = 34;echo get_post_meta($post_id, 'google_font_four', true);?>
<?php $post_id = 34;echo get_post_meta($post_id, 'google_font_five', true);?>

<style media="screen" type="text/css">

.home-form-submit{background:<?php $post_id = 34;echo get_post_meta($post_id, 'form_button_background_color', true);?> !important;}
.home-form-submit:hover {background:<?php $post_id = 34;echo get_post_meta($post_id, 'form_button_background_color_on_hover', true);?> !important;}
.footer-form-submit{background:<?php $post_id = 34;echo get_post_meta($post_id, 'form_button_background_color', true);?> !important;}
.footer-form-submit:hover {background:<?php $post_id = 34;echo get_post_meta($post_id, 'form_button_background_color_on_hover', true);?> !important;}
.main-navigation li {padding:.25em 1em .25em 1em;font-size:<?php $post_id = 7;echo get_post_meta($post_id, 'nav_font_size', true);?>px !important;<?php $post_id = 7;echo get_post_meta($post_id, 'nav_font_family', true);?>}
.main-navigation li > a:link {color:<?php $post_id = 7;echo get_post_meta($post_id, 'nav_font_color', true);?>}
.main-navigation li > a:hover {color:<?php $post_id = 7;echo get_post_meta($post_id, 'nav_font_color_on_hover', true);?>}
.main-navigation li > a:visited {color:<?php $post_id = 7;echo get_post_meta($post_id, 'nav_font_color', true);?>}

</style>

<?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>

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

<div class="container">
<div class="row">

<div class="top" style="min-height:<?php $post_id = 34;echo get_post_meta($post_id, 'top_minimum_height', true);?>px">

<div class="top-one">

<?php if (get_field('logo_image',34)) { ?>
<div class="site-logo" itemscope itemtype="http://schema.org/Organization" style="width:<?php the_field('logo_image_width',34); ?>px;padding-top:<?php the_field('logo_top_padding',34); ?>px;"><span itemprop="logo"><img src="<?php the_field('logo_image',34); ?>" alt="<?php the_field('site_name',34); ?>"></span></div>
<?php } else { ?>
<div class="site-logo-text" style="padding-top:<?php the_field('logo_top_padding',34); ?>px;color:#000;font-size:<?php the_field('logo_text_size',34); ?>px;<?php the_field('google_font_family_for_logo',34); ?>"><?php the_field('logo_text',34); ?></div>
<?php } ?>

</div>

<div class="top-two">

<?php

$values = get_field('show_top_search_form',7);

if(in_array("Yes", $values )){
?>
<div class="top-search-form">

<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">

<input type="text" id="top-search" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
<input type="submit" id="top-search-submit" value="Search" class="footer-button" />

</form>

</div>
<?
}

?>

</div>

</div>

<div class="navigation">
<nav id="site-navigation" class="main-navigation" role="navigation">
<button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false"><?php _e( 'Primary Menu', 'test-site' ); ?></button>
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_id' => 'primary-menu' ) ); ?>
</nav><!-- #site-navigation -->

<div class="clear"></div>
</div>

</div>
</div>

<?php endwhile; ?>

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

<div class="container">
test
</div>

<?php wp_link_pages(array('before' => '<nav class="pagination">', 'after' => '</nav>')); ?>
<?php endwhile; ?>

<div class="container">
<div class="row">

<div class="footer box-sizing" style="background:<?php $post_id = 34;echo get_post_meta($post_id, 'footer_background_color', true);?>;">

<div class="footer-left">

<div class="footer-form-title"><?php the_field('footer_form_title',7); ?></div>
<div class="footer-form-intro"><?php the_field('footer_form_intro',7); ?></div>

<?php echo do_shortcode('[mc4wp_form]'); ?>
</div>

<div class="clear" style="height:1em;"></div>

<?php while( have_rows('social_media',7) ): the_row();

$icon = get_sub_field('social_media_icon');
$label = get_sub_field('social_media_label');
$link = get_sub_field('social_media_link');

?>

<div class="footer-social-icon"><a href="<?php echo $link; ?>" target="_blank"><img src="<?php echo $icon; ?>" alt="<?php echo $label; ?>"></a></div>

<?php endwhile; ?>

<div class="clear"></div>

</div>

<div class="footer-bottom box-sizing">
<div class="container">
<div class="row">

<div style="text-align:center;" itemtype="http://schema.org/Organization">&copy; <?php echo date('Y'); ?> <span itemprop="legalName"><?php the_field('site_name',7); ?></span></div>

<div class="footer-test-site-logo"><img src="<?php echo get_bloginfo('url'); ?>/media/logo-test-site.png" alt="I'm an Test User"></div>
<div class="footer-test-site-link"><a style="color:#000;" href="<?php the_field('og_profile_link',7); ?>" target="_blank">I'm an Test User</a></div>

</div>
</div>
</div>
</div>
</div>

<?php wp_footer(); ?>

</body>
</html>


Answers (2)

2015-03-27

Zulfikar Nore answers:

You have two instances of `<?php while (have_posts()) : the_post(); ?>` and `<?php endwhile; ?>`.

Remove the first instance from the code and see what happens.

Zulf


Kyler Boudreau comments:

Zulfikar,

I changed from using <?php while (have_posts()) : the_post(); ?> to introduce the loop to <?php if ( have_posts() ) : ?> and the problem went away.

Do you know why I couldn't use the other method?


Zulfikar Nore comments:

<?php if ( have_posts() ) : ?> checks to see if you have any published posts and while ( have_posts() ) : the_post(); initiates the loop quering the post and returns the content.


Kyler Boudreau comments:

I see. So I doubled up the latter, and that broke it. Gotcha. Thanks man.

2015-03-27

timDesain Nanang answers:

you shouldn't use <strong>main query</strong> multiple times on one page

- main query (https://codex.wordpress.org/Function_Reference/query_posts)
if(have_posts()) :
while(have_posts()) :
//your stuff here
endwhile;
endif;


- use additional custom query instead (https://codex.wordpress.org/Class_Reference/WP_Query)
$args = array (
'ignore_sticky_posts' => 1,
'posts_per_page' => 12,
'post_type' => array('post_type_here'),
'post_status' => array('publish'),
);
$wpq = new WP_Query($args);

if($wpq->have_posts()) :
while($wpq->have_posts()) : $wpq->the_post();
//your stuff here
endwhile;
wp_reset_postdata();
endif;