I'm trying to figure this out for hours but I can't. I just want to show the second post and skip the first one, just like the Wordpress offset function. I'm using AW blog extension for Magento and I want to skip the first post in the recent blog posts. Below is my modified code showing one post recent post in a homepage block. I just want to create another block that will show the second recent post. :(
<?php $posts = $this->getPosts(); ?>
<div id="messages_product_view">
<?php Mage::app()->getLayout()->getMessagesBlock()-> setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?>
<?php echo Mage::app()->getLayout()->getMessagesBlock()->getGroupedHtml(); ?>
</div>
<?php
foreach ($posts as $post):
if ($i++ >= 1) break;
?>
<div class="postWrapper">
<div class="postTitle">
<h3><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h3>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
</div>
<?php endforeach; ?>
I will recommend you to check the count and after printing it stop the loop, otherwise it will loop the entire array to the end and print just the second iteration.
$i=0;
foreach ($posts as $post){
if ($i==1) {
//print your div here
$i++; //added here after edit.
continue;
}else if ( $i>1 ){
break;
}
$i++;
}
This way it will only iterate twice.
Try changing:
foreach ($posts as $post):
if ($i++ >= 1) break;
to:
//set $i outside of loop
$i=0;
foreach ($posts as $post):
if ($i==0) {
$i++;
//skip the first record
continue;
}
Try this -
<?php $i=1;
foreach ($posts as $post):
if ($i > 1) {
?>
<div class="postWrapper">
<div class="postTitle">
<h3><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h3>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
</div>
<?php }
$i++;
endforeach; ?>
Related
I created a database with 5 categories and created a page to display the available categories that I pull from the database. The problem is that I have 4 divs that have the following class ( col-md-3 ) and one that is ( col-md-6 ).
Code for gathering the first 5 categories ( col-md-3 ):
<?php
$this->db->limit(4);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
foreach ($categories as $key => $category):
?>
<div class="gallery-item">
<div class="grid-item-holder">
<div class="listing-item-grid">
<img src="<?php echo base_url('uploads/category_thumbnails/').$category['thumbnail'];?>" alt="" />
<div class="listing-counter">
<span>2</span>Locations
</div>
<div class="listing-item-cat">
<h3><?php echo $category['name']; ?></h3>
<p><?php echo $category['name']; ?></p>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
Code for gathering another 1 category ( col-md-6 ):
<?php
$this->db->limit(1);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
foreach ($categories as $key => $category):
?>
<div class="gallery-item gallery-item-second">
<div class="grid-item-holder">
<div class="listing-item-grid">
<img src="<?php echo base_url('uploads/category_thumbnails/').$category['thumbnail'];?>" alt="" />
<div class="listing-counter">
<span>2</span>Locations
</div>
<div class="listing-item-cat">
<h3><?php echo $category['name']; ?></h3>
<p><?php echo $category['name']; ?></p>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
With this code, I get a display of categories but instead of displaying 5 categories, it repeats one of the displayed 4. How can I fix the code so that the display automatically continues to display the categories in all divs? If I forgot to write something, correct it or ask, I will update the question.
It looks like you are getting the first 4 items for the first foreach loop and then getting the first one again for the second loop. Would it make sense to get all 5 and process them in one single foreach loop only adding 'gallery-item-second' on the 5th item, something like ...
<?php
$this->db->limit(5);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
$index = 0;
foreach ($categories as $key => $category):
$index++;
if ($index == 5) {
echo '<div class="gallery-item gallery-item-second">';
} else {
echo '<div class="gallery-item">';
}
?>
<div class="grid-item-holder">
. . .
</div>
</div>
<?php endforeach; ?>
Or if you must have the second loop then get 5 items and only show the 5th.
And to make sure you get the same 5 you could use a sort criteria when you get the items.
The problem is resolved with math:
I have added the next code:
...
$num = 5;
$i = 0;
foreach ($categories as $key => $category):
if ($i < ($num - 1)) {
// show div col-md-3
} else {
// show div col-md-6
}
endif;
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>
}
Apologies if this has already been asked (I cannot find an answer) but I am using PHP and I am building a slider, but would like two images per slide, not one. So, in theory the foreach() needs to include two per each.
An example of the setup is as follows:
<?php foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php endforeach; ?>
I was thinking I could do something like a count...
<?php $index = 0; foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php $index++; ?>
<?php if ( $index % 2 == 0 && $index !=count($page->images) ) : ?>
<li></li>
<?php endif; ?>
<?php endforeach; ?>
But I got a little confused as this would insert something every 2... not include two of whatever the foreach loop is fetching at once.
Hope this makes sense and thanks in advance
Why so complicated? Use a simple for loop instead:
<?php for ($i=0; $i<count($page->images)-1; $i+=2) { ?>
<img src="<?php echo $page->images[$i]->url; ?>"/>
<img src="<?php echo $page->images[$i+1]->url; ?>"/>
<?php } ?>
Or even more elegant, a do/while loop:
<?php $i=0; do { ?>
<img src="<?php echo $page->images[$i++]->url; ?>"/>
<img src="<?php echo $page->images[$i++]->url; ?>"/>
<?php } while ($i<count($page->images)) ?>
Compared to using a foreach loop these approaches have another advantage: you do not create copies of all objects. This can make a huge difference if those objects are non-trivial.
Okay, I managed to work this out... hopefully it will help.
<div class="each-slide">
<?php $index = 0; foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php $index++; ?>
<?php if ( $index % 2 == 0 && $index !=count($page->images) ) : ?>
</div><div class="each-slide">
<?php endif; ?>
<?php endforeach; ?>
</div>
I have this code, and I need to close a row every 4 post. Every post is inside a div. I tried some things but I coudn't implement to my code.
<?php
echo "<div class='row'>";
global $post;
$all_events = tribe_get_events(
array(
'eventDisplay'=>'upcoming',
//'posts_per_page'=>10,
)
);
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="col-sm-3">
<span class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'j M'); ?></span>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) { ?>
<div class="event-thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="event-excerpt">
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="event-content">
<?php the_content(); ?>
</div>
<?php } ?>
</div>
<?php } //endforeach ?>
<?php wp_reset_query(); ?>
<?php
echo "<div class='row'>";
global $post;
$all_events = tribe_get_events(
array(
'eventDisplay'=>'upcoming',
//'posts_per_page'=>10,
)
);
$count = 1;
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="col-sm-3">
<span class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'j M'); ?></span>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) { ?>
<div class="event-thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="event-excerpt">
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="event-content">
<?php the_content(); ?>
</div>
<?php } ?>
</div>
<?php
if($count == 4){
echo "<div class='seperator'></div>";
$count =1;
}
?>
<?php $count++; } //endforeach ?>
<?php wp_reset_query(); ?>
I'd actually solve this 100% in CSS, so you don't need any counting or handling inside your PHP code.
Have a look at this JSFiddle.
float: left will cause the single elements to all follow each other (left aligned).
clear: left on every 4 * n + 1-th element (nth-child(4n+1)) will clear this, essentially forcing a line break.
There is one caveat to this: If there's no room for all 4 entries in one row, you'll end up with additional wrapping, which can be avoided by defining a fixed width for the container.
A simplified in-code version for PHP would just count the fields written and add a line break as necessary:
$i = 1; // counter
foreach ($events as $event) { // iterate over all events
if ($i++ % 4 == 0) // a % b will be 0 for 4, 8, etc.
echo '<br />'; // print the line break using whatever HTML you see fit.
print_event($event); // print the actual event
}
You might ask whether I check for the line break before actually printing an event: That's to prevent additional line breaks if the number of entries is a multiple of 4, i.e. I avoid having an empty trailing line.
You need the modulo operator. It works like this:
$i == 0;
foreach($some_array as $some_value){
if ($i % $number_to_divide_by == 0) {
// do something here every nth time
}
$i++;
}
I am using cake PHP and on my view/technical_slider/index.ctp is the following:
I am using a heavily modified version of this tutorial Featured Content Slider Using jQuery
<?php foreach ($technicalSlides as $technicalSlide):?>
<div id="nav-fragment-1" class="ui-tabs-panel">
<h2><?php echo $technicalSlide['TechnicalSlide']['title'];?></h2>
<p><?php echo $technicalSlide['TechnicalSlide']['description'] ; ?></p>
</div>
<?php endforeach; ?>
My question is: How can I display only certain id's or items and apply different class on the same div ? Because I only want record 1-3 skip 4-7 then show record 8-15?
I am applying different classes on them because they have different backgrounds, but share the same id.
You coud add a counter and then display only those you want.
<?php int $i = 0; ?>
<?php foreach ($technicalSlides as $technicalSlide):?>
<?php if ($i < 4 || $i > 7): ?>
<div id="nav-fragment-1" class="ui-tabs-panel">
<h2><?php echo $technicalSlide['TechnicalSlide']['title'];?></h2>
<p><?php echo $technicalSlide['TechnicalSlide']['description'] ; ?></p>
<?php endif; ?>
</div>
<?php endforeach; ?>
Although I would merge all classes and use attributes.
Add a counter variable (in this case $i), increment it ($i++) so it increases by one for every loop, and add your checks to an if() block to make sure you only write the ones you want to:
<?php
$i = 1;
foreach ($technicalSlides as $technicalSlide):
if($i < 4 || $i > 7) {
?>
<div id="nav-fragment-1" class="ui-tabs-panel">
<h2><?php echo $technicalSlide['TechnicalSlide']['title'];?></h2>
<p><?php echo $technicalSlide['TechnicalSlide']['description'] ; ?></p>
</div>
<?php
}
$i++;
endforeach;
?>