I have found some PHP that I am trying to adapt onto my page but can not seem to get it to work. Here is the code:
<?php
$pagetitle = "Test Gallery";
$dirname = "img/folder_1/";
$images = glob($dirname."*.jpg");
?>
<?php $i=0; foreach($images as $image): ?>
<?php if($i%2 === 0) echo '<div class="row">' ?>
<div class="col-sm-6">
<img class="lazy store-img" src="IMAGE_SOURCE" alt="" title="">
<h3>IMAGE_NAME</h3>
<button href="#" class="btn btn-primary">Purchase Image</button>
</div>
<?php if($i%2 === 0) echo '</div>' ?>
<?php $i++; endforeach ?>
I am trying to get the images to be pulled from a directory and each image is wrapped in a .col-sm-6 div then each 2 are wrapped in a .row div, then if there is an odd number of the files the last one would just be one .col-sm-6 div on its own. Below is the structure that I am trying to achieve:
<div class="row">
<div class="col-sm-6">
<img class="lazy store-img" src="IMAGE_SOURCE" alt="" title="">
<h3>IMAGE_NAME</h3>
<button href="#" class="btn btn-primary">Purchase Image</button>
</div>
<div class="col-sm-6">
<img class="lazy store-img" src="IMAGE_SOURCE" alt="" title="">
<h3>IMAGE_NAME</h3>
<button href="#" class="btn btn-primary">Purchase Image</button>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<img class="lazy store-img" src="IMAGE_SOURCE" alt="" title="">
<h3>IMAGE_NAME</h3>
<button href="#" class="btn btn-primary">Purchase Image</button>
</div>
</div>
The IMAGE_NAME I would add something to pull through the imported filename so it can be displayed on the page and the IMAGE_SOURCE would be the images URL so for now please ignore those bits of text as I don't think it will affect this?
I am new to PHP and just can't seem to find why nothing is showing on the page unless this code is not suited to my needs?
P.s. if any of my syntax's are incorrect I apologies in advance.
$images will be filled by your golb
So basically you just edit the line
<?php if($i%2 === 0) echo '</div>' ?>
To
<?php if($i%2 === 1) echo '</div>' ?>
Here a more readable example:
<?php
$images = [
'https://via.placeholder.com/10x50','https://via.placeholder.com/20x50','https://via.placeholder.com/30x50','https://via.placeholder.com/40x50',
'https://via.placeholder.com/50x50','https://via.placeholder.com/60x50','https://via.placeholder.com/70x50',
];
?>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css"/>
<?php
$cols = 2; // dividable by 12 please
for( $i = 0; $i < count($images) && $image = $images[$i]; $i++) {
if( $i%$cols == 0 ) { echo '<div class="row">'; }
echo '<div class="col-sm-' . 12/$cols . '">';
echo '<img class="lazy store-img" src="' . $image . '" alt="" title="">';
echo '<h3>IMAGE_NAME</h3>';
echo '<button href="#" class="btn btn-primary">Purchase Image</button>';
echo '</div>';
if( $i%$cols == $cols-1 ) { echo '</div>'; } /* here comes the magic :) */
}
if( count( $images )%$cols > 0 ) {
echo '</div>';
}
?>
Related
I need to write filter data of my custom post type in custom function placed in functions.php:
The majority are corrected, but I cannot process these lines:
<div class="cpt-studies-block-image picture">
<?php $thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien'); ?>
<img class="img" src="<?php print($thumb_img[0]); ?>">
</div>
and:
<a href="<?php the_permalink()?>" class="cpt-studies-block-link link-read-more"> <?php
include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
?>
<?php if (get_field('button_text')):
the_field('button_text');
else : echo __('More', 'dw');?>
<?php endif;?>
</a>
Code in folder for writing Custom Post Type data:
if($query->have_posts()):
while($query->have_posts()) : $query->the_post();?>
<div class="col-md-6">
<div class="cpt-studies-block">
<div class="row">
<div class="col-md-6">
<a class="zoom-picture-hover" href="<?php the_permalink()?>">
<div class="cpt-studies-block-image picture">
<?php $thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien'); ?>
<img class="img" src="<?php print($thumb_img[0]); ?>">
</div>
</a>
</div>
<div class="col-md-6">
<span><?php the_field('date')?></span>
<h3>
<a href="<?php the_permalink()?>">
<?php the_title()?>
</a>
</h3>
<p class="cpt-studies-block-text"><?php the_field('text')?></p>
<a href="<?php the_permalink()?>" class="cpt-studies-block-link link-read-more"> <?php
include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
?>
<?php if (get_field('button_text')):
the_field('button_text');
else : echo __('More', 'dw');?>
<?php endif;?>
</a>
</div>
</div>
</div>
</div>
<?php endwhile;
endif;
?>
Code in Custom function in functions.php:
if(have_posts($wp_query))
{
while(have_posts($wp_query))
{
the_post();
echo '<div class="col-md-6">'
.'<div class="cpt-studies-block">'.
'<div class="row">'.
'<div class="col-md-6">'
.'<a class="zoom-picture-hover" href="'.get_permalink().'">'
.'<div class="cpt-studies-block-image picture">'
.$thumb_img=wp_get_attachment_image_src(get_field('image'),'custom-crop-studien').
'<img class="img" src=".print($thumb_img[0]).">'
.'</div>'
.'</a>'
.'</div>'
.'<div class="col-md-6">'
.'<span>'.get_field('date').'</span>'
.'<h3>'
.'<a href="'.get_permalink().'">'
.get_the_title().
'</a>'
.'</h3>'
.'<p class="cpt-studies-block-text">'.get_field('text').'</p>'.
'<a href="'.get_permalink().'" class="cpt-studies-block-link link-read-more">'
include get_stylesheet_directory() . '/img/svg/icon_arrow.svg';
'</a>'.
'</div>'.
'</div>'.
'</div>'.
'</div>';
}
}
Please can someone give me advice, or do I need to change method on writing these lines?
You got a syntax error:
.'<div class="cpt-studies-block-image picture">' // Add semicolon.
.$thumb_img = wp_get_attachment_image_src(
get_field('image'),'custom-crop-studien'). // Assignement can't be concatenated
// and missing parenthesis
'<img class="img" src=".print($thumb_img[0]).">' // Resume echo.
So:
// Ommited
.'<div class="cpt-studies-block-image picture">';
$thumb_img = wp_get_attachment_image_src(
get_field('image'), 'custom-crop-studien'));
echo '<img class="img" src=".print($thumb_img[0]).">'
Or better yet, use templates (as I explained in this other question of yours)
I am trying to display images on a page using the bootstrap grid system, where their names are dynamically created by taking it from a database. For example I have this database, and I want to use the imageID in the src name of each image. Is there any cleaner way to do it without having to manually add a new div etc for each image?
PHP Code for getting image id:
<?php
//dynamically render images
include "../storescripts/connect-mysql.php";
$sql = mysql_query("SELECT * FROM imageGallery ORDER BY dateAdded ASC");
$images = array();
$imageCount = mysql_num_rows($sql); //Count the amount of products
if($imageCount > 0){
while($row = mysql_fetch_array($sql)){
$image = $row['imageID'];
$images[] = $image;
}
}else{
$image_gallery = "<h2>You have no images in the database</h2>";
}
?>
My HTML for displaying the images:
<div class="col-md-12">
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $images[0] ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $images[1] ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $images[2] ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $images[3] ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
</div>
See the image source for how I used the PHP.
As you have images available within $images array. So make a foreach loop inside html div.
<div class="col-md-12">
<?php foreach ($images as $image): ?>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $image; ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<?php endforeach; ?>
</div>
Update
This is for if you want only four divs inside a parent div over and over again.
<?php
$i = 0;
foreach ($images as $image):
if ($i % 4 == 0) {
echo '<div class="col-md-12">';
}
echo '<div class="col-md-3 galleryImg">';
echo '<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/' . $image .'" alt="Jedi Cycle Sport Gallery Image">';
echo '</div><!-- outputs child div -->';
$i++;
if ($i % 4 == 0) {
echo '</div> <!-- outputs parent div -->';
}
endforeach;
if ($i % 4 != 0) {
echo '</div> <!-- outputs parent div-->';
}
Simply use the foreach. Use the code as follows
<div class="col-md-12">
<?php foreach($images as $image) {?>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $image ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<?php } ?>
</div>
Instead of manually adding divs, you can simply use a foreach loop like this:
<div class="col-md-12">
<?php
foreach($images as $image){
?>
<div class="col-md-3 galleryImg">
<img onclick="myfunction(this)" class="galleryImage" src="../img/gallery/<?php echo $image; ?>.png" alt="Jedi Cycle Sport Gallery Image">
</div>
<?php
}
?>
</div>
Ignore the horrible code and formating, it's what I have to work with.
<div class="stories-pictures"><div class="inside">
<?php for ($j=0; $j<2; $j++) foreach ($stories as $post) { /* can be empty field as well*/
setup_postdata($post);
if ($post->ID==$mainID && $j==0) {
$link = get_permalink($productID);
$title = "Back to <br/>".get_the_title($productID);
$img = "<img class='backbg' width='316' height='234' src=".get_template_directory_uri()."/images/bg.png";
} else if ($post->ID!=$mainID && $j==1) {
$link = get_the_permalink()."?e=$productID";
$title = get_the_title();
$img = get_the_post_thumbnail(null,'story-thumb');
} else continue;
?>
<a href="<?php echo $link; ?>" class="storiespicture">
<span>
<span><em> <span class="title"><?php echo $title; ?></span></em></span>
</span>
<?php echo $img; ?>
<div class="storieoverlay">
</div> <!-- .storieoverlay -->
</a> <?php } ?>
</div></div>
Here's what's outputted:
<a href="http://hidden/" class="storiespicture">
<span>
<span> <em> <span class="title" style="background-color: rgba(144, 137, 213, 0.901961);">hidden</span></em> </span>
</span>
<img width="316" height="234" src="http://hidden/hidden.jpg" class="attachment-story-thumb wp-post-image" alt="hidden"> </a>
I'm trying to get the .storieoverlay class to be under the img.
I removed some possibly sensitive content, hence where hidden is shown.
It's because you don't close your image tag:
$img = "<img class='backbg' width='316' height='234' src=".get_template_directory_uri()."/images/bg.png";
Should be
$img = "<img class='backbg' width='316' height='234' src='".get_template_directory_uri()."/images/bg.png'>";
I've fixed the issue now.
It was a plugin outputting the mark up on this certain page, everything said here has been useful though, so thanks.
//In PHP fetch content and truncate the string or variable, till 3rd breakline then display
//when i use this method the entire div deallocated and the its not displaying properly...hope the fetch content contain some image tags too // ...
<div class="mid-blks-cont">
<!-- Block1 -->
<div class="mid-block-1 boxgrid caption">
<?php
foreach ($querypost as $row) {
$content = $row->post_content;
$pcontent_overview = (strlen($content) > 300) ? substr($content,0,300).'... Read More' : $content;
if($img == "No Image Uploaded" ) {
?>
<img alt="" src="<?php echo base_url(); ?>assets/img/samples/sample1.jpg"style="width: 391px; height:231px"/>
<?php } else { ?>
<img alt="" src="<?php echo base_url(); ?>uploads/<?php echo $row->post_media ;?>" style="width: 391px; height:231px" />
<?php }?>
<h4 class="cat-label cat-label2"><?php echo $row->category;?></h4>
<div class="cover boxcaption">
<h3><?php echo $row->post_title;?><span class="topic-icn"><?php echo $row->comment_count;?></span></h3>
<p> <?php echo $pcontent_overview;?>....</p>
MORE <i class="fa fa-angle-double-right"></i>
</div>
<?php } ?>
</div>
</div>
I have box for dynamic images and desc slider using nivoslider. Now I have big problem -> my PHP code is:
<div id="slider" class="nivoSlider">
<?php
$featured = mysql_query("SELECT * FROM featured WHERE order > 0 ORDER BY order ASC");
$count_featured = mysql_num_rows($featured);
if ($count_featured < 1) { echo "error data" }
while ($swcms = mysql_fetch_assoc($featured)) { ?>
<img width="500" height="170" src="<?php echo "$swcms[image]"; ?>" title="#<?PHP echo "$swcms[id]"; ?>" alt="" border="" />
<div id="<?PHP echo "$swcms[id]"; ?>" class="nivo-html-caption"><?PHP echo "$swcms[desc]"; ?> </div>
<?php $c++; }?>
</div>
This Worked 100% But in firebug I see many GET undefined request after each slide:
I found the problem; nivoslider worked with this method for show images/desc (caption):
<div id="slider" class="nivoSlider">
<img src="..." title="#id" />
</div>
<div id="id" class="nivo-html-caption"></div>
And my PHP loop is:
<div id="slider" class="nivoSlider">
<img src="..." title="#id" />
<div id="id" class="nivo-html-caption"></div>
</div>
How do I fix this PHP code for nivoslider loop?
<div id="slider" class="nivoSlider">
<?php
$featured = mysql_query("SELECT * FROM featured WHERE order > 0 ORDER BY order ASC");
$count_featured = mysql_num_rows($featured);
$captions = '';
if ($count_featured < 1) { echo "error data" }
while ($swcms = mysql_fetch_assoc($featured)) { ?>
<img width="500" height="170" src="<?php echo "$swcms[image]"; ?>" title="#<?PHP echo "$swcms[id]"; ?>" alt="" border="" />
<?php $captions .= '<div id="' . $swcms[id] .'" class="nivo-html-caption"' . $swcms[desc] .'</div>'; ?>
<?php $c++; }?>
</div>
<?php echo $captions; ?>