1

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

1 Answer 1

2

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

6
  • 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 Commented Aug 15, 2011 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. Commented Aug 15, 2011 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(). Commented Aug 15, 2011 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? Commented Aug 15, 2011 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/… Commented Aug 15, 2011 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.