Dynamic CSS ID with PHP - php

A newbie here. Trying to increment CSS ID selector with PHP.
Following the code I have
<?php
$thumbnails = get_post_meta($post->ID, "plans", false);
if ($thumbnails[0] != '') :?>
<div id="images-box">
<?php foreach($thumbnails as $thumb) {
echo '<div class="holder">
<div id="image-'. $id .'" class="image-lightbox">
<span class="close">X</span>
<img src="' . $thumb . '" alt="" />
<a class="expand" href="#image-1"></a>
</div>';
} ?>
</div>
<?php endif; ?>
What I want is
<div id="image-1" class="image-lightbox">
<div id="image-2" class="image-lightbox">
<div id="image-3" class="image-lightbox">
I tried with many options that were answered here (http://codepad.org/OHuTxQPI) but couldn't get it right.

Simply add $id=1; and $id++; in your code.
<?php
$id = 1;
foreach($thumbnails as $thumb) {
echo '<div class="holder">
<div id="image-'. $id .'" class="image-lightbox">
<span class="close">X</span>
<img src="' . $thumb . '" alt="" />
<a class="expand" href="#image-1"></a>
</div>';
$id++;
} ?>
Edited: $id=1; should be used if you want to start from id='image-1'.

You're using $id as a counter but don't actually define or increment anywehre.
<?php
$thumbnails = get_post_meta($post->ID, "plans", false);
if ($thumbnails[0] != '') :?>
<div id="images-box">
<?php
$id = 0;
foreach($thumbnails as $thumb) {
echo '<div class="holder">
<div id="image-'. $id .'" class="image-lightbox">
<span class="close">X</span>
<img src="' . $thumb . '" alt="" />
<a class="expand" href="#image-1"></a>
</div>';
$id++;
} ?>
</div>
<?php endif; ?>

Unlike the rest, I'm using a straight for() loop instead of foreach() to give more control (ok it's really just cuz I'm OCD and like to use $i).
Here's the demo
PHP:
<?php
$thumbnails = array(0 => array('thumb' => 'image.jpg'), 1 => array('thumb' => 'image2.jpg'));
// $thumbnails = get_post_meta($post->ID, "plans", false);
if (isset($thumbnails)): ?>
<div id="images-box">
<?php
$i = 0;
$thumbs = count($thumbnails);
for($i;$i < $thumbs;$i++) { ?>
<div class="holder">
<div id="image-<?php print $i; ?>" class="image-lightbox">
<span class="close">X</span>
<img src="<?php print $thumbnails[$i]['thumb']; ?>" alt="" />
<a class="expand" href="#image-<?php print $i; ?>"></a>
</div>
<?php }; ?>
</div>
<?php endif; ?>
HTML output:
<div id="images-box">
<div class="holder">
<div id="image-0" class="image-lightbox">
<span class="close">X</span>
<img src="image.jpg" alt="" />
<a class="expand" href="#image-0"></a>
</div>
<div class="holder">
<div id="image-1" class="image-lightbox">
<span class="close">X</span>
<img src="image2.jpg" alt="" />
<a class="expand" href="#image-1"></a>
</div>
</div>

Use Counter for $id as told by John or use $id as index for foreach loop as below.
<?php
$thumbnails = get_post_meta($post->ID, "plans", false);
if ($thumbnails[0] != '') :?>
<div id="images-box">
<?php foreach($thumbnails as $id=> $thumb) {
echo '<div class="holder">
<div id="image-'. $id .'" class="image-lightbox">
<span class="close">X</span>
<img src="' . $thumb . '" alt="" />
<a class="expand" href="#image-1"></a>
</div>';
} ?>
</div>
<?php endif; ?>

Related

ACF PRO Image in Repeater not showing

The images are not showing in front-end. ACF settings screenshot
<?php
if( have_rows('how_it_works_functions') ):
$counter = 0;
while( have_rows('how_it_works_functions') ) : the_row();
$counter++;
?>
<div class="step<?php echo $counter; ?>">
<div class="row section-content">
<?php if($counter == 2) { ?>
<div class="col-md-8 image">
<img class="skip-lazy" src="<?php get_sub_field('how_it_works_functions_image')['url']; ?>" alt="<?php get_sub_field('how_it_works_functions_image')['alt']; ?>">
</div>
<div class="col-md-4 text">
<h3 data-aos="fade-left" data-aos-offset="300" data-aos-duration="1000"><?php the_sub_field('how_it_works_functions_name'); ?></h3>
<p data-aos="fade-up" data-aos-offset="100" data-aos-duration="1000"><?php the_sub_field('how_it_works_functions_description'); ?></p>
</div>
<?php } else { ?>
<div class="col-md-4 text">
<h3 data-aos="fade-right" data-aos-offset="300" data-aos-duration="1000"><?php the_sub_field('how_it_works_functions_name'); ?></h3>
<p data-aos="fade-up" data-aos-offset="100" data-aos-duration="1000"><?php the_sub_field('how_it_works_functions_description'); ?></p>
</div>
<div class="col-md-8 image">
<img class="skip-lazy" src="<?php get_sub_field('how_it_works_functions_image')['url']; ?>" alt="<?php get_sub_field('how_it_works_functions_image')['alt']; ?>">
</div>
<?php } ?>
</div>
</div>
<?php
endwhile;
endif;
?>
I already check the var_dump and it is getting the correct url but I still have broken image in front-end.
You need to echo that field.
Like this based on your code:
<img class="skip-lazy" src="<?php echo get_sub_field( 'how_it_works_functions_image' )['url']; ?>" alt="<?php echo get_sub_field( 'how_it_works_functions_image' )['alt']; ?>">
Alternative effective way:
<?php
$image = get_sub_field( 'how_it_works_functions_image' );
?>
<img class="skip-lazy" src="<?php echo esc_attr( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>">
Try out the above solution and let me know if it works or not.

solving repetitive html in word press template

I would like some help In creating a loop for my WP template.
I have the same div block repeated a few times, the class numbers are different and that's about it.
based of the sample blocks bellow can someone give me a example of how i could wrap this in a loop and change the class numbers ?
code block
If you see bellow some of the class names just differ with the number
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_1') ?>"
alt="<?php the_field('section_3_image_1_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_1'); ?>"
title="<?php the_field('title_1'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_2') ?>"
alt="<?php the_field('section_3_image_2_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_two', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_2'); ?>"
title="<?php the_field('title_2'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_3') ?>"
alt="<?php the_field('section_3_image_3_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_3', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_3'); ?>"
title="<?php the_field('title_3'); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<?pho $i=1; if( have_rows('parent_field') ):
while ( have_rows('parent_field') ) : the_row(); ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_' . $i) ?>"
alt="<?php echo get_field('section_3_image_' . $i . '_alt') ?>">
<h3 style="min-height: 150px"><?php echo get_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php echo get_field('link_' . $i); ?>"
title="<?php echo get_field('title_' . $i); ?>"
class="btn btn-lg btn-secondary">
<?php echo get_field('link_text'); ?>
</a>
</p>
</div>
<?php $i++; endwhile; endif; ?>
if you are working with while loop this will work fine.
This ACF field data and your while will work fine as this.
You can do with this code:
<?pho for($i = 0; $i < YOUR_MAX_VALUE; $i++): ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php the_field('section_3_image_' . ($i+1)) ?>"
alt="<?php the_field('section_3_image_' . ($i+1) . '_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_' . ($i+1)); ?>"
title="<?php the_field('title_' . ($i+1)); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link_text'); ?>
</a>
</p>
</div>
<?php endfor; ?>
Remember to set YOUR_MAX_VALUE to the value you need.
if you are working with for loop this will work fine.
<?pho for($i = 0; $i < YOUR_VALUE.length; $i++): ?>
<div class="col-6 col-lg-4">
<img class="mb-3" width="170" height="170" src="<?php
the_field('section_3_image_' . ($i+1)) ?>"
alt="<?php the_field('section_3_image_' . ($i+1) . '_alt') ?>">
<h3 style="min-height: 150px"><?php the_field('text_one', false, false) ?></h3>
<p class="pb-5">
<a href="<?php the_field('link_' . ($i+1)); ?>"
title="<?php the_field('title_' . ($i+1)); ?>"
class="btn btn-lg btn-secondary">
<?php the_field('link'); ?>
</a>
</p>
</div>

Magento category to owl carousel

This code is displaying only one product from each category, however I want it to display all products from each category in its own owl-carousel.
How can I fix this?
<?php
$categoryIds = array("3","5","6","12","7");
foreach($categoryIds as $categoryId){
$carouselcategoryProducts = $block->getCategoryProductsById($categoryId);
foreach ($carouselcategoryProducts as $carouselproduct) {
/*Get Thumbnail*/
$carouselimageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
$carouselproductImage = $carouselimageBlock->getImage($carouselproduct, 'category_page_grid');
?>
<div class="owl-carousel owl-theme">
<h4 class="item">
<a href="<?php echo $carouselproduct->getProductUrl(); ?>">
<div class="product_row2">
<div class="product_column2">
<img class="product_img2" <?php echo $carouselproductImage->toHtml(); ?><i class="far fa-clone compare"></i>
<p class="product_title2"><?php echo $carouselproduct->getName(); ?></p>
<p class="product_price2">€ <?php echo $carouselproduct->getFinalPrice(); ?>,-</p>
</div>
</div>
</a>
</h4>
</div>
<?php
}
}
?>
Fixed the issue.
Correct code:
<?php
$categoryIds = array(3,5,6,12,7);
foreach($categoryIds as $categoryId) :
//echo "<p>category id: </p>$categoryId";
$carouselcategoryProducts = $block->getCategoryProductsById($categoryId);
echo "<div class=\"owl-carousel owl-theme\">";
foreach ($carouselcategoryProducts as $carouselproduct) :
//Get Thumbnail
$carouselimageBlock = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
$carouselproductImage = $carouselimageBlock->getImage($carouselproduct, 'category_page_grid');
?>
<h4 class="item">
<a href="<?php echo $carouselproduct->getProductUrl(); ?>">
<div class="product_row2">
<div class="product_column2">
<img class="product_img2" <?php echo $carouselproductImage->toHtml(); ?><i class="far fa-clone compare"></i>
<p class="product_title2"><?php echo $carouselproduct->getName(); ?></p>
<p class="product_price2">€ <?php echo $carouselproduct->getFinalPrice(); ?>,-</p>
</div>
</div>
</a>
</h4>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
Correct Code:
$categoryIds = array(3,5,6,12,7);
foreach ($categoryIds as $cat)
{
$_category = $objectManager->create('Magento\Catalog\Model\Category')->load($cat);
echo "<div class=\"owl-carousel owl-theme\">";
$categoryProducts = $_category->getProductCollection()
->addAttributeToSelect('*');
foreach ($categoryProducts as $product) {?>
<h4 class="item">
<a href="<?php echo $product->getProductUrl(); ?>">
<div class="product_row2">
<div class="product_column2">
<img class="product_img2" <?php echo $product->toHtml(); ?><i class="far fa-clone compare"></i>
<p class="product_title2"><?php echo $product->getName(); ?></p>
<p class="product_price2">€ <?php echo $product->getFinalPrice(); ?>,-</p>
</div>
</div>
</a>
</h4>
}
echo "</div>";
}

Jump to a specific tab in WordPress

I have a code which works on my localhost but does not work when I implement it on my live site, which is hosted on WordPress.
I have the below code on another page:
<div class="smaller-col col-xs-6 col-md-3 product-thumb">
<a href='<?php echo "/the-pods#{$product_id}"; ?>' target="_blank" class="thumbnail">
<img class="img-responsive animated pulse eds-on-hover" src="<?php echo $product_image; ?>" width="120" height="120" alt="Hello Paisa">
</a>
<p><?php echo $product_tagline; ?></p>
</div>
The above page links to the page with the code below:
<div id="exTab1" class="">
<ul class="nav nav-pills">
<?php
$counter = 0; while($products->fetch()):
$product_id = $products->field('product_id');
$product_image = wp_get_attachment_url($products->get_field('product_logo.ID'));
?>
<li class="<?php if($counter == 0){ echo 'active'; } ?>">
<a href='<?php echo "#$product_id"; ?>' data-toggle="tab">
<img class="img-responsive" src="<?php echo $product_image; ?>"/>
</a>
</li>
<?php $counter++; endwhile; ?>
</ul>
<div class="tab-content">
<?php
$counter_2 = 0; while($products_2->fetch()):
$product_id_2 = $products_2->field('product_id');
$product_image_2 = wp_get_attachment_url($products_2->get_field('product_logo.ID'));
$product_desc_2 = $products_2->field('product_description');
$product_website_2 = $products_2->field('product_website');
?>
<div class="tab-pane <?php if($counter_2 == 0){ echo 'active'; } ?> fade in" id="<?php echo $product_id_2; ?>">
<?php echo '#' . $product_image_2; ?>
<?php echo "<p style='color:#777;'>" . $product_desc_2 . "</p>";?>
<?php echo $product_website_2; ?>
</div>
<?php $counter_2++; endwhile; ?>
</div>
</div>
<script>
var tabUrl = document.location.toString();
if(tabUrl.match('#')) {
$('.nav-pills a[href="#' + tabUrl.split('#')[1] + '"]').tab('show');
}
$('#nav-pills a').on('shown.bs.tab', function(e) {
window.location.hash = e.target.hash;
});
</script>
How do I implement the navigation part properly on Wordpress?

Twiiter Bootstrap carousel making dynamic with PHP

I am trying to make a carousel that uses Twitter Bootstrap default JavaScript and CSS but the images should be fetched from the MySQL database.
The HTML for the carousel that i am trying to make dynamic with php is like this:
<div class="carousel-inner">
<div class="active item">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginLast">
</div>
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
The end result for PHP file that i'm expecting is like this
<div class="carousel-inner">
<?php renderAds(); ?>
</div>
The PHP function that i wrote is like this :
function renderAds(){
$query = queryAds();
$query_exe = mysql_query($query);
if(mysql_num_rows($query_exe) == 0){
echo '<img src="images/sampleAd.jpg" >';
}else{
$numb =1;
$flag =1;
while($fetched_data = mysql_fetch_array($query_exe)){
if($numb == 1){
if($flag == 1){
echo '<div class="active item">';
}else{
echo '<div class="item">';
}
}elseif($numb == 4){
echo '';
echo '</div>';
$numb =0;
}else{
echo '';
}
$numb++;
$flag++;
}
echo '<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>';
echo '<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>';
}
}
Here, by differing from default carousel of twitter bootstrap, i made each slide with 4 images, i expect that each slide transition contains 4 images instead of as by default the single image for one slide. In HTML it works but when I replace it with my PHP function first slide works but if i press next arrow in carousel whole div collapses.
How can I make it work ?
Note : Here Last image must contain the class marginLast, and the
other should have marginTop and pull-left classes.
What I expect to be rendered in html is like this:
<div class="carousel-inner">
<div class="active item">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginLast">
</div>
<div class="item">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginLast">
</div>
<div class="item">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginTop">
<img src="images/sampleAd.jpg" class="pull-left marginLast">
</div>
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
How can i make my PHP function to display result markup like above?
Edit : The Output i get is like this:
<div id="myCarousel" class="carousel slide">
<!-- Carousel items -->
<div class="carousel-inner">
<div class="active item">
</div>
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div><!-- end carousel-inner -->
</div><!-- myCarousel -->
According to markup this should work but it doesn't, the arrow by default if there is no more slide than one should be disabled but it is clickable.
Have you MySQL query return just an array. That way, your query's reusable (could be then made into a function) and you can render the data however you see fit.
<?php
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
$sql = "SELECT * FROM table";
$res = $mysqli->query($sql);
$rows = array();
while ($row = $res->fetch_assoc()) {
$rows[] = $row;
}
?>
<div class="carousel">
<div class="carousel-inner">
<?php $i = 1; ?>
<?php foreach ($rows as $row): ?>
<?php $item_class = ($i == 1) ? 'item active' : 'item'; ?>
<div class="<?php echo $item_class; ?>">
<a href="<?php echo $row['url']; ?>">
<img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>" />
</a>
</div>
<?php $i++; ?>
<?php endforeach; ?>
</div>
</div>
You'll obviously need to change the database query, as well as the values then used in the foreach loop. I've also used the mysqli functions, as the mysql functions are now deprecated in favour of MySQLi (MySQL improved).
I think i came out with answer for my own question, even though all the above answers gave me many ideas. Thank you all
function renderAds(){
$query = queryAds();
$query_exe = mysql_query($query);
if(mysql_num_rows($query_exe) == 0){
echo '<img src="images/sampleAd.jpg" >';
}else{
$numb =1;
$flag =1;
echo '<div class="carousel-inner">';
while($fetched_data = mysql_fetch_array($query_exe)){
if($numb == 1){
if($flag == 1){
echo '<div class="active item">';
}else{
echo '<div class="item">';
}
echo '<a target="_blank" href="http://'.$fetched_data['url'].'" class="pull-left marginTop" ><img src="'.$fetched_data['image_url'].'"></a>';
}elseif($numb == 4){
echo '<a target="_blank" href="http://'.$fetched_data['url'].'" class="pull-left marginLast" ><img src="'.$fetched_data['image_url'].'"></a>';
$numb = 0;
echo '</div>';
}else{
echo '<a target="_blank" href="http://'.$fetched_data['url'].'" class="pull-left marginTop" ><img src="'.$fetched_data['image_url'].'"></a>';
}
$numb++;
$flag++;
}
if($numb > 1 && $numb < 4 ){
echo '</div>';
echo '</div>';
}elseif($numb == 1){
echo '</div>';
}elseif($numb == 4){
echo '</div>';
echo '</div>';
}
echo '<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>';
echo '<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>';
}
}
<div class="carousel-inner">
<?php
// Twitter Bootstrap Carousel.
$caseStudies = get_posts(array(
'numberposts' => 12,
'post_type' => 'case-study',
'meta_key' => 'show_case_study_home',
'meta_value' => 'yes',
'post_status' => 'publish',
));
$open = 0;
$close = 1;
$active = 0;
$didClose = false;
foreach($caseStudies as $caseStudy){
$image = get_field('image_home', $caseStudy->ID);
$header = get_field('header_home', $caseStudy->ID);
$intro = get_field('intro_home', $caseStudy->ID);
$activeCss = "";
if(($open)%4 == 0) {
if ($active == 0) $activeCss = " active";
echo '<div class="item'. $activeCss .'"><ul class="thumbnails">';
}
?>
<li class="span3">
<div class="case-study">
<img src="<?php echo $image['url']; ?>" alt="">
Read More
<h4><?php echo $header; ?></h4>
<p><?php echo $intro; ?></p>
</div>
</li>
<?
if(($close)%4 == 0) {
echo "<ul></div>";
$didClose = true; // Maybe the carousel has less than 4 items ...
}
$open++;
$close++;
$active++;
}
if($didClose == false) echo "</div></div>"; // If has less than 4 items in the carousel close the tags.
?>
</div><!--/.caroussel-inner -->
I solved it as :
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php $slider = $guest->getAllData("slider"); ?>
<?php foreach( $slider as $slide) : ?>
<li data-target="#myCarousel" data-slide-to="<?=$slide['id']; ?>" class=""></li>
<?php endforeach; ?>
</ol>
<div class="carousel-inner">
<?php foreach($slider as $slide) : ?>
<div class="item ">
<img src="images/slider/<?=$slide['img']; ?>" alt="Los Angeles">
</div>
<?php endforeach; ?>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
and add JS code:
$('.carousel-indicators>li:first-child').addClass('active');
$('.carousel-inner>div:first-child').addClass('active');

Categories