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 sort of a noob when it comes to Wordpess; I've only recently started building themes and I've run into a wall. I'm trying to feature 3 different articles at the top of my site using wpquery and for some reason it's only displaying one. I'll include my code below and if anyone can help my figure out what's wrong I would be very grateful!

Code included in pastebin: http://pastebin.com/1DB7vent

I'm trying to get a setup similar to this: [div class="site_width"] [ul] [li] [a h*ref="FeaturedLink1"] [i*mg s*rc="FeaturedLink1 Featured Image] [/a] [label] [a h*ref="FeaturedLink1"] "Featured Link Title" [/a] [/label] [/li] [Repeat format for two more posts] [/ul] [/div]

[]'s & *'s included to prevent se from thinking I'm spamming

share|improve this question

1 Answer 1

After calling get_recent_posts() (or any other loop query), you typically have to run a foreach or while loop to cycle through the posts. So, you'll need to change this:

    <?php
    $args = array( 'tag' => 'featured', 'posts_per_page' => '3' );
    $recent_posts = wp_get_recent_posts( $args );
    ?>

<!-- Your HTML and Wordpress template tags here. -->

            <?php wp_reset_query(); ?>

to this:

    <?php
    $args = array( 'tag' => 'featured', 'posts_per_page' => '3' );
            $the_query = new WP_Query( $args );
            //start the WP_Query loop
            while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>

<!-- Your HTML and Wordpress template tags here. -->

            <?php endwhile; //end the WP_Query loop ?>
            <?php wp_reset_query(); ?>

Note that if you're ever wondering why a Wordpress function isn't working as expected, you can find that function's entry in the codex and (usually) see an example of use in practice. http://codex.wordpress.org/Function_Reference/wp_get_recent_posts

share|improve this answer
    
Thanks for your help! I tried doing what you said and now it shows more than one 'box' where I want the featured articles, but it isn't loading any of the info (Title, Thumbnail, Links, etc). Here's what I have entered and also the result: pastebin.com/URfN3HZn –  Matt Aug 15 '11 at 1:56
    
Check the Codex-evidently wp_get_recent_posts() uses a parameter of numberposts instead of posts_per_page. It also doesn't have a tag parameter. Same with get_posts(). So, you can give it a try with a wp_query object. –  supertrue Aug 15 '11 at 2:14
    
I may be mistaken in that last point about parameters/arguments. In any case, I've changed the answer to use new WP_Query instead of wp_get_recent_posts(). –  supertrue Aug 15 '11 at 2:22
    
Aha! That seems to have done the trick :P Do you known of any way I would be able to add a class to the third post it finds so that I can change the css styling of it? –  Matt Aug 15 '11 at 2:28
    
Declare a count variable before your loop: $count = 1; and increment it after each post by including $count++; right before endwhile. Then write an if statement to conditionally echo your CSS class, such as: <div class="<?php if ($count % 3 == 0) { echo "third "; } ?>"> For example: wordpress.stackexchange.com/questions/6679/… –  supertrue Aug 15 '11 at 3:36

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.