removed foreach item doesn't keep count correctly - php

<ul>
<?php foreach (array_slice($items, 0, 3) as $item): ?>
<?php if (!isset($item->story)): ?> <!-- hide story updates -->
<li>
<?php if (isset($item->message)) echo $item->message; ?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
I am displaying a news feed, and only want to display the items without a story update. So i am using !isset to check if a story item is present, and hide the item if it is.
This works perfectly, but now I am trying to display 3 results in the feed, but i think the story items are still being counted in the foreach, so when the last 3 updates are story items, nothing displays in the feed.
I have read this question which looks like what I am trying to do, but I dont understand where he has got the $elementkey variable from, and what it does?
How do I remove the item from the foreach, and remove its count so 3 items will display?

This should work for you:
(Here i just added a $count variable which i only increment if a message get's posted. And after 3 i break the foreach)
<ul>
<?php $count = 0;
foreach ($items as $item): ?>
<?php if ($count >= 3) break; if (!isset($item->story)): ?> <!-- hide story updates -->
<li>
<?php if (isset($item->message)) {
echo $item->message;
$count++;
}
?>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>

Related

How can I skip multiple strings from php array?

I have the categories: 2D, 3D, Photo, Animation, which become buttons.
How can I skip the 3D and Animation from being displayed? on this particular page when running foreach. For example, the buttons that will appear have to be: All, 2D, Photo.
I tried to find a solution, but no luck.
What I managed to remove was only a string, but not multiple ones, with:
$key = array_search('3D',$terms);
unset($terms[$key]);
This is my code that I want to apply the changes to:
$terms = get_terms("portfolio_category",$cat_arguments);
if($terms):
?> <!-- category menu, this -->
<div class="row categories-p">
<ul class="col-sm-12">
<li><?php _e("All","um_lang"); ?></li>
<?php
sort($terms);
foreach($terms as $term):?>
<li>
<?php echo $term->name; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
You can use php's in_array, here's a basic example:
Assuming you have the following:
$categories = ['2D', '3D', 'Photo', 'Animation'];
And your skip list looks like this:
$skipList = ['3D', 'Animation'];
Then your render function will look like this:
iterate over category list
if the item is not in the skip list, render
foreach ($categories as $category) {
if (!in_array($category, $skipList)) {
// Display the category button
// e.g. echo "<button>{$category}</button>";
}
}

Combining php loop and count - Wordpress ACF

I wondered if anyone could help me combine two blocks of code I have. I have one block looping though items and the start of another block showing a count and hopefully enabling me to display the items looping through in rows by adding a div around them every two items... Heres the first bit of code, the loop:
<?php if(get_field('areas')): ?>
<?php while(has_sub_field('areas')): ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php endwhile; ?>
<?php endif; ?>
I'm using Advance Custom Fields for Wordpress and this is pulling through repeater fields... this displays them just one after the other.
Here's the code I have found to hopefully display them in rows.
<?php
$num = 1;
foreach ( $terms as $term ) {
if($num%2) {
echo '<div class="area-row">';
}
// Other Code
if($num %2) {
echo '</div>';
}
$num++
}
?>
I would like to display them in rows of two...
ONE TWO
THREE FOUR
FIVE SIX
Etc...
So, Im guessing I need to combine the code somehow... I currently have this: but it doesn't seem to work:
<?php
$num = 1;
foreach ( $terms as $term ) {
if($num%2) {
echo '<div class="area-row">';
}
if(get_field('areas')): ?>
<?php while(has_sub_field('areas')): ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php endwhile; ?>
<?php endif; ?>
if($num %2) {
echo '</div>';
}
$num++
}
?>
Okay so it looks like there was a couple of simple issues with this, you had closed the php tag after your if statment and then continued to write php without reopening the php tags. Also there is a slight logic error with the if($num%2) statements as one of these needs to be if, the other needs to be if not, so that the alternate.
Give this code a try and let me know how you get on:
<?php
if(get_field('areas')):
$num = 1;
?>
<?php while(has_sub_field('areas')):
if($num%2) {
echo '<div class="area-row">';
} ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php
if(!$num%2) {
echo '</div>';
}
$num++
endwhile; ?>
<?php endif; ?>
The main reason behind this question was for me to be able to target the third item of the looped through items, as it had padding on it that was breaking the rows that I needed to remove.
I have since found another solution that seems to be working.
By using the nth-child element, I have been able to target and remove the padding on every third item that's looped through - fixing the issue.
.single-area-item:nth-child(3n+3) {
margin-left: 0;
}
This is for rows or 2 items, if it were rows of 3 or 4 then it would need to target every 4th or 5th item respectively.

PHP while loop ODD and Even wrapped in either the left or right column

I cant seem to get this right, basically I have a while loop and i want all odd posts to get wrapped in a div called .split-left, so 1,3,5 etc will go into that column. Then all EVEN posts must go into .split-right div so 2, 4, 6 etc.
Currently my loop is putting post 1 into the .split-left div but then its putting 2, 4, 6 into the split-left div as well, something is not right.
<!-- SPLIT EFFECT PAGE BUILDER -->
<div class="page-builder split">
<?php if( have_rows('split_effect_page_builder') ): ?>
<div class="split-left">
<?php $i = 1; ?>
<?php while ( have_rows('split_effect_page_builder') ) : the_row(); ?>
<?php get_template_part('template-parts/page', 'builder'); ?>
<?php
if($i % 2 == 0){
echo '</div><div class="split-right">';
$i = 0;
}
$i++;
?>
<?php endwhile; ?>
</div>
<?php else : ?>
<?php // no layouts found ?>
<?php endif; ?>
</div>
<!-- END SPLIT EFFECT PAGE BUILDER -->
This part below is just a template with the loop, so just think of this as a normal PHP while loop.
<?php get_template_part('template-parts/page', 'builder'); ?>
Nobody seems to be able to help me with this, i have googled and checked everywhere for some code that splits even and odd posts into two columns, but have found nothing to even reference to help me out to fix this.
Use For loop,
In between for loop you apply the if else condition.
<?php if( have_rows('split_effect_page_builder') ): ?>
<div class="split-left">
<?php
for($i=1;$i<have_rows('split_effect_page_builder');$i++){
the_row(); ?>
<?php get_template_part('template-parts/page', 'builder'); ?>
<?php
if($i % 2 == 0){
echo '</div><div class="split-right">';
}
else{
echo '</div><div class="split-left">';
}
?>
</div>
<?php } ?>
</div>
</div>

PHP if condition does not work anymore when inside a PHP foreach statement

One of the conditions in my if statement does not work anymore when the if statement is inside a foreach statement. I understand where does the issue come from, but don't know how to solve it:
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this example, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.

Group items in while loop

I want to add some markup in the while loop, so that each three items are wrapped in a <ul> and each of the ul should be wrapped in a div. There can be maximum 6 items, and I want to get following output:
<div class="one">
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</div>
<div class="two">
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</div>
I am trying following code:
<div class="one">
<ul>
<?php
$i = 0 ;
while (have_posts()) : the_post();
$i++; ?>
<li>...</li>
<?php
if ($i === 3){
echo "</ul></div><div class='two'><ul>";
$right_div = true;
}
?>
}
if ($right_div){
</ul></div>
<?php } ?>
?>
It works fine if there are at least 3 items, but if there are less than 3, then breaks the code as it does not close the ul and div.
It is important to use the while loop because its part of a WordPress theme which uses the while loop to get the posts.
Im not sure I get it, but I think the problem is:
if ($right_div){
</ul></div>
<?php } ?>
That should not be a condition, that should be always printed.
If I'm understanding correctly, you're saying you want to
group things in group of 3's, but if you have 11 items or something
like that it breaks. If that's the case, then on the last iteration
of the while loop you should close the ul and div elements.
Like so:
if(condition){
echo "</ul></div>";
}
Taking reference from http://codex.wordpress.org/Function_Reference/have_posts
function more_posts() {
global $wp_query;
return $wp_query->current_post + 1 < $wp_query->post_count;
}
if (!more_posts()){
echo "</ul></div><div class='two'><ul>";
$right_div = true;
}
Oh and i am not a wordpress guy :P

Categories