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.

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>
share|improve this question
2  
Obviously the get_posts function takes on the id of the page - this is very far from obvious, get_posts() is completely unaware of context and should only use arguments you pass to it. What exactly do you get in $posts_array? –  Rarst Mar 10 '12 at 17:44
    
oh.. I just assumed this because i got the same title as a result in both cases. it turns out when i use the_title(); it gives me the title from the first page loop, to the get_posts call. So when i ran print_r on $monster_news i got the right data. I guess i have to point to a individual variable to get to the title out of the array? or should i be able to use the_title(); –  Ole Henrik Skogstrøm Mar 10 '12 at 19:41
    
the result i get is this: stdClass Object ( [ID] => 1 [post_author] => 1 [post_date] => 2012-03-06 13:29:43 [post_date_gmt] => 2012-03-06 13:29:43 [post_content] => Welcome to WordPress. This is your first post. Edit or delete it, then start blogging! [post_title] => Hello world! [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => open [post_password] => [post_name] => hello-world [to_ping] => [pinged] => [post_modified] => .... and so on :) –  Ole Henrik Skogstrøm Mar 10 '12 at 19:46

1 Answer 1

up vote 4 down vote accepted

Change:

foreach ($posts_array as $monster_news) { 
                    setup_postdata($monster_news);

To:

foreach ($posts_array as $post) { 
                    setup_postdata($post);

It seems setup_postdata() actually doesn't modify $post itself (which was news to me).

PS also loose empty , in array... I am not sure how it actually works for you with that.

share|improve this answer
    
your correct in both cases :) just figured it out my self. But this confirms it. :) I did not think that it was important to use '$post' rather than just any $var. I also got an error trying to run it with the empty variables in the array. forgot to update my question :) –  Ole Henrik Skogstrøm Mar 10 '12 at 20:47

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.