I have this code:
<?php if (types_render_field( "slider-imagine-1", array( 'raw' => true) ) !== "") : ?>
<div class="item">
<img src="<?php echo(types_render_field( 'slider-imagine-1', array( 'raw' => true) )); ?>" alt="">
</div>
<?php else :?> <?php endif; ?>
I want to repeat this code 10 times, and each time it repeats, the number from the variable "slider-imagine-1" increments by 1.
SOLVED:
<?php for($i = 1; $i < 11; $i++) {
// Increment variable by 1
$name = "slider-imagine-".$i;
// Extract incremented variable from WordPress
$variable = (types_render_field($name, array( "raw" => true) ));
// If variable is not empty, do this
if ($variable != "") {
echo '<div class="item">';
echo '<img src="'.$variable.'"></img>';
echo '</div>';
}
} ?>
Try something like that maybe
<?php
for($i = 0; $i < 10; $i++){
$name = "slider-imagine-".$i;
if (types_render_field($name, array( 'raw' => true) ) !== "") : ?>
<div class="item">
<img src="<?php echo(types_render_field($name, array( 'raw' => true) )); ?>" alt="">
...
You Miss to close php tag to rendre the HTML, you need something like that
// Php code in the for loop
if($name !='')
?>
<div>HTML <?php echo $name;?> Continue html<\div>
<?php
// Php code
// If you don't to close php tag
echo ' <div>'. $name .'<\div>';
That only an example on how to mix php and html, this will fit to your needs.
Related
I want to use $i into his PHP condition how can I do this? I have no idea how to echo into condition. plz see the code and if you can plz help me. thank you advanced.
<?php
for($i=1; $i<4; $i++) {
if($top_h_text_**I want to use $i here!**) {
?>
<li>
<?php
if($top_h_icon_**I want to use $i here!**) {
?>
<i class="<?php echo $top_h_icon_**I want to use $i here!**;?>"></i>
<?php
}
if($top_h_icon_**I want to use $i here!** == 'fa fa-envelope'){
?>
<span class="top_header_text"><?php echo $top_h_text_**I want to use $i here!**;?></span>
<?php
} else {
?>
<span class="top_header_text"><?php echo $top_h_text_**I want to use $i here!**;?></span>
<?php
}
?>
</li>
<?php
}
}
?>
To accomplish what you are trying to do, simply follow this example:
$top_h_text_**I want to use $i here!**
becomes
${"top_h_text_".$i}
So you basically take the string you want (which should be the name of an existing variable) and wrap it with ${}
If this is in fact WordPress, and your top_h_icon_ and top_h_text are in fact defined. This would be appropriate method for concatenation of your $i increment and the variables. This is how you join two parts to make a single string.
Note that with WordPress and outputting dynamic variables, you should escape them.
<?php
for ( $i = 1; $i < 4; $i++ ) {
if ( $top_h_text_ . $i ) {
?>
<li>
<?php
if ( $top_h_icon_ . $i ) {
?>
<i class="<?php echo esc_attr( $top_h_icon_ . $i ); ?>"></i>
<?php
}
if ( 'fa fa-envelope' === $top_h_icon_ . $i ) {
?>
<span class="top_header_text"><?php echo esc_attr( $top_h_text_ . $i ); ?></span>
<?php
} else {
?>
<span class="top_header_text"><?php echo esc_attr( $top_h_text_ . $i ); ?></span>
<?php
}
?>
</li>
<?php
}
}
I have a foreach loop giving out the results of a landscape gallery images.
Basically starting with
<?php foreach ( $images as $image ) : ?>
and then the regular div + anchor + image tags for the gallery images.
<?php if ( $image->description == "portrait" ) : ?>
<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box portrait" <?php echo $image->style ?> >
<div class="ngg-gallery-thumbnail" >
<a class="portrait-image" href="<?php echo $image->imageURL ?>" title=" " id="<?php echo $image->alttext ?>" rel="lightbox" <?php echo $image->thumbcode ?> >
<img title="<?php echo ' ' /* $image->alttext */ ?>" alt="<?php echo ' ' /* $image->alttext */ ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
<?php else: ?>
<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box landscape" <?php echo $image->style ?> >
<div class="ngg-gallery-thumbnail" >
<a class="landscape-image" href="<?php echo $image->imageURL ?>" title=" " id="<?php echo $image->alttext ?>" rel="lightbox" <?php echo $image->thumbcode ?> >
<img title="<?php echo ' ' /* $image->alttext */ ?>" alt="<?php echo ' ' /* $image->alttext */ ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
</a>
<?php endif; ?>
I want to have special case, where I have two portrait images side-by-side, filling the space of one landscape image.
What I was thinking about is to have a tag in the admin area which would trigger the break out of the foreach loop to have two images in one container div, and then it would continue the regular loop just with the container + image for the landscape images.
Is it possible in the case of foreach loop, to jump out of the loop, make a case with different settings, and then return to the regular looping method?
You can try something like this:
foreach($images as $key => $image) {
if($image has special tag) { // <--- PSEUDOCODE
echo "<div class='SPECIAL_CSS_CLASS'><img src='" . $img->path . "' /></div>";
}
}
Then in your CSS just do your Magic!
I would go ahead and do something like this:
//Function to get all the images based on a specific portrait_group
function getImagesPortraitGroup($group, $images) {
$result = array();
foreach ($images as $image) {
if (isset($image['portrait_group']) && $image['portrait_group'] == $group) {
$result[] = $image;
}
}
return $result;
}
//The images to show
$images = array(
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image1", "portrait_group" => 1],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image2", "portrait_group" => 1],
["url" => "http://lorempixel.com/800/200/", "caption" => "Lorem Image3"],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image4", "portrait_group" => 2],
["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image5", "portrait_group" => 2],
);
//Everytime we show a group, we track it by adding it to this array in order not to show it again
$addedGroups = array();
//Loop through all the images
for ($i = 0; $i < count($images); $i++) {
echo "<div>";
//If the image has a portrait_group that is not already shown, show it
if (isset($images[$i]['portrait_group']) && !in_array($images[$i]['portrait_group'], $addedGroups)) {
$groupImages = getImagesPortraitGroup($images[$i]['portrait_group'], $images);
foreach ($groupImages as $image) {
echo "<img src='{$image['url']}' title='{$image['caption']}' >";
}
//Save the group to the array in order not to show it again
$addedGroups[] = $images[$i]['portrait_group'];
} else {
echo "<img src='{$images[$i]['url']}' title='{$images[$i]['caption']}' >";
}
echo "</div>";
}
I managed to make the grouping by jQuery, this is the code
jQuery(".ngg-gallery-thumbnail-box.portrait").filter(":odd").each(function(){
$content = jQuery(this).find("a");
jQuery(this).prev().find(".ngg-gallery-thumbnail").append($content);
jQuery(this).remove();
});
So basically I am building a wordpress gallery, I need to have the images output into 2 rows, each row is 3 images across so essentially I am just splitting the output every 3..
I have that functionality working with a for loop using the modulus operator, what I need is that every 2 rows I need to have them wrapped in an outer HTML container
<div class="wrappy">
but I cant seem to get this quite right having worked on it most of yesterday, Im not that great at PHP but I understand enough to get by so any help at all would be great... I am using wordpress advanced custom fields for storing the images etc so you can ignore that part, the magic Im working on starts at
<div class="wrappy">
<?php
$rows = get_field('staff_slides', 'options');
$countmax = count($rows) - 1;
//echo $row_count;
$ender = "";
$mainEnder = "";
$outer_wrapper = "";
if( have_rows('staff_slides', 'options') ):
// loop through the rows of data
while ( have_rows('staff_slides', 'options') ) : the_row();
$image_id = get_sub_field('slide_image', 'options');
$staff_members_name = get_sub_field('staff_members_name', 'options');
$staff_members_position = get_sub_field('staff_members_position', 'options');
$staff_image = wp_get_attachment_image_src( $image_id , 'homepage-staff');
?>
<?php
echo '<div class="wrappy">';
echo '<div class="row">';
if( $ender != "script-ended" ) {
for( $i=0; $i <= $countmax; )
{
?>
<div class="staff-img c3">
<div class="staff-caption">
<h3><?php echo $rows[$i]['staff_members_name']; ?></h3>
<span></span>
<h4><?php echo $rows[$i]['staff_members_position']; ?></h4>
</div>
<img src="<?php echo wp_get_attachment_image_src( $rows[$i]['slide_image'], 'homepage-staff')[0]; ?>">
</div>
<?php
if( $i % 3 == 2 )
{
echo '</div><div class="row">';
}
if( $i == $countmax )
{
$ender = "script-ended";
}
if( $i == 6){
$outer_wrapper = "set";
}
$i++;
}
}
?>
<?php
if( $outer_wrapper == "set" ){
echo '</div><div class="wrappy">';
}
?>
You should use the array_chunk function to split the array in smaller arrays.
I'm not familiar with the ACF functions but you can do something like:
<div class="wrappy">
<?php
if( have_rows('staff_slides', 'options') ) :
$slides = array_chunk(get_field('staff_slides', 'options'), 3);
foreach ($slides as $slides_row) :
?>
<div class="row">
<?php foreach ($slides_row as $slide_element) : the_row(); ?>
<div class="staff-img c3">
<div class="staff-caption">
<h3><?php the_sub_field('staff_members_name', 'options'); ?></h3>
<span></span>
<h4><?php the_sub_field('staff_members_position', 'options'); ?></h4>
</div>
<img src="<?php echo wp_get_attachment_image_src( get_sub_field('slide_image', 'options'), 'homepage-staff')[0]; ?>">
</div>
<?php endforeach; ?>
</div>
<?php endforeach; else : ?>
No slides do to show
<?php endif; ?>
</div>
This way you create a multidimensional array, divided in chunks of 3 elements, and iterate throught it.
I'd like to set the following up as a loop (so that the pattern is repeated every 5 elements). I've tried to read up on it and I believe I need to set it up as an array, but I have no idea where to start.
Here's my code:
<div class="ccm-page-list">
<?php
$i= 0;i;
foreach ($pages as $page):
$i++;
$title = $th->entities($page->getCollectionName());
$url = $nh->getLinkToCollection($page);
$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
$target = empty($target) ? '_self' : $target;
$description = $page->getCollectionDescription();
$description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
$description = $th->entities($description);
$img = $page->getAttribute('thumbnail');
$thumb = $ih->getThumbnail($img, 550, 550, true);
?>
<a href="<?php echo $url ?>" target="<?php echo $target ?>"> <div class="col-sm-4 grid-item">
<div class="item-img-blog" >
<img src="<?php echo $thumb->src ?>" width="auto" height="100%" alt="" />
<div <?php
if($i == 1) {
?>class="item-class-1" <?php }
if($i == 3) {
?>class="item-class-2" <?php }
if($i == 2) {
?>class="item-class-3" <?php }
if($i == 4) {
?>class="item-class-3" <?php }
if($i == 5) {
?>class="item-class-1" <?php }
?>>
<div class="text-container">
<h1><?php echo $description ?></h1>
<p><?php echo $date = $page->getCollectionDatePublic('d/m/Y'); ?></p>
</div>
</div>
</div>
</div></a>
<?php endforeach; ?>
</div>
I would really appreciate any help! Thanks in advance!
$i= 0;
$classes = array(1 => 1, 2 => 3, 3 => 2, 4 => 3, 5 => 1);
foreach ($pages as $page) {
$i++;
.....
echo 'class="item-class-'.$classes[$i % (count($classes) + 1)];
.....
}
You don't need an extra loop, you can use the modulo operator (aka, the remainder of a division) and some basic math to cope with the fact that you want to cycle 1->5:
$i= 0; // note that I removed the "i;" at the end of this line!
foreach ($pages as $page):
$cyclici = ($i % 5) + 1;
// then, use $cyclici everywhere you want to see that 1-5 1-5 pattern
...
if($cyclici == 1) {
...
// and increment $i at the *end* of the loop instead of the start.
$i++
endforeach;
EDITed to clarify the relationship between $i and $cyclici - $i is still incremented for each iteration of the loop, $cyclici is derived from that incrementing value to obtain the desired 1 2 3 4 5 1 2 3 4 5 ... sequence.
UPDATE:
Thanks to everybody's wonderful input, I now have the code working properly in the main page. Still can't figure out why that same code isn't functioning in the widget. it's pulling down the title and permalink, so I know it's getting the data, but the custom values are returning as zero
I am making a wordpress page in which the group using the page is accepting challenges from viewers.
The challenges page is a special template that shows a percentage bar of how complete the challenge is.
the bar was working OK, with a small glitch in chrome, but the client wanted 'pending' for challenges that had 0% completeion and 'completed' for 100% instead of the graph.
I've put this code into both the page and a widget with strange results. on the page, only the percentage bar ever shows, but on the widget, all challenges are listed as 'pending'
my code for the page is as follows:
<?php
/**
* Template Name: Challenges Page
*/
?>
<?php get_header(); ?>
<!-- content -->
<div id="content">
<?php query_posts( array( 'post_type' => 'challenges', 'posts_per_page' => -1 ) ); ?>
<?php while (have_posts()) : the_post(); $more = 0; ?>
<div class="post archive">
<div class="post-comments"><?php comments_popup_link('0', '1', '%'); ?></div>
<h3><?php the_title(); ?></h3>
<?php
$target = get_post_meta($post->ID, 'target', true);
$complete = get_post_meta($post->ID, 'complete', true);
$percentage = $complete / $target;
$percentage = round($percentage * 100);
$whatsleft = 100-$percentage;
if($whatsleft < 0) $whatsleft=0;
echo "<table width='250' border='0' cellpadding='0' cellspacing='0'><tr>";
if($complete == $target) {
echo "<td><img src='http://www.smokeyvstheworld.com/wp-content/uploads/2012/05/completed.gif' style='width:200>px;height:24px;'></td>";
} else if ($complete == 0) {
echo "<td><img src='http://www.smokeyvstheworld.com/wp-content/uploads/2012/05/pending.gif' style='width:200>px;height:24px;'></td>";
} else {
echo "<td><img src='http://www.smokeyvstheworld.com/wp-content/themes/spectre/images/brown/grnbar.jpg' style='width:". $percentage ."px;height:12px;'></td><td><img src='http://www.smokeyvstheworld.com/wp-content/themes/spectre/images/brown/grybar.jpg' style='width:". $whatsleft ."px;height:12px;'></td>";
}
echo "</tr><tr><td colspan='2'><div align='right'>". $complete ." of ". $target ." completed</div></td></tr></table>";
?>
<div class="post-date"><?php the_time('l, F jS, Y') ?></div>
<?php if (get_post_meta($post->ID, 'post_image_value', true)) { ?><div class="post-tnail"><?php if (get_post_meta($post->ID, 'post_image_value', true) && $mb_resize == 0) { ?><img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo get_post_meta($post->ID, "post_image_value", $single = true); ?>&w=98&h=98&zc=1&q=95" alt="<?php the_title(); ?>" /><?php } else if (get_post_meta($post->ID, 'post_image_value', true) && $mb_resize == 1) { ?><img src="<?php bloginfo('home'); ?><?php echo get_post_meta($post->ID, "post_image_value", $single = true); ?>" alt="<?php the_title(); ?>" /><?php } ?></div><?php } ?>
<?php the_excerpt() ?>
<p>Continue reading...</p>
</div>
<?php endwhile; ?>
</div>
<div id="sidebar"><?php get_sidebar(); ?></div>
<?php get_footer(); ?>
I'm confused as to WHAT I'm doing wrong, and why the same code works in one instance and not the other... I've tried switching from brackets to colons and back, etc.
width:200>px;height:24px; => width:200px;height:24px;
Try
$target = (int) get_post_meta($post->ID, 'target', true);
$complete = (int) get_post_meta($post->ID, 'complete', true);
Try changing both the == to === so:
if($complete === $target) {
and
} else if ($complete === 0) {
Edit:
Thinking about it, the $complete value is probably a string so if above doesn't work change that line to
} else if ($complete === "0") {