• Resolved shaun1981

    (@shaun1981)


    I’ve got a static home page, but i have an area at the top of the page that shows the most recent blog posts. Trouble is now the static home page text is gone and it shows the blog posts underneath this section too.

    Here’s the code:

    ?<?php
    /*
    Template Name: Homepage
    */
    ?>
    
    <?php get_header(); ?>
    <div id="news">
    <div id="featured">
    <h1>Latest News</h1>
    <?php query_posts('showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    </div>
    <div id="headlines">
    <h2>News Headlines</h2>
    <?php query_posts('showposts=3&offset=1'); ?>
    <ul>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    </div>
    <p class="clear">
    
    </div>
    <div id="contenthome">
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h2><?php the_title(); ?></h2>
    			<div class="entry">
    				<?php the_content('<p class="serif"><strong>Read the rest of this page &raquo;</strong>
    '); ?>
    
    				<?php link_pages('<strong>Pages:</strong> ', '
    ', 'number'); ?>
    
    			</div>
    		</div>
    	  <?php endwhile; endif; ?>
    	<?php edit_post_link('Edit post.', '', '
    '); ?>
    
    </div>
    
    <?php get_footer(); ?>

    Here is a link to the page, basically underneath the blue div showing the latest news should be the static home page text rather than the blog posts.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Your query_posts() calls are overriding the default query, which would be the static page’s content.

    Take a look at the “Multiple Loops Example 2” section in “The Loop” (see link, below). There, the default, automatic query (which here, should be your static page content) is saved in a variable, then the query is called later.

    Applying that example to your template, at the top of the file, put <?php $temp_query = $wp_query; ?>. Then, right after the <div class="contenthome">, put <?php $wp_query = $temp_query; ?>.

    In the alternative, you might be able to use <?php rewind_posts(); ?> before your third loop (right after the “contenthome” div. Or you might be able to use the query_posts() function to call a specific page — the page you’re using for the static home page.

    More info:
    Codex – The Loop
    Codex – Query Posts
    Forum Topic: How to query posts after querying pages
    Forum Topic: How to reset or rewind query_posts

    Thread Starter shaun1981

    (@shaun1981)

    Thanks. I have managed to get the dynamic content and static content displaying on the page now with just one problem. I want the dynamic “news” section at the top of the page above the static content but can’t get that to work.

    Here’s my code now:

    <?php
    /*
    Template Name: Homepage
    */
    ?>
    
    <?php get_header(); ?>
    
    	<div id="content">
    
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h2><?php the_title(); ?></h2>
    			<div class="entry">
    				<?php the_content('<p class="serif"><strong>Read the rest of this page &raquo;</strong></p>'); ?>
    
    				<?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
    
    			</div>
    		</div>
    	  <?php endwhile; endif; ?>
    
    </div>
    
    <?php $temp_query = $wp_query; ?> 
    
    <div id="news">
    <div id="featured">
    <h1>Latest News</h1>
    <?php query_posts('showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    </div>
    <div id="headlines">
    <h2>News Headlines</h2>
    <?php query_posts('showposts=3&offset=1'); ?>
    <ul>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    </div>
    <p class="clear"></p>
    </div>
    
    <?php get_footer(); ?>

    I tried re-arranging the code so that the news section was above but when i do that it loses the static content and just displays the full posts. Any ideas?

    Thread Starter shaun1981

    (@shaun1981)

    Ok i think i have resolved this using query_posts. Here’s the code for anyone interested

    <?php
    /*
    Template Name: Homepage
    */
    ?>
    
    <?php get_header(); ?>
    
    <?php $temp_query = $wp_query; ?> 
    
    <div id="news">
    <div id="featured">
    <h1>Latest News</h1>
    <?php query_posts('showposts=1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
    <?php the_excerpt(); ?>
    <?php endwhile; ?>
    </div>
    <div id="headlines">
    <h2>News Headlines</h2>
    <?php query_posts('showposts=3&offset=1'); ?>
    <ul>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    </div>
    <p class="clear"></p>
    </div>
    
    <div id="content">
    
     <?php
    
    query_posts('page_id=5');
    
    global $more;
    $more = 0; 
    
    while (have_posts()) : the_post();
      the_content('Read the full post ?');
    endwhile;
    ?>
    
    </div>
    
    <?php get_footer(); ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Can anyone help me with this? Static homepage’ is closed to new replies.