PHP While Loop wrapping every 4 results with a li - php

i am trying to wrap each 4 results inside a LI and repeat for every 4 items like
<li>
<div>item 1</div>
<div>item 2</div>
<div>item 3</div>
<div>item 4</div>
</li>
PHP so far.... the loop ive attempted is of course not working :)
if ( $query->have_posts() ) {
$opnews .= '<ul class="newsitems orbit-slides-container">';
$count = 0;
while ( $query->have_posts() ) : $query->the_post();
$post_id = get_the_ID();
$opnews_item_data = get_post_meta( $post_id, 'opnews_item', true );
if ($i%4 == 1) {
$opnews .= '<li>';
}
$opnews .= '<div class="columns large-3 small-12 medium-3">';
$opnews .= '<div class="panel green opacity-change">';
$opnews .= '<h1>' . get_the_title() . '</h1>';
$opnews .= get_the_content_with_formatting();
$opnews .= '</div>';
$opnews .= '</div>';
if ($count%4 == 0) {
$opnews .= '</li>';
}
endwhile;
$opnews .= '</ul>';
wp_reset_postdata();
}

You are using $i and $count, so pick only one.
Then you have to increment it between your <li> to get it working.
And finally, you should check, once you finished the loop, that the last <li> has been echoed or you will get some trouble (a missing </li>)
$array = range(1, 9);
$i = 0;
foreach ($array as $val) {
if ($i%4 == 0) echo '<li>';
$i++;
echo $val;
if ($i%4 == 0) echo '</li>';
}
if ($i%4 != 0) echo '</li>';
Output :
<li>
1 2 3 4
</li>
<li>
5 6 7 8
</li>
<li>
9
</li>

The modulus operator (%) divides the number and returns the remainder. So, your line if ($i%4 == 1) probably isn't what you're after, as if it's every 4th row, you'll want it with no remainder.
The $count%4 == 0 line also doesn't make much sense to me, as you're not incrementing the number. You're also not incrementing $i.
Try the following:
if ( $query->have_posts() ) {
$opnews .= '<ul class="newsitems orbit-slides-container">';
$i = 0;
while ( $query->have_posts() ) : $query->the_post();
$post_id = get_the_ID();
$opnews_item_data = get_post_meta( $post_id, 'opnews_item', true );
if ($i%4 == 0) {
if ($i != 0){
$opnews .= '</li>';
}
$opnews .= '<li>';
}
$opnews .= '<div class="columns large-3 small-12 medium-3">';
$opnews .= '<div class="panel green opacity-change">';
$opnews .= '<h1>' . get_the_title() . '</h1>';
$opnews .= get_the_content_with_formatting();
$opnews .= '</div>';
$opnews .= '</div>';
$i++;
endwhile;
$opnews .= '</li>';
$opnews .= '</ul>';
wp_reset_postdata();
}

It's not working because you never change count. Count is always 0, so $count % 4 == 0 is always true. Also, unless it's somewhere else you haven't defined i.
Use only count (or only i).
if ($count % 4 == 0) {
$opnews .= '<li>';
}
DO STUFF HERE
$count += 1
if ($count % 4 == 0) {
$opnews .= '</li>';
}

It looks like you're mixing both $i and $count. One of them you're using the modulous operator and comparing if the remainder after division is 1, and the other you're comparing if the remainder is 0. Neither of them seems to be incrementing (and $i doesn't look to be defined from the snippet you've provided).
Choose one, $count, and compare it with 0 using the modulous and be sure to increment it within the loop:
if ($count % 4 == 0) {
$opnews .= '<li>';
}
// ...
$count++;
if ($count % 4 == 0) {
$opnews .= '</li>';
}

Related

Additional <a> tag in final print in foreach php

I want print wordpress nav menu in custom html .
This function print my favorite out put , but the problem is printing the additional tags in output
I don't want use wp_nav_menu function
my favorite output is like :
<div class="menu">
<ul>
<li>
<a href="" ></a>
<div class="menu">
<ul>
.....
</ul>
</div>
</li>
</ul>
</div>
and my function is :
function sh_mobileMenuRender($menu_id)
{
$menu_items = wp_get_nav_menu_items($menu_id);
$html = '<div class="position-fixed mobileMenu-continer"
id="mobileMenu-continer" data-open="' . $opening_from . '"><div id="mobileMenu" class="menu"><ul>';
$is_frist_menu = true;
//این آرایه مقدار تورفتگی ها را محاسبه میکند
$y = array();
for ($x = 0; $x < $c; $x++) {
if ($menu_items[$x]->menu_item_parent == 0) {
//تو رفتگی رو صفر کن
$y[$x] = 0;
if ($is_frist_menu) {
$html .= '<li>بستن<i aria-hidden="true" class="fas fa-arrow-right"></i></li>';
$html .= '<li><a href="#">' . $menu_items[$x]->title;
$is_frist_menu = false;
} else {
$html .= '</a>';
if ($y[$x] < $y[$x - 1]) {
$dif = $y[$x - 1] - $y[$x];
for ($v = 1; $v <= $dif; $v++) {
$html .= '</li></ul></div>';
}
}
$html .= '</li><li><a href="#">' . $menu_items[$x]->title;
}
continue;
}
//find parent menu form privous menu
for ($b = $x - 1; $b >= 0; $b--) {
if ($menu_items[$x]->menu_item_parent == $menu_items[$b]->ID) {
//محاسبه تورفتگی
$y[$x] = $y[$b] + 1;
if ($b == $x - 1) {
$html .= '<i aria-hidden="true" class="fas fa-arrow-left"></i></a>';
$html .= '<div class="menu"><ul class="submenu">';
$html .= '<li>بازگشت<i aria-hidden="true" class="fas fa-arrow-right"></i></li>';
$html .= '<li><a href="#">' . $menu_items[$x]->title;
} else {
$html .= '</a>';
if ($y[$x] < $y[$x - 1]) {
$dif = $y[$x - 1] - $y[$x];
for ($v = 1; $v <= $dif; $v++) {
$html .= '</li></ul></div>';
}
}
$html .= '</li><li><a href="#">' . $menu_items[$x]->title;
}
break;
}
}
}
$html .= '</ul></div></div>';
echo $html;
}
I want print wordpress nav menu in custom html .
This function print my favorate out put , but the problem is printing the additional tags in output
I don't want use wp_nav_menu function

Wrapping every 3 elements in a loop leaves an empty wrapper

I am wrapping every 3 elements in my loop in a wrapper div like this:
$query = array(
'post_type' => 'post',
);
$i = 1;
$posts = new WP_Query( $query );
$out = '<div class="wrapper">';
if ($posts->have_posts()){
while ($posts->have_posts()){
$posts->the_post();
$out.= '<div class="content">
//content here
</div>';
if($i % 3 == 0) {
$out .= '</div><div class="wrapper">';
}
$i++;
}
}
$out .= '</div>';
wp_reset_postdata();
return '<section>'.$out.'</section>';
Which creates a good wrapping html minus one little thing that's bothering me:
<section>
<div class="wrapper">
<div class="content"></div>
</div>
<div class="wrapper">
<div class="content"></div>
</div>
<div class="wrapper"></div>
</section>
If I have exactly 6 posts (or any multiple of 3, and modulo is doing this like it should) I'll get an extra empty wrapper. Which is really not needed.
So what conditional should I include in my query to ensure that I don't get empty wrappers?
Add the wrapper inside:
$query = array(
'post_type' => 'post',
);
$i = 1;
$posts = new WP_Query( $query );
$out = '';
$endingNeeded = false;
if ($posts->have_posts()){
while ($posts->have_posts()){
if($i % 3 == 1) {
$out .= '<div class="wrapper">';
$endingNeeded = true;
}
$posts->the_post();
$out.= '<div class="content">
//content here
</div>';
if($i % 3 == 0) {
$out .= '</div>';
$endingNeeded = false;
}
$i++;
}
}
if($endingNeeded) {
$out .= '</div>';
}
wp_reset_postdata();
return '<section>'.$out.'</section>';

How do I get multiple images from a single string

I've looked around and haven't found a answer that did it for me. I don't want all the images from an HTML page. I just want all the images from a single string.
On a page, I'm using two different strings. I want all the images that are within the second image. I want to loop them into a carousel.
I've looked around a bit and this is what I got:
function GetImgString($plaatje){
preg_match_all('/<img[^>]+>/i',$plaatje, $result);
$house = $result[0];
$i = 1;
$output = '<div class="carousel-inner">';
foreach ( $house as $houses ) {
if ($i == 1) {
$output.= '<div class="item active">';
}else{
$output.= '<div class="item">';
}
$output.=
$house.'
<div class="container">
<div class="carousel-caption">
</div>
</div>
</div>
';
$i++;
}
$output .= '</div>';
return $house;}
This is the output:
Array
(
[0] => &ltimg src="images/Huizen/huis-3.jpg" alt="" />
[1] => &ltimg src="images/Huizen/huis-4.jpg" alt="" />
)
How do I solve this?
I assume, when you do the regexp, in your results, there are the 2 images.
You are start to building your output into a variable, calles $output, but you return with $house, what contain the 2 result from preg_match.
So you can try with this:
function GetImgString($plaatje) {
preg_match_all('/<img[^>]+>/i', $plaatje, $result);
$houses = $result[0];
$houses = str_ireplace('<', '&lt', $house);
$houses = str_ireplace('>', '&gt', $house);
$i = 1;
$output = '<div class="carousel-inner">';
foreach ($houses as $house) {
$class = ' class="item"';
if ($i === 1) {
$class = ' class="item active"';
}
$output.= '<div '.$class.'>';
$output.= $house . '
<div class="container">
<div class="carousel-caption"></div>
</div>
</div>' . "\n";
$i++;
}
$output .= '</div>';
return $output;
}

How can i detect category page on Joomla?

I want to detect virtuemart category page in mod_breadcrumbs.
Breadcrumbs is loading all page and i just wanted write a message in category page.
My breadcrumbs code:
<div class="breadcrumbs<?php echo $moduleclass_sfx; ?>">
<?php
echo '<ul>';
for ($i = 0; $i < $count; $i ++) {
// If not the last item in the breadcrumbs add the separator
if ($i < $count -1) {
if (!empty($list[$i]->link)) echo '<li>'.$list[$i]->name.'</li>';
else echo '<li class="pathway">' . $list[$i]->name . '</li>';
if($i < $count -2) echo ' <li class="pathway separator">></li> ';
} else if ($params->get('showLast', 1)) { // when $i == $count -1 and 'showLast' is true
if($i > 0) echo ' <li class="pathway separator">></li> ';
echo '<li class="pathway">' . $list[$i]->name . '</li>';
}
}
echo '</ul>';
?>
</div>
Here's how you can check if you're on a category view:
$appInput = Jfactory::getApplication()->input;
if($appInput->getCmd('option')=='com_content' && $appInput->getCmd('view')=='category' ){
//add your code here
}

Can I add an ID to the divs in this Wordpress query?

I'm using this PHP snippet to wrap every 12 Wordpress posts in a div.
<?php
$i = 1;
echo '<div class="row content-block">';
if ( $project->have_posts() ) :
while ( $project->have_posts() ) : $project->the_post();
get_template_part('project', 'content');
if($i % 12 == 0) {echo '</div><div class="row content-block">';}
$i++; endwhile; endif;
echo '</div>';
?>
Is there any way that I can apply unique ID's to the row content-block divs? for example block-1, block-2, etc/
Yes (edited):
<?php
$i = 1;
$id = 2;
echo '<div class="row content-block" id="block-1">';
if ( $project->have_posts() ) :
while ( $project->have_posts() ) : $project->the_post();
get_template_part('project', 'content');
if($i % 12 == 0) {echo '</div><div class="row content-block" id="block-' .$id .'">'; $id++;}
$i++; endwhile; endif;
echo '</div>';
?>

Categories