I'm trying to get first the content of a single page (the one that is queried), and then in a small section below i want to display the title of my bloggposts as a "Latest News" section.
The problem is that if i first use <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
to get the page content. And then try to use $posts_array = get_posts( $args );
to get the blogposts i just get the heading of the page twice. Obviously the get_posts
function takes on the id of the page instead of looping through the blogposts. Is there a way to tell get_posts to search for the first 5 blogpost id's and not the current page id?
Grateful for all answers, suggestions, tips or hints :)
here is my code:
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="mcenter">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="article">
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'monster' ), 'after' => '</div>' ) ); ?>
<?php edit_post_link( __( 'Edit', 'monster' ), '<span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php endwhile; // end of the loop. ?>
<?php
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => ,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' => ,
'post_type' => 'post',
'post_mime_type' => ,
'post_parent' => ,
'post_status' => 'publish');
$posts_array = get_posts( $args );
?>
<div id="news">
<div id="news-roll" class="mcenter">
<h3><?php _e('Siste nytt:', 'monster'); ?></h3>
<?php
$count = 1;
foreach ($posts_array as $monster_news) {
setup_postdata($monster_news);
if ($count == 1) {
echo '<p class="first-news">';
} else {
echo '<p>';
}
the_title();
echo '</p>';
}
$count = null;
?>
</div>
</div>
get_posts()
is completely unaware of context and should only use arguments you pass to it. What exactly do you get in$posts_array
?