Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My lovely colleagues have once again designed something that gives me a hard time getting it in Wordpress. Surprisingly it's something that I have done multiple times before; having recent posts and page content on one page.

The homepage is a page. Just above the page's content there are three recent posts. For the posts I need to display the first part of the content before the <!--more--> tag.

That loop seems to work fine. Later in the template I loop through the actual page content. No matter what I do, it always gives me dodgy results.

Here's a trimmed version of my index.php/page.php template (they happen to be identical):

<div id="content">
    <?php get_template_part('news'); ?>

    <?php
    $args = array(
        'hierarchical'  => false,
        'sort_column'   => 'menu_order'
    );

    $pages = get_pages($args);
    ?>

    <?php while(have_posts()): the_post(); ?>
        <?php $id = get_the_ID(); ?>

        <div id="page-content">
            <!-- lets have a dodgy menu here! -->
            <ul id="navigation">
                <?php foreach($pages as $page): ?>
                    <li<?php echo (($page->ID !== $id) ? '': ' class="active"'); ?>>
                        <a href="<?php echo get_page_link($page->ID); ?>"><?php echo $page->post_title; ?></a>
                    </li>
                <?php endforeach; ?>
            </ul>

            <div class="content-wrapper">
                <div class="subnav-wrapper">
                    <ul id="subnavigation">
                        <li><a href="#">jaarrekeningen</a></li>
                        <li><a href="#">administraties</a></li>
                        <li><a href="#">salarisverwerking</a></li>
                        <li><a href="#">fiscaal advies</a></li>
                        <li><a href="#">overig advies</a></li>
                    </ul>
                </div>

                <div class="content"><?php the_content(); ?></div>

                <div class="clear"></div>
            </div>
        </div>
    <?php endwhile; ?>

    <?php get_template_part('social'); ?>
</div>

As you can see, I'm including the news template. Here it is:

<?php
$args = array('numberposts' => 3);

$posts = get_posts($args);

global $more;
?>

<div id="news">
    <?php foreach($posts as $key => $post): setup_postdata($post); ?>
        <?php if($key > 0): ?><div class="news-item-splitter"></div><?php endif; ?>
        <?php $more = 0; ?>

        <div class="news-item">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

            <?php the_content('Lees meer'); ?>
        </div>
    <?php endforeach; wp_reset_postdata(); ?>

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

What am I doing wrong?

Thanks in advance.

share|improve this question
1  
Please explain "dodgy" –  s_ha_dum Nov 9 '12 at 14:49
    
@s_ha_dum not showing anything, showing the the titles of the pages correctly, but the content of the first post, etc. –  Tim S. Nov 12 '12 at 7:45

2 Answers 2

At the end of your news.php, use wp_reset_query(). This will revert to the default query for that page.

Hope this helps!

Codex Function Reference for wp_reset_query()

share|improve this answer
    
Now the content of the page shows the content of the first post... :( –  Tim S. Nov 12 '12 at 7:47
up vote 0 down vote accepted

never, ever use the variable posts in Wordpress for custom loops. After changing the variable name to items in news.php everything worked excellent.

When I changed the variable post to item, it stopped working again...

<?php foreach($items as $post): setup_postdata($post); ?>
    <!-- post -->
<?php endforeach; wp_reset_postdata(); ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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