Multiple divs with a repeater field of image - php

i´m trying to display multiple divs with the images of a repeater field (ACF plugins).
For example Div 1 with image 1 - Div 2 with image 2 - etc...
with this markup:
<div id="project" class="item">
<a href="#">
<img src="img/project1.jpg" alt="project1" width="240" height="173">
</a>
<div class="art_title">
<p>SWEET LIFE #1</p>
</div>
<div class="mask"></div>
</div>
So, repeating it thru the html I displayed all the images, but now i´m integratin it in the wordpress and I have the following problem:
I´m using the repeater field to get all the images, so with this code:
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
echo '<div id="project_slider" class="item">';
foreach($slides as $s) {
echo '<div class="aimagediv" >'; //adding the start tag of the div
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //closing the first div,not sure if you want this, its optional
}
?> <?php endwhile; // end of the loop. ?>
I get the images of the repeater but it displays all into the same div, it doesn´t create a new div id=¨project". So it shows like this:
And the markup created on the page it´s like all the images are into the same div.
<div id="project" class="item">
<a href="#">
<img src="img/project1.jpg" alt="project1" width="240" height="173">
<img src="img/project1.jpg" alt="project1" width="240" height="173">
</a>
<div class="art_title">
<p>SWEET LIFE #1</p>
</div>
<div class="mask"></div>
</div>
What i´m doing wrong?

<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
foreach($slides as $s) {
echo '<div id="project_slider" class="item">';
echo '<div class="aimagediv" >'; //adding the start tag of the div
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //closing the first div,not sure if you want this, its optional
}
}
?> <?php endwhile; // end of the loop. ?>

Related

Best practice when echo repeated code

I have a question that bothers me for a long time. Lets say i have a code with several nested div's and span's. All these compose a square with an image inside.
echo '<div> <div> <div> <div> <span> <img src='.$image.'> </span></div></div></div>';
Only that this code has about 15 rows.
From what i know when echo-ing the results from db in that form i put in the loop the whole html code. It looks clumsy this way.
It is there a better practice ?
foreach ($query->result() as $row)
{
$row->address_link=strtolower($row->network);
echo '<li class="col-md-3 isotope-item '.$row->network.'">';
echo '<div class="portfolio-item img-thumbnail">';
echo '<table border="0"><tr>';
echo '<a href="order/'.$row->address_link.'/'.$row->value.'" class="thumb-info">';
echo '<img alt="" class="img-responsive" src="img/'.$row->address_link.'.png">';
echo '<span class="thumb-info-title">';
echo '<span class="thumb-info-inner">'.$row->value.' Euro</span>';
echo '</span>';
echo '<span class="thumb-info-action">';
echo '<span title="Universal" href="order/'.$row->address_link.'/'.$row->value.'" class="thumb-info-action-icon"><i class="icon icon-link"></i></span>';
echo '</span>';
echo '</a>';
echo '</div>';
echo '</tr><tr>';
echo '<span class="thumb-info-type">'.$row->value*$row->rate.' Eur</span>';
echo '</tr></table>';
echo '</li>';
}
If you are new to php you can define a function for this:
function wrapImage($src){
return '<div> <div> <div> <div> <span> <img src='.$src.'> </span></div></div></div>';
}
And just use echo wrapImage($src) where you need it with different params.
EDIT: consider following way of presenting the data:
<?php
$query = 'select * from Unicorns';
foreach ($query->result() as $row){
$row->address_link=strtolower($row->network);
?>
<!-- html -->
<li class="col-md-3 isotope-item <?php echo $row->network; ?>">
<div class="portfolio-item img-thumbnail">
<table border="0"><tr>
<a href="order/<?php echo $row->address_link.'/'.$row->value; ?>" class="thumb-info">
<img alt="" class="img-responsive" src="img/'.$row->address_link.'.png">
<span class="thumb-info-title">
<span class="thumb-info-inner"><?php echo $row->value; ?> Euro</span>
</span>
<span class="thumb-info-action">
<span title="Universal" href="order/<?php echo $row->address_link.'/'.$row->value ?>" class="thumb-info-action-icon"><i class="icon icon-link"></i></span>
</span>
</a>
</div>
</tr><tr>
<span class="thumb-info-type"><?php echo ($row->value*$row->rate); ?> Eur</span>
</tr></table>
</li>
<!-- /html -->
<?php } ?>
It is called spaghetti code .. and it is NOT the best practice but is better then your example in case the HTML is more then the PHP data.
first of all, dont use echo in loops (optimalization), store your output in variable and print it only once.
Repeated code can be stored inside function
function square($image){
return '<div> <div> <div> <div> <span> <img src='.$image.'> </span></div></div></div>';
}
$output = '';
while ($loop){
$output .= square($image);
}
echo $output

Move menu next to logo instead of below or above in WordPress theme (header.php)

I want to move the menu next to the logo instead of under it in an WordPress theme.
See the actual page I'm working on here: http://wwconsult.no/client/alive/
I found the code for the menus placement in the header.php, here's the snippet:
<?php
if ( $smof_data['head_menu_style'] == "menu_default" ){
if ( has_nav_menu('top_navigation', 'GoGetThemes') ) {
echo '<div id="menu_back">';
menu_back();
echo '</div>';
}
echo'<div class="container">'.$logo.'
<div class="nav'.$navClass.'">
<nav>
'.$parallax_menu.'
</nav>
</div>';
if(!is_page_template('home-index.php')){
echo '<div class="mob_nav"><nav><span class="trigger"></span>'.$parallax_menu.'</nav></div>';
} else{
echo '<div class="mob_nav js"><nav><span class="trigger"></span>'.$parallax_menu.'</nav></div>';
}
echo '</div>';
} else if ( $smof_data['head_menu_style'] == "menu_dropdown" ) {
echo'<div class="container">'.$logo.'
<div class="nav'.$navClass.'">
<nav>
<div id="menu_current" class="'.$menu_cur.'">
<a href="'.$href.'" class="menu_1">
<span class="menu_name">'.$smof_data["home_menu_name"].'</span><span class="hover"><span class="arr"></span></span>
</a>
</div>
'.$parallax_menu.'
</nav>
</div>
<div class="mob_nav js"><nav><span class="trigger"></span>'.$parallax_menu.'</nav></div>
</div>';
if ( has_nav_menu('top_navigation', 'GoGetThemes') ) {
echo '<div id="menu_back">';
menu_back();
echo '</div>';
}
// Header Border
echo'<div class="head_box bot_box"></div>';
}
?>
I'm no php wizard, but I have tried to make it work without much succsses. Got it above the logo, got blank page, but that about it... Any help would be highly appreciated.
replace below line
echo'<div class="container">'.$logo.'
<div class="nav'.$navClass.'">
with
echo'<div class="container">'.$logo;
if ( has_nav_menu('top_navigation', 'GoGetThemes') ) {
echo '<div id="menu_back">';
menu_back();
echo '</div>';
}
echo '<div class="nav'.$navClass.'">

Show 2 results by div

I have the following foreach:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
?>
<div class='wrapper'>
<div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246"></div>
</div>
<?php } ?>
But thus appears only a div .album within each div .wrapper. How do I see two divs .album within each div .wrapper?
UPDATE
Guys, found the solution:
<?php
$total = 0;
foreach($result15 as $row15){
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
if($total == 0){
echo '<div class="wrapper">';
}
?>
<div class="album" data-disco="disco<?php echo $id15; ?>">
<img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
</div>
<?php
$total = $total + 1;
if($total % 2 == 0){
echo '</div>';
$total = 0;
}
}
?>
<div class='wrapper'>
<div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
<div class="album"><img src="img/..." alt="" width="246" height="246"></div>
</div>
Something like this?
EDIT
I dont understand what you want. But you can do this to get two divs, but you will need to get your image path for the second div image:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '</div>';
} ?>
Try this:
<?php
foreach($result15 as $row15) {
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;
echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '</div>';
} ?>
A nice solution could be to use array chunk considering you want to treat the data in 'chunks' of 2 images at a time. This way, if you want to alter how many images appear in a wrapper you only need to change your chunk size.
$chunkSize = 2;
foreach (array_chunk($result15, $chunkSize) as $wrapperImages) {
echo '<div class="wrapper">';
foreach ($wrapperImages as $image) {
$thumb = $image->thumb;
echo '<div class="album"><img src="img/'.$thumb.' alt="" width="246" height="246"></div>';
}
echo '</div>';
}

Browse to specific slide - jquery cycle

I have a page that features certain image thumbs from a custom field. What I need to do is have the link on the images thumbs link directly to it's image in the slider, the problem is that the slider is in other page not the same.
This is the code i'm using to get the images from the custom post repeater field:
<?php $query = new WP_Query( 'post_type=artworks_post&posts_per_page=-1&order=DESC' ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $slides = get_field('project_slider');
// Grabs the array
if($slides) {
foreach($slides as $s) {
echo '<div id="project_slider" class="item"> ';
echo '<div class="aimagediv" >'; //
echo '<a>';
echo '<img src="'.$s['project_image'].'" alt="" width="240" />';
echo '</a>';
echo '</div>'; //aimagediv
echo '<div class="art_title">';
echo '<p>SWEET LIFE</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //first div
}
}
?>
<?php endwhile; // end of the loop. ?>
I'm using this to link to the slider page ( I know it's wrong link that way but I had to do it to show you how it links to slider):
$('#project_slider').click(function() {
location.href = '?page_id=42';
});
and this is the code of the jquery cycle plugin i'm using for the slider:
$("#slideshow").css("overflow", "hidden");
$('ul#slides').cycle({
fx: 'fade',
timeout: 0,
prev: '#prev',
next: '#next',
after: onAfter
});
function onAfter(curr,next,opts) {
var caption = '' + (opts.currSlide + 1) + ' / ' + opts.slideCount;
$('.project_number p').html(caption);
}
How can I fix to link each project to it's corresponding position in the slider, instead of always opening in the start image of the slider?
here it's a working example of what i'm trying to achieve: loscarpinteros.net/#exhibitions any image get's you to the link of it in the slider position.
<?php $query = new WP_Query( 'post_type=artworks_post&posts_per_page=-1&order=DESC' ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $slides = get_field('project_slider');
// Grabs the array
if($slides) {
$i = 0;
foreach($slides as $s) {
echo '<div id="project_slider" counter="'.$i++.'" class="item"> ';
echo '<div class="aimagediv" >'; //
echo '<a>';
echo '<img src="'.$s['project_image'].'" alt="" width="240" />';
echo '</a>';
echo '</div>'; //aimagediv
echo '<div class="art_title">';
echo '<p>SWEET LIFE</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //first div
}
}
?>
<?php endwhile; // end of the loop. ?>
in JS
$('#project_slider').click(function() {
location.href = '?page_id=42&slide='+$(this).attr('counter');
});
and in JQuery cycle
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}
$("#slideshow").css("overflow", "hidden");
$('ul#slides').cycle({
fx: 'fade',
timeout: 0,
prev: '#prev',
next: '#next',
after: onAfter,
startingSlide: getURLParameter('slide'),
});
function onAfter(curr,next,opts) {
var caption = '' + (opts.currSlide + 1) + ' / ' + opts.slideCount;
$('.project_number p').html(caption);
}
BTW you can make href of a tag in your php loop.

Display multiple image on separated div with repeater field

I´m trying to display a list of div with images using the repeater field of ACF.
this is the code i´m using:
<?php $query = new WP_Query( 'post_type=artworks_post&posts_per_page=-1&order=DESC' ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$slides = get_field('project_thumbnails'); // Grabs the array
// Check if there is any data in the array before looping
if($slides) {
echo '<div id="project_slider" class="item">';
foreach($slides as $s) {
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
}
?>
<?php endwhile; // end of the loop. ?>
The problem with this is that it displays all images in the same div, instead of display every image of the repeater field in a new div.
what i´m doing wrong?
Im not a php expert but,i think is just a matter of missing closing tags for the first div, also im gonna put another div within the loop, it looks like this:
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
echo '<div id="project_slider" class="item">';
foreach($slides as $s) {
echo '<div class="aimagediv" >'; //adding the start tag of the div
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
echo '</div>'; //closing the first div,not sure if you want this, its optional
}
?> <?php endwhile; // end of the loop. ?>
Another alternative would be this (just figuring out what's the resulting layout you're expecting):
<?php $slides = get_field('project_thumbnails');
// Grabs the array
// Check if there is any data in the array before looping
if($slides) {
//we need to close this div
foreach($slides as $s) {
echo '<div id="project_slider" class="item">';
echo '<a href="#">';
echo '<img src="'.$s['project_thumb'].'" alt="" />';
echo '</a>';
echo '</div>'; //CLOSING THE DIV JUST ADDED
}
echo '<div class="art_title">';
echo '<p>SWEET LIFE2</p>';
echo '</div>';
echo '<div class="mask">';
echo '</div>';
}
?> <?php endwhile; // end of the loop. ?>
Hopefully this would be enough.

Categories