I'm not that good at php. What i have is a list of items looped with endforeach. What I want is that the first loop will have a class of col-lg-12 and from the second one that class will become col-lg-6. How can I achive that?
Here's the code:
<?php $firstLoop = true;
foreach( $network_value as $key => $value ){
if( $firstLoop ){
echo '<div class="col-lg-12">';
}
echo '<div class="col-lg-6">';
$firstLoop = false;
} ?>
^This is the code that I've tried, but it's not working how i wanted.
<?php if ($img): ?>
<img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
<?php endif; ?>
<h4>
<div class="circle"><?php echo $datePublic = date('d M', strtotime($page->getCollectionDatePublic())); ?></div>
<br>
<a class="blogTitle" href="<?php echo $url ?>" target="<?php echo $target ?>"><?php echo $title ?></a></h4>
<h6>Posted by <?php echo $author; ?></h6>
<br>
<p><?php echo $description ?></p>
</div>
<?php endforeach; ?>
The most simple way to do it is to add a counter and check if it is the first value in the counter.
<?php
$counter = 0;
foreach( $network_value as $key => $value )
{
if($counter == 0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
$counter++;
}
?>
Also I want to add that there are two ways of using foreach and if-statements, but you are trying to mix them, which is wrong.
The first method is using brackets "{" and "}":
foreach($users as $user) {
// do things for each user
echo $user->name; // Example of writing out users name
}
And if-statement:
if(true) {
// do something
}
The second method is using "foreach(statement):" and "endforeach;"
foreach($users as $user):
// do things for each user
echo $user->name; // Example of writing out users name
endforeach;
And if-statement:
if(true):
// do something
endif;
Edited:
In your case you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} if($key==1){
echo '<div class="col-lg-6">';
}
endforeach; ?>
Or you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
endforeach; ?>
OLD:
foreach($array as $key=>$row){
if($key==0){
echo '<div class="col-lg-12"></div>';
}
if($key==5){
echo '<div class="col-lg-6"></div>';
}
// OR try use %
if($key%2 == 0){
echo '<div class="col-lg-12"></div>';
} else {
echo '<div class="col-lg-12"></div>';
}
Related
I've been racking my brain for weeks trying to remove this block of code, it's from paid memberships pro plugin.
I need to remove the below section as it produces the file fields on the user profile.
need to remove it and put it in a loop of its own and then print it, as I need it to be in a separate <section> from the rest of the fields that are in that original loop.
//----Trying to remove this piece of code----//
?>
<strong><?php echo $field[0]; ?></strong>
<div class="Test"><?php echo pmpromd_display_file_field($meta_field); ?></div>
<?php
//---This is the FullSnippet with the above code included----//
if(!empty($fields_array))
{
foreach($fields_array as $field)
{
if(empty($field[0]))
break;
// Fix for a trailing space in the 'fields' shortcode attribute.
if ( $field[0] === ' ' ) {
break;
}
$field_val = $field[1];
$meta_field = $pu->$field_val;
if(!empty($meta_field))
{
?>
<section class="pmpro_member_directory_<?php echo esc_attr($field[1]); ?>">
<?php
if(is_array($meta_field) && !empty($meta_field['filename']) )
{
//this is a file field
?>
<strong><?php echo $field[0]; ?></strong>
<div class="brendan"><?php echo pmpromd_display_file_field($meta_field); ?></div>
<?php
}elseif(is_array($meta_field)){
//this is a general array, check for Register Helper options first
if(!empty($rh_fields[$field[1]])) {
foreach($meta_field as $key => $value)
$meta_field[$key] = $rh_fields[$field[1]][$value];
}
?>
<strong><?php echo $field[0]; ?></strong>
<ul class="servicesList"><?php echo '<li>'.implode("</li>\n<li>",$meta_field).'</li>';?></ul>
<?php
}
elseif(!empty($rh_fields[$field[1]]) && is_array($rh_fields[$field[1]]) )
{
?>
<strong><?php echo $field[0]; ?></strong>
<?php echo $rh_fields[$field[1]][$meta_field]; ?>
<?php
}
else
{
if($field[1] == 'user_url')
{
?>
<?php echo $field[0]; ?>
<?php
}
else
{
?>
<strong><?php echo $field[0]; ?></strong>
<?php
$meta_field_embed = wp_oembed_get($meta_field);
if(!empty($meta_field_embed)){
echo $meta_field_embed;
}else{
echo make_clickable($meta_field);
}
?>
<?php
}
}
?>
</section>
<?php
}
}
}
?>
I am trying to get shuffle and only 19 items in a for each loop. I used shuffle() and if (++$i == 19) {break;}, but the problem is one time it returns 12 items, one time 13, and another time 14. What did I do wrong?
Here is the code to check:
<?php
$i = 0;
shuffle($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
if (++$i == 19) {
break;
}
}
?>
Increase $i only when a result has been printed. Check the value before starting a new foreach loop.
<?php
$i = 0;
shuffle($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
$i++;
}
if ($i == 19) {
break;
}
}
?>
I think you want to shuffle array,
Use this function,
function shuffle_assoc(&$array) {
$keys = array_keys($array);
shuffle($keys);
foreach ($keys as $key) {
$new[$key] = $array[$key];
}
$array = $new;
return true;
}
$i = 0;
shuffle_assoc($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
if (++$i == 19) {
break;
}
}
?>
I hope this will help you solve your problem.
A simplified version of your code
foreach ($children as $child) {
if ($child['name_total'] > 0) {
// display stuff
}
if (++$i == 19) {
break;
}
}
So $i is incremented every time, even if nothing is displayed. If $child['name_total'] is 0, nothing will be displayed - but $i is still incremented.
If you want 19 items displayed, you need to only increment $i when something is actually displayed:
foreach ($children as $child) {
if ($child['name_total'] > 0) {
// display stuff
if (++$i == 19) {
break;
}
}
}
Please try the following.
<?php
$counter = 0;
shuffle($children);
foreach ($children as $child)
{
if ($counter < 18)
{
break;
}
if($child['name_total'] > 0)
{
$counter++;
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : "");?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
}
?>
I'm struggling with a problem and I thought someone could help me. I'm trying to insert a clear div after a certain amount div containers. Now in this example I've managed to get it to work that after the first three a clear div is placed, but I want it to be inserted after each set of 3 divs depending on how many featured articles I've setup (3, 6, 9 etc).
$featured_articles = get_field('featured_articles', false, false );
$id = $featured_articles[0];
//var_dump();
$numberOfArticles = count($featured_articles);
//var_dump($featured_articles);
echo "<div class='container'>";
echo "<div class='row-fluid'>";
for ($i=0; $i < $numberOfArticles; $i++) {
if ($featured_articles) {
$id = $featured_articles[$i];
//var_dump($featured_articles);
if ( has_post_thumbnail($id) ) {
echo "<div class='span4'>";
echo "<article class='post__holder recipes'>";
echo "<a href='".get_permalink($id)."' class='image'>";
echo get_the_post_thumbnail( $id, 'medium-thumb' );
$image_large = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'large');
echo "</a>";
}
echo "<a href='".get_permalink($id)."'><h3>".get_the_title($id)."</h3></a>";
echo "<div class='starRating'>";
echo "<img src='".home_url()."/wp-content/themes/BUZZBLOG-theme/images/StarRating.svg' alt='Testergebnis'>";
echo "<div class='bar' style='width:".get_field('rating', $id)."%'></div>";
echo "</div>";/** end starrating **/
echo "<a href='".get_permalink($id)."'><div class='readmore-button'>Zum Testbericht...</div></a>";
echo "</div>";
echo "</article>";
if ($i >= 2) {
if($i == 2) {
echo "<div class='clearfix'>";
}
if ($i == $numberOfArticles) {
echo "</div>";
}
}
}
}
echo "</div>";
echo "</div>";
Try this It will check for multiples of 3 and place a clearfix div
Also dont forget to start the $i from 1 upto $i <= $numberOfArticles
$featured_articles = get_field('featured_articles', false, false );
$id = $featured_articles[0];
//var_dump();
$numberOfArticles = count($featured_articles);
//var_dump($featured_articles);
echo "<div class='container'>";
echo "<div class='row-fluid'>";
for ($i=1; $i <= $numberOfArticles; $i++) {
if ($featured_articles) {
$id = $featured_articles[$i-1];
//var_dump($featured_articles);
if ( has_post_thumbnail($id) ) {
echo "<div class='span4'>";
echo "<article class='post__holder recipes'>";
echo "<a href='".get_permalink($id)."' class='image'>";
echo get_the_post_thumbnail( $id, 'medium-thumb' );
$image_large = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'large');
echo "</a>";
}
echo "<a href='".get_permalink($id)."'><h3>".get_the_title($id)."</h3></a>";
echo "<div class='starRating'>";
echo "<img src='".home_url()."/wp-content/themes/BUZZBLOG-theme/images/StarRating.svg' alt='Testergebnis'>";
echo "<div class='bar' style='width:".get_field('rating', $id)."%'></div>";
echo "</div>";/** end starrating **/
echo "<a href='".get_permalink($id)."'><div class='readmore-button'>Zum Testbericht...</div></a>";
echo "</div>";
echo "</article>";
if($i%3==0) {
echo "<div class='clearfix'></div>";
}
}
}
echo "</div>";
echo "</div>";
You can do like this:-
for ($i=0; $i < $numberOfArticles; $i++) {
// your code
if($i%3==0) echo '<div class="clearfix"></div>';
}
You can use array_chunk() function to divide your array in group of 3 members each and you can iterate over each array.
PHP is a server side scripting language that is embedded in HTML.
So use this feature in your code instead of writing echo "html element" every where in code.
I think you have miss match of div element. Because <article> must be close before the <div class='span4'> but in your code it is not there.
$featured_articles = get_field('featured_articles', false, false );
$dividedIn3GroupsEach = array_chunk($featured_articles, 3);
foreach ($dividedIn3GroupsEach as $array) {
?>
<div class='container'>
<div class='row-fluid'>
<?php
foreach ($array as $key => $value) {
$id = $featured_articles[$key];
if (has_post_thumbnail($id)) {
?>
<div class='span4'>
<article class='post__holder recipes'>
<a href='<?php echo get_permalink($id); ?>' class='image'>
<?php
echo get_the_post_thumbnail($id, 'medium-thumb');
$image_large = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'large');
?>
</a>
<?php } ?>
<a href='<?php echo get_permalink($id); ?>'><h3><?php echo get_the_title($id); ?></h3></a>
<div class='starRating'>
<img src='<?php echo home_url() ?>"/wp-content/themes/BUZZBLOG-theme/images/StarRating.svg' alt='Testergebnis'>
<div class='bar' style='width:<?php echo get_field('rating', $id); ?>%'></div>
</div>
<a href='<?php echo get_permalink($id); ?>'><div class='readmore-button'>Zum Testbericht...</div></a>
</article>
</div>
<?php }
?>
</div> <!-- row-fluid end -->
</div> <!-- container end -->
<div class='clearfix'></div>
<?php } ?>
I hope this may help you.
<div class="puffar">
<?php
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children($post->post_parent, $all_wp_pages) : get_page_children($post->ID, $all_wp_pages);
$i = 0;
//Build custom items
echo "<div class='row'>";
foreach ($children as $child) {
?>
<div class="col-sm-6">
<div class="puff">
<div class="puff-image-holder">
<?php echo get_the_post_thumbnail($child->ID, 'full'); ?>
</div>
<fieldset class="linedHeadline hlmedium">
<legend><?php echo get_the_title($child->ID); ?></legend>
</fieldset>
<?php echo get_field("puff_introtext", $child->ID); ?>
<?php
$values = get_field('puff_lanktext', $child->ID);
if (get_field("popup_eller_lank", $child->ID) == "popup") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage open-popup"
href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
</fieldset>
<?php
} elseif (get_field("popup_eller_lank", $child->ID) == "extern") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage"
href="<?php echo get_field("puff_lank", $child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
<?php
$i++;
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
} else {
}
?>
</div>
</div>
<?php } ?>
</div>
</div>
I want every 2 items that's rendered out to be wrapped in a <div class="row">, however I can't figure it out. Can anyone help me?
So basically the row should wrap every 2 elements that is getting looped. I have been stuck on this forever... Hopefully anyone got better expertise than me hehe.
The div="row" should wrap the col-sm-6 and class="puff".
$i = 0;
foreach ($children as $child) {
$i++;
// your code goes here
if($i % 2 == 0) {
echo "</div><div class='row'>";
// reset the counter to 0
$i = 0 ;
}
}
use proper logic
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
First check if its mod by 2 or not (Gives 0 value after MOD), then close div , open new.
Now increase counter . Because for the first time , i will be 0 , then you increment it and then you use logic. So in short counter shoul be incremented at the end only not in between before you do any operation/logic.
Updated
Use code as it is **: issue was you have i++ and your condition in 3rd else if which never executed. So took it outside All and just before foreach.
<div class="puffar">
<?php
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children($post->post_parent, $all_wp_pages) : get_page_children($post->ID, $all_wp_pages);
$i = 0;
//Build custom items
echo "<div class='row'>";
foreach ($children as $child) {
?>
<div class="col-sm-6">
<div class="puff">
<div class="puff-image-holder">
<?php echo get_the_post_thumbnail($child->ID, 'full'); ?>
</div>
<fieldset class="linedHeadline hlmedium">
<legend><?php echo get_the_title($child->ID); ?></legend>
</fieldset>
<?php echo get_field("puff_introtext", $child->ID); ?>
<?php
$values = get_field('puff_lanktext', $child->ID);
if (get_field("popup_eller_lank", $child->ID) == "popup") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage open-popup"
href="<?php echo get_page_link($child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
</fieldset>
<?php
} elseif (get_field("popup_eller_lank", $child->ID) == "extern") {
?>
<fieldset class="linedHeadline hlmedium">
<legend><a class="linktopage"
href="<?php echo get_field("puff_lank", $child->ID); ?>"><?php echo get_field("puff_lanktext", $child->ID); ?> </a>
</legend>
<?php
} else {
}
?>
</div>
</div>
<?php
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
} ?>
</div>
</div>
First set $i=0;
if ($i % 2 == 0) {
echo "</div><div class='row'>";
}
$i++;
I am using array_chunk but i want to be able to echo something different for the first array in the chunk.
Not sure on how to do this and could not find anything. Any help/guidance would be appreciated.
Say i use:
foreach (array_chunk($entries, 4) as $row) {
if (the array chunk is the first) {echo '<div class="cn_page" style="display:block;">';}
else {echo '<div class="cn_page">';}
}
Full code below:
echo '
<div id="cn_preview" class="cn_preview">';
foreach($entries as $entry){
echo '<div class="cn_content" style="top:5px;">
<img src="newsslider/images/polaroidphotobar.jpg" alt=""/>
<h1>'.$entry->title.'</h1>
<span class="cn_date">'.$entry->modified.'</span>
<span class="cn_category"></span>
<p>'.$entry->text.'</p>
Read more
</div>';
}
echo '</div>';
echo '<div id="cn_list" class="cn_list">';
foreach (array_chunk($entries, 4) as $row) {
if (the array chunk is the first) {echo '<div class="cn_page" style="display:block;">';}
else {echo '<div class="cn_page">';}
foreach ($row as $entry) {
$i++;
if ($i == 1){echo '<div class="cn_item selected">';}
else {echo '<div class="cn_item">';}
echo '<h2>'.$entry->title.'</h2>
<p>'.$entry->text.'</p>
</div>';
echo '</div>';
}
echo '</div>';
}
echo'<div class="cn_nav">
<a id="cn_prev" class="cn_prev disabled"></a>
<a id="cn_next" class="cn_next"></a>
</div>
</div>
</div>';
That would be:
foreach (array_chunk($entries, 4) as $key => $row) {
if ($key == 0) {
echo '<div class="cn_page" style="display:block;">';
} else {
echo '<div class="cn_page">';
}
}