Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

What's wrong with my for loop?

I'm currently developing a WordPress site and would like to output the four most recent posts on a page that is not the default blog page (blog posts will appear on a "news" page, which is configured in wp-admin, and on the homepage I would like to output only the four most recent posts with the particular markup given below).

What I've done is stored the output of wp_get_recent_posts('4') to a variable, and I'm trying to use a for loop to run through that array and output the appropriate markup for each returned post. Here's my code:

<?php 
$recent_posts = wp_get_recent_posts('4');
    foreach ($recent_posts as $this_post) {
        $this_post_url = get_permalink($this_post);
        $this_post_title = get_the_title($this_post); 
        echo "<div class='span3'>";
        echo "<a href='" . $this_post_url . "'>" . $this_post_title . "</a>";
        echo "<p>This is some text</p>"; // placeholder for text taken from the post
        echo "</div>";
        }
?>

The result is four divs as planned, but the href attribute and the associated text of each link is the same in each instance (a link to the most recent post, with the link text being the title of that post) rather than what I expected to see - four divs with unique links and titles.

Any WordPress/PHP gurus able to offer any insight?

8 Answers

Hi Mike,

You're close! But, If you look in the Wordpress Codex you'll notice from the examples that the way you're parsing the ID of the post to the two functions get_permalink and get_the_title is a bit off.

What happens if you try this?

<?php
$recent_posts = wp_get_recent_posts('4');
foreach ($recent_posts as $this_post) {

    // Get the ID of the post once
    $this_post_id = $this_post['ID'];

    $this_post_url = get_permalink($this_post_id);
    $this_post_title = get_the_title($this_post_id);
    echo "<div class='span3'>";
    echo "<a href='" . $this_post_url . "'>" . $this_post_title . "</a>";
    echo "<p>This is some text</p>"; // placeholder for text taken from the post
    echo "</div>";
}
?>

Here is the code you can use:

<?php 
    $recent_posts = wp_get_recent_posts('4');
    foreach ($recent_posts as $this_post) {
      $this_post_url = get_permalink($this_post["ID"]);

      echo "<div class='span3'>";
      echo "<a href='" . $this_post_url . "'>" . $this_post["post_title"] . "</a>";
      echo "<p>This is some text</p>"; // placeholder for text taken from the post
      echo "</div>";
    }
?>

You are close in what you are trying to do. Because an array is returned you use the foreach loop (as you are doing), and then call each item with the key of the element you are trying to access (ie. post_title, id, etc.)

You're absolutely right, Marc. Thank you very much for your help! I'm now getting exactly the result I was looking for. It seems I needed to store the ID first as you demonstrated to get the rest of the code to function as I was expecting.

No problem, glad I could help :)

Damian snuck another version of the solution in while I was posting a response to Marc - thanks to you as well!

And thank you both for not just giving me the answer but also explaining why it solved my problem. It's my first time working with for loops and writing my own PHP within WordPress templates so it's brilliant to have a fuller understanding of the solution.

Anytime. Let me know if you have other questions too as I'd be more than happy to help.

If you want I have a free theme available on github that you are more than welcome to pick apart. Might help with other tasks you are trying to accomplish as well :-)

https://github.com/modlearning/yeti

Bookmarked :D

Great advice guys!

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Damian and Marc thanks for helping out here, you rock!