I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>
Related
How could I limit this foraech statement to just 5 loops? I think I should just use Break but I'm not sure where to put it.
<?php if(!empty($locations)): foreach($locations as $location): ?>
<?php if(empty($location["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
<?php endforeach; ?>
You can use array_slice() first to get a new array with no more than 5 elements.
$locations = array_slice($locations, 0, 5);
Then everything unchanged.
There are three methods:
Method 1: foreach with a counter var
$counter = 1;
foreach($locations as $location) {
// use $location here
if($counter++ == 5) {
break;
}
}
Method 2: foreach using $key=>$val
foreach($locations as $key=>$val) {
// Your content goes here
if($key === 4) {
break;
}
}
Method 3: for loop
for($i = 0; $i < 5; $i++) {
// Use $locations[$i] here and do something with it
}
Using a counter variable into your loop you can control/limit to any number.
Example:
$counter = 0;
foreach($locations as $location):
if($counter++ == 5):
break;
// Your other content goes here
endif;
endforeach;
Add a variable ... increment it each iteration ... after reach 5 just break loop.
<?php $i = 1;?>
<?php if(!empty($locations)): foreach($locations as $location): ?>
<?php if(empty($location["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
<?php if ($i++ == 5) break; ?>
<?php endforeach; ?>
You can use for():
<?php if(!empty($locations)):
for($i=0; $i<5; $i++) {
$location = $locations[$i];
<?php if(empty($locations["title"])) continue; ?>
<li>
<a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
<?php esc_html_e($location["title"]) ?>
</a>
<?php if($param->count): ?>
<div class="wpjb-widget-item-count">
<div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
</div>
<?php endif; ?>
</li>
}
I have next code for WordPress loop of tags for single post:
<?php if ($tags) : foreach($tags as $tag): ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>,
<?php endforeach; endif; ?>
I have comma added to last anchor. There is also white space after comma.
How can I remove the comma from last anchor while I am doing this with foreach() PHP loop?
Thanks for ideas and help!
Check if your loop is working on the last one:
<?php if ($tags) : ?>
<?php $count = count($tags); ?>
<?php foreach($tags as $i => $tag): ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>
<?php if ($i < $count - 1) echo ", "; ?>
<?php endforeach; ?>
<?php endif; ?>
What has a higher cost, calling a function or setting a variable? Here's another way to do it perhaps, which sets a variable and removes the offending chars at the end - no extra math or if checks needed.
<?php
$tagoutput = '';
if ($tags) {
foreach ($tags as $tag)
$tagoutput .= '' . $tag->name . ', ';
$tagoutput = rtrim($tagoutput, ', ');
}
echo $tagoutput;
?>
You can do it the other way around (remove it from the first one). If your array is numeric you can try something like this:
<?php if ($tags): ?>
<?php foreach ($tags as $key => $tag): ?>
<?php if ($key > 0): ?>,<?php endif ?>
<a href="<?php echo get_tag_link($tag); ?>">
<?php echo $tag->name; ?>
</a>
<?php endforeach ?>
<?php endif ?>
You can also try it using counter.
$values = array('value','value','value');
$count = count($values);
$i = 0;
foreach($values as $value){
$i++;
echo $value;
if($count > $i){
echo ', ';
}
}
Output: value, value, value
I have two columns and I want one post type to get distributed to each column evenly. So it’s two side-by-side divs and I want:
Div1 = Post1, Post3, Post5
Div2 = Post2, Post4, Post6
So basically get odd/even posts. Not exactly sure how to do it.
<?php query_posts('post_type=post-type'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="column1">
<?php
//Get Odd Posts
?>
</div>
<div class="column2">
<?php
//Get Even Posts
?>
</div>
<?php endwhile; ?>
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>
You want to use the modulus operator something like this:
<?php
$i = 0;
for ($i = 0; $i <20; $i++){
$class = $i % 2 == 0 ? "even" : "odd";
echo "<div class='" . $class . "'>";
echo "</div>";
}
?>
To do what you want, first you have to store the results somewhere (as even/odd), then display them.
Though you should really target these posts with CSS, not PHP, as it's hackish at best.
<?php query_posts('post_type=post-type'); ?>
<?php if (have_posts()) : ?>
<?php
$i = 0;
while (have_posts())
{
$key = $i & 1 ? 'odd' : 'even';
$post[$key] = array(get_the_title() => get_the_content());
$i++;
}
?>
<div class="column1">
<?php foreach ($post['even'] as $title => $content) : ?>
<?php echo $title; ?>
<?php echo $content; ?>
<?php endforeach; ?>
</div>
<div class="column2">
<?php foreach ($post['odd'] as $title => $content) : ?>
<?php echo $title; ?>
<?php echo $content; ?>
<?php endforeach; ?>
</div>
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>
In my controller I have this Code to loop through database and return the data
$faultgroup = $this->booking_model->Get_Fault_Group_Display($grouptype);
$data['Get_Fault_Group_Display'] = $faultgroup; $getresults = array();
$data['get_fault_group_data'] = array();
foreach ($faultgroup as $key ) {
$show = $key->Showgroup;
$getresults = $this->booking_model->get_fault_group_data($grouptype,$show);
$data['get_fault_group_data'] = $getresults ;
}
In my View i have this Code to loop through each record with the specific grouptype and display record (to_do_item) from database that match that grouptype
<?php if ( ! is_null($Get_Fault_Group_Display)): ?>
<?php if (count($Get_Fault_Group_Display)): ?>
<?php foreach ($Get_Fault_Group_Display as $result): ?>
<?php echo $result->Showgroup; ?>
<?php foreach ($get_fault_group_data as $key) :?>
<?php echo $key->to_do_item; ?>
<?php endforeach ?>
<?php endforeach ?>
<?php else: ?>
<?php endif ?>
My problem is only the last row is shown on all the grouptypes because the loop keeps overiding $data['get_fault_group_data'] with the new $getresults
Shouldn't you use the $data['get_fault_group_data'] as an array?
Controler:
$data['get_fault_group_data'][$key] = $getresults ;
View:
<?php if ( ! is_null($Get_Fault_Group_Display)): ?>
<?php if (count($Get_Fault_Group_Display)): ?>
<?php foreach ($Get_Fault_Group_Display as $i => $result): ?>
<?php echo $result->Showgroup; ?>
<?php foreach ($get_fault_group_data[$i] as $key) :?>
<?php echo $key->to_do_item; ?>
<?php endforeach ?>
<?php endforeach ?>
<?php else: ?>
<?php endif ?>
HI all,
This code works perfectly printing all 5 results with borders on the bottom of each list item through CSS.
However the last item id like there to be no border. How could i add a class to the list item on the last iteration?
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php foreach ($last_articles as $article): ?>
<li>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php endforeach; ?>
</ul>
Thanks for your help.
<?php list($parent) = split('/', $this->url); ?>
<?php $last_articles = $this->find('/news')->children(array('limit'=>5, 'order'=>'page.created_on DESC')); ?>
<ul id="latest-news">
<?php $count = count($last_articles); $num = 0; ?>
<?php foreach ($last_articles as $article): ?>
<li <?php if($num == $count-1){ ?> class="last-item" <?php } ?>>
<?php echo '<h3>'.$article->link($article->title()).'</h3>'; ?>
<?php echo strip_tags(substr($article->content(),0,100)).'...'; ?>
</li>
<?php $num++ ?>
<?php endforeach; ?>
</ul>
BTW, this adds the class "last-item" to the last item processed.
And yes, you should restructure your code so its readable.
You can use the :last-child CSS modifier:
ul.latest-news:last-child { border-bottom: none; }
But it's not so widely supported (yet), so adding an explicit .last class as you're about to do is probably best.