By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,387 Members | 1,729 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,387 IT Pros & Developers. It's quick & easy.

How do i control a "foreach" loop when it is inside a "while" loop? (uses smarty)

P: 31
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But on the homepage of a site, i wanted to truncate the content to like the first 75 characters and then put "..." (a perfect use of the smarty "truncate" modifier) and then give the visitor a link to read the whole article. But since "the_content" is a function, i can't really control the output without going in and tweeking the function. So i said, Ill just query the post db on my own, load the first 5 results in an array variable, and loop through them in the presentation, no sweat... here's what i got:

[PHP]
<ul>
//Wordpress (WP) checking for posts, then starts a "while" loop
{php}if ( have_posts() ) : while ( have_posts() ) : the_post(); {/php}

<!-- If the post is in the category we want to exclude, we simply pass to the next post. -->

//Tell WP to do the first 5 posts
{php} if (in_category('5')) continue; {/php}

<div class="post">

//Display the link for the full blog post, using the title of the post as the name
<li><h2><a href="{php} the_permalink(); {/php}">{php} the_title();{/php}</a></h2></li>

//Display the date posted
<small>{php} the_time('F jS, Y'); {/php}</small><br />

//now HERE is where i dropped in my foreach loop to pull out the first 5
//contents and truncate them and add the "..."
{foreach from=$users item="query"}
{$query->post_content|truncate:75:"...":false}
{/foreach}

//Made another "Read All" link at the add of content summary
<a href="{php} the_permalink(); {/php}">Read All</a>
<hr />


</div> <!-- closes the first div box -->
{php} endwhile; else: {/php}
<p>Sorry, no posts matched your criteria.</p>
{php} endif; {/php}
[/PHP]


I thought this would be gravy, and at first glance it looked like it worked. Until i took a closer look and saw what happened. WP code works fine, it's provided in their documentation. But my foreach loop cycles through the first five contents every time WP's while loop displays a post. I wanted it to do the first content in the first post, second content in the second post, and so on. To illustrate.

(example)
WP function got this (A,B,C,D,E)
My query got these results array(1,2,3,4,5)

Now i want this to be displayed...

A1B2C3D4E5

But this is what I get...
A12345B12345C12345D12345E12345

I understand why this is happening: everytime the WP while loop gets to my query, my smarty code is saying to loop through all my results. So how do i get my foreach loop to output the first index and wait til the next time it's called. My only other way I can think of is making a counter ($i) and actually typing in something like: $array[$i], and at the end of the while loop adding: $i++; but that makes it seem like im missing a bigger picture.

Let me know
Thanks
Nov 13 '07 #1
Share this Question
Share on Google+
2 Replies


Atli
Expert 5K+
P: 5,058
Hi.

You posted this in the PHP Articles section. I have moved it over to the PHP Forums for you.

Please try to avoid posting in the Article sections in the future.

Moderator
Nov 13 '07 #2

P: 31
Hi.

You posted this in the PHP Articles section. I have moved it over to the PHP Forums for you.

Please try to avoid posting in the Article sections in the future.

Moderator
sorry about the misktake, any advice?
Nov 14 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.