How to show a single Item in cakePHP - php

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;
?>

Related

Adding div class based on PHP variable from ACF repeater field?

I am looping through a repeater field in Advanced Custom Fields and displaying divs for each item in the repeater. If the index is equal to 0, I want to add a special class to just that div. Is this possible? Here's what I've tried:
<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
<?php while (have_rows('products')) : the_row(); ?>
<div class="product <?php if ($i == 0) { echo 'active-class'; } ?>"></div>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>
Unfortunately this is not working.
Instead of doing the conditional within the class, I just did it on the outside and defined two different class tags based on the condition:
<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
<?php while (have_rows('products')) : the_row(); ?>
<div <?php if ($i == 0) { ?>class="product active"<?php } else { ?>class="product"<?php } ?> ></div>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>

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>

Define Grid Based on Multiple Item Counts

Looking to solve a problem I'm having dynamically defining classes using WordPress Advanced Custom Fields' Repeater field. I've done some digging but haven't been able to find a resource with the specificity that I'm looking for.
I'm displaying a series of portfolio images, and depending on how many there are I would like to give certain photos specific class names within the Foundation grid. Essentially I'm looking to do the following:
For items 1-4: Apply a class of large-6
For items 5+: Apply a class of large-4
If only 1 item: Apply a class of large-8
I've been able to get the first two examples to work, but I'm having trouble defining a third set of parameters. I've tried a number of different routes but I'm not sure what's best at this point. See below for a mockup of what I am looking to accomplish, as well as my code example.
Appreciate any advice!
<div class="row portfolio-examples">
<?php
if( have_rows('portfolio_examples') ):
while ( have_rows('portfolio_examples') ) : the_row();
$count = 0;
$work = get_sub_field('portfolio_work');
?>
<?php if ($count != 0 || $count < 4) :?>
<div class="large-6 medium-6 columns end">
<?php if( $work ): ?>
<img src="<?php echo $work['url']; ?>" alt="<?php echo $work['alt'] ?>" />
<?php endif; ?>
</div>
<?php else: ?>
<div class="large-8 medium-8 large-centered medium-centered columns">
<?php if( $work ): ?>
<img src="<?php echo $work['url']; ?>" alt="<?php echo $work['alt'] ?>" />
<?php endif; ?>
</div>
<?php endif; ?>
<?php
$counter++;
endwhile;
endif;
?>
</div>
Your question is not clear enough to me because you say Apply a class of large or Apply a class of small, but then in your code you use .large-6 and also .large-8.
Anyway, supposing what you're trying to achieve (given your image), your PHP code should be something like this:
<div class="row portfolio-examples">
<?php
if (have_rows('portfolio_examples')) :
/* We need to know the total number
* of rows in your repeater since you have
* a case where there's only one item.
*/
$portfolio = get_field('portfolio_examples');
$count = is_array($portfolio) ? count($portfolio) : 0;
$index = 0;
while (have_rows('portfolio_examples')) : the_row();
if ($count == 1) :
/* Place here your code for when
* there's only 1 item in your portfolio.
*/
?>
<div class="medium"><!-- Medium item --></div>
<?php
elseif ($count > 1 && $index <= 3) :
// Code for your first 4 items
?>
<div class="large"><!-- Large item --></div>
<?php
elseif ($count > 1 && $index > 3) :
// Code for your 5+ items
?>
<div class="small"><!-- Small item --></div>
<?php
else :
// There are no items in your portfolio.
endif;
$index++;
endwhile;
endif;
?>
</div>
Notice: You were reseting to 0 your iterator index ($count in your case) in every iteration. Also, you were incrementing $counter (not $count) which wasn't defined.

While loop adding extra empty element

I am using the following to wrap every 3 divs, and wrap the the 4th-5 the same way if there is only 2. (same would apply it was every 6, 4, etc)
When its only 3 divs/elements they get wrapped as they should. But then an empty element gets created as well. So in the example below. Lets say you had 3 divs per list item. Well, in this one you only had 3, but an empty "list item" gets created containing nothing. How can I append my code so it doesn't create an empty element? (in my case, using the repeater with a flexslider, empty slide is being produced)
<?php //going to wrap every 3 in this example
if ( get_field( 'your_repeater_name' ) ): ?>
<?php $index = 1; ?>
<?php $totalNum = count( get_field('your_repeater_name') ); ?>
<li>
<?php while ( has_sub_field( 'your_repeater_name' ) ): ?>
<div class="col-sm-4">
<?php the_sub_field( 'your_sub_field' ); ?>
</div>
<? if ($index % 3 == 0) : ?>
<? if ($index < $totalNum) : ?>
// more rows, so close this one and start a new one
</li>
<row>
<? elseif ($index == $totalNum) : ?>
// last element so close row but don't start a new one
</li>
<? endif; ?>
<? endif; ?>
<?php $index++; ?>
<?php endwhile; ?>
<?php endif; ?>
Try changing the "loop" part of your script like this:
<li>
<?php while ( has_sub_field( 'your_repeater_name' ) ): ?>
<div class="col-sm-4">
<?php the_sub_field( 'your_sub_field' ); ?>
</div>
<?php if ($index % 3 == 0 && $index < $totalNum) : ?>
</li><li>
<?php endif; ?>
<?php $index++; ?>
<?php endwhile; ?>
</li>
The difference is the last </li> outside the loop (because it needs to be closed in any case, there's no need to put it inside an if), so now you only have to check if $index < $totalNum to insert another row.

Need to add a counter in php to separate the rows every 4 columns

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++;
}

Categories