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.
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()
never, ever use the variable
posts
in WordPress for custom loops. After changing the variable name toitems
innews.php
everything worked excellent.When I changed the variable
post
toitem
, it stopped working again…