I have for each loop return an array
<?php foreach( get_uf_repeater( 'clients' ) as $document_files ): extract( $document_files ) ?>
<div class="ui-grid-a my-breakpoint">
<div class="ui-block-a"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
<div class="ui-block-b"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
</div>
<?php endforeach ?>
how can i jump to the next array for the second line "" instead of displaying the same of the first one, I assume I need nested loop but didnt know how do it exactly.
You could use array_chunk
<?php foreach (array_chunk(get_uf_repeater( 'clients' ), 2, true) as $array) { ?>
<div class="ui-grid-a my-breakpoint">
<?php foreach($array as $document_files) { ?>
<?php extract( $document_files ); ?>
<div class="ui-block-a"><img src="<?php echo $project_image ?>" title="Project image" /><div><?php echo $project_detailes ?></div></div>
<?php } ?>
</div>
<?php } ?>
Related
In WordPress I need to fetch the Author image of author who created post. I tried to get the profile image in this way, but it didn't work.
Here is the code I have written to get other author_meta_details.
<div class="row">
<div class="avatar col-md-3">
<picture class="avatar-circle">
<?php if($avatar = get_avatar(get_the_author_meta('ID')) !== FALSE): ?>
<img src="<?php echo $avatar; ?>" alt="">
<?php else: ?>
<img src="/wp-content/themes/lymited-child/images/avatar-image.png">
<?php endif; ?>
</picture>
</div>
<div class="author-details col-md-9">
<div class="author-name"><strong><?php echo get_the_author_meta('first_name'); ?></strong> - <?php echo get_the_author_meta('nickname'); ?>
</div>
<div class="author-summery"><?php echo get_the_author_meta('description'); ?>
</div>
</div>
</div>
Try this
<?php
$get_author_id = get_the_author_meta('ID');
$get_author_gravatar = get_avatar_url($get_author_id, array('size' => 450));
if(has_post_thumbnail()){
the_post_thumbnail();
} else {
echo '<img src="'.$get_author_gravatar.'" alt="'.get_the_title().'" />';
}
?>
OR Remove this if the condition and try
echo get_avatar( get_the_author_meta('ID') );
get_avatar() returns <img> element so you don't need to wrap it to img again
Also you need to wrap $avatar = ... to parenthesis as = priority is lower than !==
try to replace this
<?php if ( $avatar = get_avatar(get_the_author_meta('ID')) !== FALSE): ?>
<img src="<?php echo $avatar; ?>" alt="">
with this
<?php if ( ( $avatar = get_avatar(get_the_author_meta('ID')) ) !== FALSE): ?>
<?php echo $avatar; ?>
I have a problem looping my html tags. My goal is to enclose every 4 div with class "item" inside the class of "item-wrap". So far here's my code:
$speakers will return 8 rows so "item-wrap" class will be display twice with 4 "item" class inside
<?php
$speaker_ctr = 0;
if(count($speakers) > 0){
?>
<div class="item-wrap">
<?php foreach($speakers as $speaker){ ?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speaker['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speaker['name']; ?></h3>
<p class="no-mn"><?php echo $speaker['position']; ?></p>
<?php echo $speaker['company']; ?>
</div>
<?php } ?>
</div>
<?php
$speaker_ctr++;
}
?>
Try below code:-
<?php
if(count($speakers) > 0){
for($i=0;$i<=count($speakers);$i++){
if($i%4==0){
echo '<div class="item-wrap">';
}
?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speakers[$i]['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speakers[$i]['name']; ?></h3>
<p class="no-mn"><?php echo $speakers[$i]['position']; ?></p>
<?php echo $speakers[$i]['company']; ?>
</div>
<?php
if($i%4==0){
echo '</div>';
}
}
}
Try with -
$speaker_ctr = 0;
if(count($speakers) > 0){
?>
<div class="item-wrap">
<?php
foreach($speakers as $speaker){
$speaker_ctr++; // increment
?>
<div class="item"> <img class="lazy" data-lazy-src="<?php echo $speaker['imagepath']; ?>" />
<h5 class="txt-18 bold-font text-uppercase no-mbt mt-20"><?php echo $speaker['name']; ?></h3>
<p class="no-mn"><?php echo $speaker['position']; ?></p>
<?php echo $speaker['company']; ?>
</div>
<?php
// print if divisible by 4
// every 4th element
if($speaker_ctr%4 == 0 && $speaker_ctr != count($speakers)) {
echo "</div><div class='item-wrap'>";
}
}
?>
</div>
<?php
}
I'm doing some changes on an online store that is running OpenCart 2.2.
When browsing below the categories on the left side, there are 2 carousels that show different promotions. I want to modify it that before the second carousel there is a description text. To do that I should edit the template file. And here is where I get lost..
This is the code in the template file:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
From I see in the browser inspector and in my case, this code generates 2 banners with the ids - "banner0" and "banner1". The description text should go at the top of the code (right before ). If I use a simple paragraph, it gets displayed twice - above each banner.. How should I change it so that it will display the paragraph only above the second banner (id - banner1)?
I was thinking about an if-else statement, but I'm not sure if that will work... Could anyone help out a bit? My knowledge in PHP isn't much... :S
Thanks in advance!
Best regards,
Tsvetko Krastev
Then you need to know that this is the second time round the foreach loop. So change the foreach to include the index like this, and add a test inside the loop for $i == 1
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php //foreach ($banners as $banner) {
foreach ($banners as $i => $banner) {
?>
<div class="item">
<?php if ($banner['link']) {
if ( $i == 1 ) {
echo 'YOUR HTML CONTAINING SOME TEXT HERE';
}
?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
If I get the code right, $module is 0/1 (if id's get values banner0 and banner1), then this should work:
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<?php if ($module == "1") { ?>
<p>Your description</p>
<?php } ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
Thank you so very much guys!!! Tried both versions and they throw a error, but looking more carefully into the code and both your solutions, I came up with this:
<div id="desc<?php echo $module; ?>">
<?php if ($module == 1) { ?>
<p>Description</p>
<?php } ?>
<div id="banner<?php echo $module; ?>" class="owl-carousel">
<?php foreach ($banners as $banner) { ?>
<div class="item">
<?php if ($banner['link']) { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } else { ?>
<img src="<?php echo $banner['image']; ?>" alt="<?php echo $banner['title']; ?>" class="img-responsive" />
<?php } ?>
</div>
<?php } ?>
</div>
</div>
Thank you very much again for the quick responses and the help! :) :3
Best regards,
Tsvetko Krastev
I have a question:
I make a pre for news:
Array
(
[0] => Array
(
[name] => tag
[id] => 57
[title] => Article1,Article2,Article3
[views] => 53,54,58
[smallimage] => Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg
[date] => 2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40
)
)
How to create the foreach in view to show 1 title 1 views and 1 date...I create an foreach but show first all titles,all views,all dates;
My foreach:
<?php if ($news):?>
<?php foreach($news as $n):?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $n['smallimage']; ?>" alt="">
<?php echo $n['title'] ?><br/>
<?php echo $n['views'] ?>
<?php endforeach ?>
<?php endif; ?>
With this foreach I get: Article1,Article2,Article3 53,54,58...I want to get Article1 53,Article2 54, Article3 58....Help me please
Try this:
<?php
$news = array(array( "tag",
57,
"Article1,Article2,Article3",
"53,54,58",
"Koala-08.jpg,Jellyfish-08.jpg,Mountain-08.jpg",
"2014-05-07 09:21:58,2014-05-08 09:24:38,2014-05-08 14:36:40"
) );
?>
<?php if ($news){?>
<p>Tag:<?php echo $news[0][0]; ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo $news[0][4]; ?>" alt="">
<?php $views = explode(',',$news[0][3]);?>
<?php $article = explode(',',$news[0][2]);?>
<?php for($i=0;$i<count($article);$i++){
echo $article[$i].'-'.$views[$i];
} ?>
</div>
<?php }?>
I don't know where is "config_item('content_folder')" function result.Try this. And let me know whether it is works or not. Feel free to ask help.
<?php foreach($news as $n):?>
<?php $titles = explode(",", $n['title']); ?>
<?php $smallimages = explode(",", $n['smallimage']); ?>
<?php $views= explode(",", $n['views']); ?>
<p>Tag:<?php echo $n['name'] ?></p>
<div class="container">
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[0]; ?>" alt="">
<?php echo title[0] ?><br/>
<?php echo views[0] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[1]; ?>" alt="">
<?php echo title[1] ?><br/>
<?php echo views[1] ?>
<img src="<?php echo config_item('content_folder');?>news/small/<?php echo smallimages[2]; ?>" alt="">
<?php echo title[2] ?><br/>
<?php echo views[2] ?>
<?php endforeach ?>
<?php endif; ?>
I am assuming that for example 'Article1,Article2,Article3' is a string, so you would have to use:
$n['title'] = explode(',',$n['title'])
To make it into an array. Same goes for views, smallimage and date. Here's an example on how to apply that:
<p>Tag:<?php echo $n['name'] ?></p>
<div id="container">
<?php
$n['title'] = explode(',',$n['title'])
$n['views'] = explode(',',$n['views'])
$n['smallimage'] = explode(',',$n['smallimage'])
foreach ($n['title'] as $key=>$value) {
echo "<img src='" . config_item('content_folder') . "news/small" . $n['smallimage'][$key] . "' alt='' />";
echo $n['title'][$key] . "<br />";
echo $n['views'][$key]
}
?>
</div>
You would place this in the existing foreach-loop.
so i guess this is pretty easy for most of you, but i can't figure this out.
im trying to make the links dynamic eg: href="linkname(#1 or #2 etc)"
any ideas?
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(GENERATE CODE HERE)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>
Suppose below is what you need.
<?php if ($top_fundraisers && is_array($top_fundraisers)): ?>
<?php foreach ($top_fundraisers as $index => $fundraiser): ?>
<a title="" class="fancybox" href="linkname(#<?php echo $index + 1; ?>)">
<div class="top-fundraiser">
<div id="newo<?php print htmlentities($index + 1); ?>" class="top-fundraiser-image">
<img src="<?php
if($fundraiser['member_pic_medium']) {
print htmlentities($fundraiser['member_pic_medium']);
} else {
print $template_dir . '/images/portrait_placeholder.png';
}
?>"/>
</div>
</div>
</a>
<?php endforeach;?>
<?php endif; ?>