PHP / Wordpress Loop - Creating a separator - php

I have the following function which grabs and displays the categories associated with the post, which works perfect however i want to place separators between them and not 100% sure how to achieve this. Can anyone lend me a hand?
CAT1 CAT2 CAT3
ideally would like to have like:
CAT1 / CAT2 / CAT3
Notice no seperator on the last one
PHP
<ul class="inline-list">
<?php
$id = get_the_ID();
$cats = get_the_category($id);
foreach ( $cats as $cat ):
?>
<li><?php echo $cat->name; ?></li>
<?php
endforeach;
?>
<li><?php FoundationPress_entry_meta(); ?></li>
</ul>

As explained by others, CSS would be the prefered way. But if you really want to do this in PHP, you could save the html code for the list to a variable, either by concatenation or output buffering:
ob_start();
foreach ( $cats as $cat ) {
echo '<li>'.$cat->name.' /</li>';
}
$list = ob_get_clean();
Then you could remove the last 7 characters (" /</li>") using the substr function and put "</li>" back before outputting the code:
echo substr($list, 0, -7).'</li>;

mikemike's answer is a perfect solution but if for some reason you want to do it using code you could try this.
<ul class="inline-list">
<?php
$id = get_the_ID();
$cats = get_the_category($id);
$count = 1;
foreach ( $cats as $cat ):
?>
<li><?php echo $cat->name; ?> </li>
<?php
if($count < count($cats)):
echo '/';
endif;
$count++;
endforeach;
?>
<li><?php FoundationPress_entry_meta(); ?></li>
</ul>

To do this in your code you can simply do the following:
<ul class="inline-list">
<?php
$id = get_the_ID();
$cats = get_the_category($id);
foreach ( $cats as $cat ):
?>
<li><?php echo $cat->name; ?></li>
<?php
endforeach;
?>
<li class="no-border"><?php FoundationPress_entry_meta(); ?></li>
</ul>
CSS:
ul.inline-list li{
border-right:1px solid #eee;
}
ul.inline-list li.no-border{
border-right:none;
}
Alternatively, you can achieve this using nth-child, first-child or last-child CSS selectors.
For example:
ul.inline-list li {
border-left:1px solid #eee;
}
ul.inline-list li:first-child{
border-left:none;
}
This code sets a light-grey border to the left of all the lis. The second statement then removes the border from the first li.
The same can be achieved with border-right and last-child (removing the border from the last li).
In both cases you can use CSS for the separator, whether that's a border, a background or by using before to add a / character.

You can count how many iterations you have done, and if less that the total, output the separator:
$count = count($cats);
$i = 0;
foreach ( $cats as $cat ):?>
<li>
<?php echo $cat->name; ?>
<?php echo ++$i < $count ? '/' : '';?>
</li>
<?php endforeach;
Live example: http://codepad.viper-7.com/7QU7x9

If you solely want to do this in php, I'd change from a foreach loop to a for loop, then you can decipher between the two.
IE:
$length = count($cat);
for ($i = 0; $i < $length; $i++) {
$cat = $cats[i];
if ($i + 1 == $length) {?>
<li><?php echo $cat->name; ?></li>
<?php } else { ?>
<li><?php echo $cat->name; ?> / </li>
<?php }
}

Related

How to add a span tag if the UL list is first child and last child? PHP Condition if else

I'm working on WordPress custom fields and I need to write PHP if-else conditions in a loop whether it is the first child or last child of the ul list. but I don't know how to apply, kindly please help me to solve it
Here is my code below
<?php if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
?>
<li><div><span class="w-num"><?php echo $sr_no; ?></span><?php echo $list_content; ?></div><span class="chart-divider"><span class="chart-divider-r"></span></span></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Count the entries for rows.
Set up a counter, and every time you iterate increase.
<?php if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php
$counter = 1;
$total-row = // Figure out how to count(rows), or something like that
while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
if($counter == 1){
// First Row
} elseif ($counter == $total-row){
// Last Row
}
?>
<li><div><span class="w-num"><?php echo $sr_no; ?></span><?php echo $list_content; ?></div><span class="chart-divider"><span class="chart-divider-r"></span></span></li>
<?php
$counter++;
endwhile; ?>
</ul>
<?php endif; ?>
Please try with below:
The result you will see in the li as a class
If fist li, you will see the first_class
If last li, you will see the last_class
Else only some_class
<?php
$Total_charts_count = wp_count_posts( 'chart' )->publish; //Total count of charts
$chart_number = 1; //Loop starting point.
if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php
while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
// First item
if($chart_number == 1) {
$class = 'some_class first_class';
// Last item
} else if ($chart_number == $Total_charts_count) {
$class = 'some_class last_class';
// Not first / Not last item
} else {
$class = 'some_class';
}
?>
<li class="<?php echo $class; ?>">
<div>
<span class="w-num"><?php echo $sr_no; ?></span>
<?php echo $list_content; ?>
</div>
<span class="chart-divider">
<span class="chart-divider-r"></span>
</span>
</li>
<?php
$chart_number++;
endwhile; ?>
</ul>
<?php endif;
?>
Hope it's work for you.

How to show 3 <li> in one row

I need to show 3 images in each row in <ul> tag. I have multiple <ul> tags and in each <ul> tag, I am showing 6 images. Now i have to show 3 <li> in first row and next 3 <li> in second row. Then same for next <ul>. I need to break row after 3 <li>. This is my code :
<div class="sets">
<?php foreach ($sets as $set => $items) : ?>
<ul class="set test-set">
<?php $i=0; foreach ($items as $thumb) : ?>
<?php
/* Prepare Image */
$content = '<img src="'.$thumb['cache_url'].'" width="'.$thumb['width'].'" height="'.$thumb['height'].'" alt="'.$thumb['filename'].'" />';
?>
<?php if($i === 0):
echo '<li><div>'; ?>
<?php endif; ?>
<?php echo $content; ?>
<?php if($i === 2): $i = 0; ?>
<?php else: $i++; endif; ?>
</div></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</div>
http://jsfiddle.net/z4Q48/
li{
float: left;
}
li:nth-child(3n+4){
clear: both;
}
see http://css-tricks.com/how-nth-child-works/

Making a nested list with php foreach

I'm trying to make a nested list with php by using a foreach loop but I'm kind of stuck now. My code uses a foreach loop and checks if the item is a heading, if it is it will start a nested list below it. The problem now is that if it's not a heading I want to put the corresponding list items into a single ul element below its heading. Now as you can see it puts every single list items that isn't a heading into a seperate ul element of its own because of the foreach loop. How can I fix this?
<ul>
<?php foreach($listitems as $listitem) : ?>
<?php if( $listitem['heading'] == 1) : ?>
<li><?php echo $listitem['listitem']; ?><!--begin nested list-->
<?php endif; ?>
<?php if( $listitem['heading'] == 0) : ?>
<ul><li><?php echo $listitem['listitem']; ?></li></ul>
<?php endif; ?>
<?php endforeach; ?>
</li><!--end nested list-->
</ul>
This is the desired html output:
<ul>
<li>Javascript Basics<!--begin nested list-->
<ul>
<li>Getting Started</li>
<li>Data and Variables</li>
<li>Functions</li>
<li>Scope</li>
<li>Working With Objects</li>
<li>Creating Objects</li>
<li>Arrays</li>
<li>Conditions And Decisions</li>
<li>Loops</li>
</ul>
</li><!--end nested list-->
</ul>
You need to set the inner lists start and end tags when the heading changes.
Something like this
<ul>
<?php
$NonHeadCount = 0;
$HeadingOpen = false;
foreach($listitems as $listitem)
{
if( $listitem['heading'] == 1)
{
$HeadingOpen = true;
if ($NonHeadCount != 0)
{
echo "</ul>";
}
echo "<li>".$listitem['listitem'];
$NonHeadCount = 0;
}
if( $listitem['heading'] == 0)
{
if ($NonHeadCount == 0)
{
echo "<ul>";
}
echo "<li>".$listitem['listitem']."</li>";
}
}
if ($NonHeadCount != 0)
{
echo "</ul>";
}
if ($HeadingOpen)
{
echo "</li>";
}
?>
</ul>
You have to change the $listitems format.
Some like this:
<?php
$listitems = array(
'item1' => array(
'item11',
'item12',
'item13'
),
'item2' => array(
'item21',
'item22',
),
'item3',
'item4'
);
?>
Then, do this:
<?php
echo "<ul>";
foreach($listitems as $item => $listitem):
echo "<li>$item</li>";
if(count($listitem) > 0):
echo "<ul>";
foreach($lisitem as $item):
echo "<li>$item</li>";
endforeach;
echo "</ul>";
endif;
endforeach;
echo "<ul>";
?>

Order data in three columns instead of one

At the moment, with the code from below, I have the data shown like this:
http://img27.imageshack.us/img27/8083/29769986.jpg
but I want it to be shown like this:
http://img259.imageshack.us/img259/3233/24033830.jpg
The code for the data shown, as it is on the first image, is:
<div id="content">
<?php foreach ($categories as $category) { ?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php } ?>
</div>
I order to get the "Hewlett-Packard" text under the "HTC" text, I've changed the "/ 4" into "/ 1", but I have no idea how to make the data to be shown into three columns (like on the second picture), instead of one, as it is now (as shown on the first picture).
Thanks in advance.
EDIT: What I actually need, is to count and to do the calculation on this code:
<?php foreach ($categories as $category) { ?>
.
.
.
<?php } ?>
So it needs to count the number of categoris, do the calculations, and present the code between into three columns.
Try this one.
<div id="content">
<div class="content-column">
<?php
$cols = 3; // Change to columns needed.
$catcount = count($categories);
$catpercol = ceil($catcount / $cols);
$c = 0;
foreach ($categories as $category) {
if ( $c == $catpercol ) {
$c = 0;
print "</div><div class='content-column'>";
}
?>
<div class="manufacturer-list">
<div class="manufacturer-heading"><?php echo $category['name']; ?><a id="<?php echo $category['name']; ?>"></a></div>
<div class="manufacturer-content">
<?php if ($category['manufacturer']) { ?>
<?php for ($i = 0; $i < count($category['manufacturer']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['manufacturer']) / 4); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['manufacturer'][$i])) { ?>
<li><?php echo $category['manufacturer'][$i]['name']; ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
<?php } ?>
</div>
</div>
<?php $c++; } ?>
</div>
</div>
Add .content-column { float: left; width: 33.33333%; } to your CSS.
Details:
$cols = 3; enables you to set the desired number of columns (note: you might need to change CSS accordingly).
$catcount = count($categories); gives you the total number of categories about to be rendered.
$catpercol = ceil($catcount / $cols); divides that total number evenly into the required number of columns with the last column having eventually less items than the others.
$c = 0; is your counter. It increases at the end of the outer foreach loop.
Within the loop, $cis checked if it matches the $catpercol number and if so, the current parent div is closed and a new one created. You end up with as many parent divs as you need columns. Just add appropiate CSS to make them appear besides each other.
understand the following code that according to your requirement, the code just give the hint to achieve that you want according to your given screen shoot http://img259.imageshack.us/img259/3233/24033830.jpg
echo "<table>";
echo "<tr>";
$i = 1;
do{
// column range
$range = 3;
echo "<td>" . $i;
if( $i % $range == 0 ){
echo "</tr>";
echo "<tr>";
}
echo "</td>";
$i++;
}while( $i <= 10 );
echo "</tr>";
echo "</table>";
Hope this will help you
I think it is possible to create three column layout via html/css and without any change of your PHP code. Just use float:left; width: 33%. You can also use absolute value for width property because of margins and borders.

how to add big space

<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
the above code is used to output an article list. now i want to add big space after the lines which is the multiple of 10.namely,add a big space (eg:margin-bottom:30px,only to after every 10 bullet points but the space between other li is 15px )after every 10 bullet points. how to change the above code. then i can use css to get that.
–
Although I don't have a clue what you mean by 'big space', I assume you want to place an arbitry HTML tag after every 10th list item. You can do this like so, for example:
<?php $i = 0; ?>
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php if ($i++ === 10): ?><br /><?php $i = 0; endif; ?>
<?php endforeach; ?>
EDIT, you clarified:
no,eg:30px, but the space between other <li> is 20px.
Which can be done like so:
<?php $i = 1; ?>
<?php foreach ($rows as $id => $row): ?>
<?php $i++; ?>
<li style="margin-bottom:<?php echo ($i === 10) ? : '30' : '20'); ?>px;" class="<?php print $classes[$id]; ?>"><?php print $row; ?></li>
<?php if ($i === 10) $i = 1; ?>
<?php endforeach; ?>
The modulus operator (%) is ideal for this:
<?php $i = 0; foreach ( $rows as $id => $row ): ?>
<li style="margin-bottom: <?php echo $i ++ % 10 ? '30' : '20' ?>px;" class="<?php echo $classes[$id] ?>">
<?php echo $row ?>
</li>
<?php endforeach ?>
As a slight amendment to Aron's code, if you wanted to use CSS to style the <li>:
<?php $i = 1;
foreach ($rows as $id => $row):
if($i === 10){
$end_class = ' end-class';
$i = 1;
} else {
$end_class = '';
$i++;
}
echo '<li class="' . $classes[$id] . $end_class . '">' . $row . '</li>';
endforeach;
?>
You can then apply the style to .end-class.

Categories