I am using the below code on a WP site.
<?php
$images = array();
$images[] = $profile_user->banner_image_1;
$images[] = $profile_user->banner_image_2;
$images[] = $profile_user->banner_image_3;
$images[] = $profile_user->banner_image_4;
if(!empty($images[0]) || !empty($images[1])|| !empty($images[2])|| !empty($images[3])){
?>
<?php echo '<div class="slider2">'; ?>
<?php foreach($images as $img): ?>
<?php if(!empty($img)): ?>
<div>
<img src="
<?php
$image_id = $img;
$post_image_data = wp_get_attachment_image_src( $image_id, $size='profile_banner_img' );
echo $post_image_data[0];
?>" />
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php echo '</div>'; ?>
<?php } ?>
What i need to do is use a "IF" or "array" statement or something that will see if only "banner_image_1" has been filled by user and display a different code.
In other words if banner_image_1 returns information but 2,3,4 dont i need remove the
<div class="slider2"> and the </div>
Try this:
<?php
$images = array();
for($i=1; $i<=4; $i++) {
if(!empty($profile_user->{"banner_image_$i"})) {
$images[] = $profile_user->{"banner_image_$i"};
}
}
$validPics = count($images);
if($validPics > 0) {
if($validPics > 1) echo '<div class="slider2">';
foreach($images as $img_id) {
$img_src = wp_get_attachment_image_src( $img_id, $size='profile_banner_img' );
echo '<div><img src=\"' . $img_src[0] . '" /></div>';
}
if($validPics > 1) echo '</div>';
}
Related
I want to insert php code in between tabby tabs shortcodes.
I am using a plugin tabby tab for tab view and have added this code in my theme template:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
[tabbyending]'); ?>
I want to use a custom fields gallery under images tab using code like this:
<?php echo do_shortcode('[tabby title="Gallary Name"]
name content
[tabby title="Images"]
<?php
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ): ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul></div>
<?php endif; ?>
[tabbyending]'); ?>
This code is not working, it's showing a blank page. How can I fix this?
Tabby uses a global variable to track what's going on, so I think either one of these will work. The first one is a little more straightforward, but the second one will definitely work.
Option 1: output everything in order:
echo do_shortcode( '[tabby title="Gallery Name"] name content' );
echo do_shortcode( '[tabby title="Images"]' );
// your php code as-is
$i = 0;
$images = get_field('vil_pics');
if( $images ): ?>
<div>
<ul>
<?php foreach( $images as $image ):
$i++ ?>
<li<?php if ( $i % 3 == 0 ) echo ' class="break"' ?>>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a><p>.</p>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif;
echo do_shortcode( '[tabbyending]' );
or Option 2: save everything to a variable and output it all at once:
$output = '';
$output .= '[tabby title="Gallery Name"] name content';
$output .= '[tabby title="Images"]';
$i = 0;
$images = get_field('vil_pics');
if ( $images ) {
$output .= '<div><ul>';
foreach( $images as $image ) {
$i++;
$li_class = ( $i % 3 == 0 ) ? ' class="break"' : '';
$output .= '<li' . $li_class . '>';
$output .= '<a href="' . $image['url'] . '">';
$output .= '<img src="' . $image['sizes']['thumbnail'] . '" alt="' . $image['alt'] . '" />';
$output .= '</a><p>.</p></li>';
}
$output .= '</div></ul>';
}
$output .= '[tabbyending]';
echo do_shortcode( $output );
Note that I didn't see anything increasing $i so I added that. Everything else is as-is.
I have two working codes that do two things to the images in my word-press site. However they don't work when I use them both as is. I can't seem to combine them and keep the functionality of both.
Outputs all the images in my word-press post with the last one wrapped in #last-img
<?php
preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
for( $i=0; isset($images[1]) && $i < count($images[1]); $i++ ) {
if ($i == end(array_keys($images[1]))) {
echo sprintf('<div id="last-img">%s</div>', $images[1][$i]);
continue;
}
echo $images[1][$i];
}
?>
puts the last image as the background of #last-img
<?php
preg_match_all('/src="([^"]*)"/i', get_the_content(), $images);
?>
<div id="last-img" style="background:url(<?php echo $images[1][count($images[1])-1] )"> ... </div>
I want to display all the images and have the last image as the background of #last-img
You can do it like below:-
<?php
preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
preg_match_all('/src="([^"]*)"/i', get_the_content(), $images1);
$count = count($images[1]);
$count1 = count($images1[1]);
foreach($images[1] as $k=> $img){
if($k == $count-1){
$format = '<div id="last-img" style="background:url(%s)"> ... </div>'; // Or $format = "<div id='last-img' style='background:url(%s)'> ... </div>";
$image = $images1[1][$count1-1];
echo sprintf($format,$image);
}else{
echo $img;
}
}
?>
Or
<?php
preg_match_all('/(<img [^>]*>)/', get_the_content(), $images);
preg_match_all('/src="([^"]*)"/i', get_the_content(), $images1);
$count = count($images[1]);
$count1 = count($images1[1]);
foreach($images[1] as $k=> $img){
echo $img;
}
$format = '<div id="last-img" style="background:url(%s)"> ... </div>'; // Or $format = "<div id='last-img' style='background:url(%s)'> ... </div>";
$image = $images1[1][$count1-1];
echo sprintf($format,$image);
?>
Here's my problem, I have to display array values (description) under pictures like this :
Instead it appears like this
I'm working on this problem, this is my current code :
if (preg_match_all('/<div id="description" class="description">([^<]*)<\/div>/', $content, $match)) {
for( $i = 0; $i < count($match[0]); $i = $i+1 ) {
$description[] = $match[0][$i];
}
}
$attachments =& get_children($args);
$arrayMatches = array();
if ($attachments) {
foreach(array_chunk($attachments, 2) as $img) {
echo '<div class="two_cols">';
foreach($img as $attachment) {
foreach($attachment as $attachment_key => $attachment_value) {
$imageID = $attachment->ID;
$imageTitle = $attachment->post_title;
$imagearray = wp_get_attachment_image_src($attachment_value, $size, false);
$imageAlt = get_post_meta($imageID, '_wp_attachment_image_alt', true);
$imageURI = $imagearray[0]; // 0 is the URI
$imageWidth = $imagearray[1]; // 1 is the width
$imageHeight = $imagearray[2]; // 2 is the height
?>
<div class="col_1_2">
<div id="attachment_<?php echo $imageID; ?>" class="wp-caption alignnone" style="width: 356px;">
<a rel="lightbox-0" href="<?php echo $imageURI; ?>"><img class="wp-image-<?php echo $imageID; ?> size-full" title="<?php echo $imageTitle; ?>" src="<?php echo $imageURI; ?>" alt="<?php echo $imageAlt; ?>" width="456" height="304" /></a>
<p class="wp-caption-text"><?php echo $imageTitle; ?></p>
<?php
$arrayMatches[] = $match[0][$j];
?>
</div>
</div>
<?php
break;
}
$j++;
}
foreach(array_chunk($arrayMatches, 2) as $desc) {
echo '<div class="description">';
foreach($desc as $item) {
echo $item;
}
echo '</div>';
}
echo '</div>';
}
}
I try so many solutions but no one was the good one.
Thanks for the help ;)
Try:
foreach(array_chunk($arrayMatches, 2) as $k => $desc) {
if($k < 1) continue;
echo '<div class="description">';
foreach($desc as $item) {
echo $item;
}
echo '</div>';
}
I'm not that good at php. What i have is a list of items looped with endforeach. What I want is that the first loop will have a class of col-lg-12 and from the second one that class will become col-lg-6. How can I achive that?
Here's the code:
<?php $firstLoop = true;
foreach( $network_value as $key => $value ){
if( $firstLoop ){
echo '<div class="col-lg-12">';
}
echo '<div class="col-lg-6">';
$firstLoop = false;
} ?>
^This is the code that I've tried, but it's not working how i wanted.
<?php if ($img): ?>
<img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
<?php endif; ?>
<h4>
<div class="circle"><?php echo $datePublic = date('d M', strtotime($page->getCollectionDatePublic())); ?></div>
<br>
<a class="blogTitle" href="<?php echo $url ?>" target="<?php echo $target ?>"><?php echo $title ?></a></h4>
<h6>Posted by <?php echo $author; ?></h6>
<br>
<p><?php echo $description ?></p>
</div>
<?php endforeach; ?>
The most simple way to do it is to add a counter and check if it is the first value in the counter.
<?php
$counter = 0;
foreach( $network_value as $key => $value )
{
if($counter == 0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
$counter++;
}
?>
Also I want to add that there are two ways of using foreach and if-statements, but you are trying to mix them, which is wrong.
The first method is using brackets "{" and "}":
foreach($users as $user) {
// do things for each user
echo $user->name; // Example of writing out users name
}
And if-statement:
if(true) {
// do something
}
The second method is using "foreach(statement):" and "endforeach;"
foreach($users as $user):
// do things for each user
echo $user->name; // Example of writing out users name
endforeach;
And if-statement:
if(true):
// do something
endif;
Edited:
In your case you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} if($key==1){
echo '<div class="col-lg-6">';
}
endforeach; ?>
Or you can use:
<?php
foreach ($pages as $key=>$page):
if($key==0){
echo '<div class="col-lg-12">';
} else {
echo '<div class="col-lg-6">';
}
endforeach; ?>
OLD:
foreach($array as $key=>$row){
if($key==0){
echo '<div class="col-lg-12"></div>';
}
if($key==5){
echo '<div class="col-lg-6"></div>';
}
// OR try use %
if($key%2 == 0){
echo '<div class="col-lg-12"></div>';
} else {
echo '<div class="col-lg-12"></div>';
}
I have a code that is for a gallery. The images in this gallery opens in a fancybox. I don't want to link this images to a fancybox but i want link this images to one specific page. This is the code that must be customized:
<?php
global $sr_prefix;
$gallery = get_post_meta($post->ID, $sr_prefix.'_medias', true);
$postid = $post->ID;
$maintitle = 'h2'; $subtitle = 'h5';
if (get_option($sr_prefix.'_blogentrieslayout') == 'masonry' || get_option($sr_prefix.'_blogentrieslayout') == 'bloglist') { $maintitle = 'h4'; $subtitle = 'h6'; }
if( empty($gallery) || get_option($sr_prefix.'_blogentriesdisplay') == 'featuredimage' || get_option($sr_prefix.'_blogpostgallerydisplay') == "list" ) {
?>
<?php if(has_post_thumbnail()) { ?>
<div class="entry-thumb entry-media blog-media">
<div class="imgoverlay">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('single-blog-image'); ?>
<div class="overlay"><span class="overlaycolor"></span><span class="overlayinfo">
<?php echo '<'.$maintitle.'>'; ?><strong><?php the_title(); ?></strong><?php echo '</'.$maintitle.'>'; ?>
<?php if (!get_option($sr_prefix.'_blogpostsdisabledate')) { ?>
<?php echo '<'.$subtitle.'>'; ?><?php the_time(get_option('date_format')); ?><?php echo '</'.$subtitle.'>'; ?>
<?php } ?>
</span></div>
</a>
</div>
</div> <!-- END .entry-media -->
<?php } ?>
<?php
} else {
$medias = explode('|||', $gallery);
$output_medias = '';
foreach ($medias as $media) {
$object = explode('~~', $media);
$type = $object[0];
$val = $object[1];
$output_medias .= "<li>";
if ($type == 'image') {
$image = wp_get_attachment_image_src($val, 'single-blog-image'); $image = $image[0];
$fancyimage = wp_get_attachment_image_src($val, 'full'); $fancyimage = $fancyimage[0];
$thisimage = '<img src="'.$image.'" alt="'.get_the_title($image[1]).'"/>';
if(get_option($sr_prefix.'_blogpostsdisablefancybox') !== "on") {
$output_medias .= '<div class="imgoverlay">'.$thisimage.'<div class="overlay"><span class="overlaycolor"></span></div></div>';
} else {
$output_medias .= $thisimage;
}
} else {
$output_medias .= '<div class="embeddedvideo">'.$val.'</div>';
}
$output_medias .= "</li>";
}
?>
<?php if(get_option($sr_prefix.'_blogpostgallerydisplay') !== "list" ) { ?>
<div class="entry-media blog-media">
<div id="slider-<?php echo $postid; ?>" class="flexslider-container post-slider">
<div class="flexslider">
<ul class="slides">
<?php echo $output_medias; ?>
</ul>
</div>
</div>
</div> <!-- END .entry-media -->
<?php } else { ?>
<div class="entry-media blog-media">
<?php the_post_thumbnail('single-blog-image'); ?>
</div> <!-- END .entry-media -->
<?php } ?>
<?php } ?>
Seems like you are using Wordpress.
First of all you have to look at this part
if ($type == 'image') {
$image = wp_get_attachment_image_src($val, 'single-blog-image'); $image = $image[0];
$fancyimage = wp_get_attachment_image_src($val, 'full'); $fancyimage = $fancyimage[0];
$thisimage = '<img src="'.$image.'" alt="'.get_the_title($image[1]).'"/>';
if(get_option($sr_prefix.'_blogpostsdisablefancybox') !== "on") {
$output_medias .= '<div class="imgoverlay">'.$thisimage.'<div class="overlay"><span class="overlaycolor"></span></div></div>';
You have if(get_option($sr_prefix.'_blogpostsdisablefancybox') !== "on") option, which i suppose can disable fancybox, maybe it's somewhere in WP admin panel.
Anyway, if I remember correctly, fancybox links it's events to particular class, in this case it's openfancybox class. You can remove this class and disable fancybox.
$output_medias .=
'<div class="imgoverlay">
<a href="'.$fancyimage.'" class="openfancybox" rel="gallery'.get_the_ID().'" title="'.get_the_title($image[1]).'">'.$thisimage.'
<div class="overlay"><span class="overlaycolor">
</span>
</div>
</a>
</div>';
Then you can just change $fancyimage variable to you desired URL.