4327 posts ThemeForest Reviewer
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Member of the Envato Team
+12 more
Ivor Envato team says

Hey guys I’m struggling with this issue for over 5 hours now,

I’ll try to explain:

I have 2 foreach loops, one for the navigation and the other for the posts items. I need the same class for both (navigation and each post).

However in the second foreach loop it returns only the last tag name. I read in the PHP documentation: “the last array element remain after the foreach loop” and that’s exactly what’s happening.

What’s the best way to achieve this? Do you need a better explanation? Any ideas?

$tags = array(tag1, tag2, tag3, tag4);
<?php foreach($tags as $tag):  ?>
<li class="&lt;?php echo $tag->name?&gt;">
    <a href="#">&lt;?php echo $tag?&gt;name ?&gt;</a>
</li>
&lt;?php endforeach; ?&gt;
&lt;?php foreach($work ?&gt;posts as $key => $work_item): ?&gt;    

<div class="post &lt;?php echo $tag->name ?&gt;">
  // My content here
</div>

&lt;?php endforeach; ?&gt;
  • I know I’m running <?php echo $tag?>name ?> outside of the tag loop, that’s only to explain what I’m trying to do.

Thanks in advance.

EDIT : better illustration

\\

3431 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark Volunteer moderator says

I’m not 100% sure what you’re asking. But if you are saying that you want to apply one tag name as a class to each post, you could just create a counter and reference the $tags array in the second loop.

&lt;?php $i = 0; 
foreach($work?&gt;posts as $key => $work_item): ?&gt;

<div class="post &lt;?php echo $tags[$i]->name; ?&gt;">
  // My content here
</div>

&lt;?php $i++; endforeach; ?&gt;

Also, you seem to have a type in your second foreach:

foreach($work->posts as $key => $work_item): // use a dash (-) rather than a ?

Otherwise I’m not really sure what you’re trying to accomplish. If this isn’t it, how do you want the tags to map to the posts?

4327 posts ThemeForest Reviewer
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Member of the Envato Team
+12 more
Ivor Envato team says

Thanks man,

I’m using get_terms to store the data in the $tags variable $tags= get_terms('portfolio_tags'); so for example print_r($tags) will return:

Array ( 
[0] => stdClass Object 
    ( [term_id] => 18 
      [name] => Html 
      [slug] => html 
      [term_group] => 0 
      [term_taxonomy_id] => 18 
      [taxonomy] => portfolio_tags 
      [description] => [parent] => 0 
      [count] => 1 )

[1] => stdClass Object 
    ( [term_id] => 20 
      [name] => Photoshop 
      [slug] => photoshop 
      [term_group] => 0 
      [term_taxonomy_id] => 20 
      [taxonomy] => portfolio_tags 
      [description] => [parent] => 0 
      [count] => 1 
))

I can’t simply use a counter because I’ll set the tags via WordPress backend, each tag will be the name of the navigation and the classes for posts and navigation items.

3431 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark Volunteer moderator says

Right, I get that. Can you provide a sample HTML output of what you’re trying to accomplish (for the posts)? Specifically, what would the class attributes be for each post once the loops have completed (assuming everything was working properly)?

385 posts
  • Has referred 10+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+8 more
uDesignStudios says

Hi,

i’d do:

<div class="post &lt;?php echo implode(' ', $tags) ?&gt;"> </div>

for the second loop.

Or if you want to have only one tag per pass in the second loop, then you need to have only one loop. Like this:

<?php for($i = 0; $i<count($tags); $i++):
$li1 = ."<li class="$tags[$i]?>name?>">
    <a href="#"><?php echo $tags[$i]?>name ?></a>
";
$li2 .= "<div class="post <?php echo $tags[$i]->name ?>">
  // My content here
</div>";
 endfor;?>

<ul><?php echo $li1 ?></ul>
<ul><?php echo $li2 ?></ul>

BTW this code is just a quick & dirty example, there are things that you should do differently, you can mail me if you run into trouble.

UDS

by
by
by
by
by
by