Loop after all elements in wordpress custom field - php

I'm using Types Fields form Toolset Plugin for Wordpress, and I've got simple custom post type with few checkoboxes. I'm rendering checked values by:
<?php
$partners = types_render_field("partners", array('normal' => true));
?>
<?php echo($partners); ?>
output looks like this:
value1, value2, value3
The question is how can I loop after all elements in $partners, to manage this output:
<div class="value1"></div>
<div class="value2"></div>
<div class="value3"></div>

Explode your string then use php foreach short syntax :
$partners = explode(', ', $partners);
<?php foreach ($partners as $partner) : ?>
<div class="<?= $parner ?>"></div>
<?php endforeach; ?>

Final working version:
<?php $partners = explode(', ', $partners); ?>
<?php foreach ($partners as $partner) : ?>
<div class="<?php echo($partner) ?>"></div>
<?php endforeach; ?>

Related

Wordpress: Advances Custom Field multiple select

I’ve been wracking my brain tonight trying to figure out how to display multiple labels from a select field.
First I tried it with a single field and that is working. Now I want to make use of multiple fields but can't get it working. Can someone help me?
Thanks!
<?php if ( get_field( 'locationCompany', $joboffer->ID) ) : ?>
<span class="company-compact"><? echo (get_field('locationCompany', $joboffer->ID))?></span>
<?php endif ?>
Make sure that you enable the Select multiple values? in the ACF settings. Here's your code:
<?php
$locationcompany = get_field( 'locationCompany', $joboffer->ID);
if ( $locationcompany ) :
foreach ($locationcompany as $value): ?>
<span class="company-compact"><?php echo $value; ?></span>
<?php
endforeach;
endif;
?>
You can use the same approach as above but you need to wrap the whole div with the foreach:
<?php
$locationcompany = get_field( 'locationCompany', $joboffer->ID);
if ( $locationcompany ) :
foreach ($locationcompany as $value): ?>
<div id="job-offer-filter"
class="col-xl-4 col-lg-6 col-md-6 col-sm-12 col-xs-12 height-130 job-offer-card all
<? echo (get_field('company', $joboffer->ID))?>
<? echo ($value) /*** HERE ***/?>
<? echo (get_field('workingtime', $joboffer->ID) === "fulltime" ? "Fulltime" : "Parttime")?>
<? echo (get_field('jobCategories', $joboffer->ID))?>">
<?php
endforeach;
endif;
?>
Please note if this div is in the same file with the span tag from above, you can just put this div inside the foreach. To avoid repetition of code.

string is not getting exploded

Here is my code where explode function is not getting correctly.code looks like this
<div id="demo-1" data-zs-src='[<?php foreach( $slides as $slide ){?>"<?php echo base_url();?>uploads/<?php echo $slide->image;?>"<?php }?>]' data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
my result looks like this
<div id="demo-1" data-zs-src='["http://localhost/fahiz_kitchen/uploads/upload-file1496902770.jpg""http://localhost/fahiz_kitchen/uploads/upload-file1496901910.gif""http://localhost/fahiz_kitchen/uploads/upload-file1496901900.jpg""http://localhost/fahiz_kitchen/uploads/upload-file1496901887.jpg"]' data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
</div>
i want to get comma in between the result that means my result should be like this
<div id="demo-1" data-zs-src='["http://localhost/fahiz_kitchen/uploads/upload-file1496902770.jpg",
"http://localhost/fahiz_kitchen/uploads/upload-file1496901910.gif",
"http://localhost/fahiz_kitchen/uploads/upload-file1496901900.jpg",
"http://localhost/fahiz_kitchen/uploads/upload-file1496901887.jpg"
plz use this;
<?php foreach( $slides as $slide ){
$images= base_url()."uploads/". $slide->image;
var_dump($images);
$arr= explode(',',$images);
print_r($arr);
}
?>
Chnage it like.
$data = [<?php
foreach( $slides as $slide ){
?>"<?php echo base_url();?>uploads/<?php echo $slide->image;?>",
<?php }?>];
$finaldata = rtrim($data, ',');
<div id="demo-1" data-zs-src="'.$finaldata.'" data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>
You are starting php inside already started it.
After doing some changes i got the result and the code used for that is as follows
<?php foreach ( $slides as $slide ) {
$data[] = $images='"'.base_url()."uploads/". $slide->image.'"';
$y=implode(',',$data);
}?>
<div id="demo-1" data-zs-src='[<?php echo $y;?>]' data-zs-overlay="dots">
<div class="demo-inner-content">
<h1><span>Tasty</span> & <span>Healthy</span></h1>
<p>For those who have taste for life.</p>
</div>

cake php: HTML links for certain id?

I'm new to CakePHP, I was wondering if there is a way to echo information from the database using a foreach loop but only have HTML links on images where the id is 1 & 7. What's the best way of achieving this?
<?php if ( isset($articles) ){ ?>
<?php foreach($articles as $article):?>
<?php echo ($this->Html->image($article['Post']['picture'], array('alt' => 'storyimage', 'class' => 'smallimg'));?>
<h3 class="caps"><?php echo $article['Post']['genre'];?></h3>
<h2><?php echo $article['Post']['story'];?></h2>
<div id="contentbox2">
</div>
<?php endforeach; ?>
<?php } ?>
This is how it looks in the veiw, my database in looks image data is stored like this:
suki-burberry-sq_500_500_90_s_c1.jpg
Would it be best to echo all the data individually without the foreach loop or could I write an if statement?
If you want only to get the 1 & 7 id you can do this, assuming that it is a predefined number. If it is dynamic or changeable you can do that in your controller.
<?php if ( isset($articles) ){ ?>
<?php foreach($articles as $article):?>
<?php if ($article['Post']['id']==1 || if ($article['Post']['id']==7) ): ?>
<?php echo ($this->Html->image($article['Post']['picture'], array('alt' => 'storyimage', 'class' => 'smallimg'));?>
<h3 class="caps"><?php echo $article['Post']['genre'];?></h3>
<h2><?php echo $article['Post']['story'];?></h2>
<div id="contentbox2">
</div>
<?php endif; ?>
<?php endforeach; ?>
<?php } ?>
Best way is to find from your Model only what you needed.
with the condition $conditions=array('Post.id'=>array(1,7));

Unable to apply foreach loop properly in PHP

I want two posts at each slide. but getting only one slide. I am new in programming, please help me.
$widget_id = $widget->id.'-'.uniqid();
$settings = $widget->settings;
$navigation = array();
$captions = array();
$i = 0;
?>
<div id="slideshow-<?php echo $widget_id; ?>" class="wk-slideshow wk-slideshow-revista-articles" data-widgetkit="slideshow" data-options='<?php echo json_encode($settings); ?>'>
<div>
<ul class="slides">
<?php foreach ($widget->items as $key => $item) : ?>
<?php
$navigation[] = '<li><span></span></li>';
$captions[] = '<li>'.(isset($item['caption']) ? $item['caption']:"").'</li>';
/* Lazy Loading */
$item["content"] = ($i==$settings['index']) ? $item["content"] : $this['image']->prepareLazyload($item["content"]);
?>
<li>
<article class="wk-content clearfix"><?php echo $item['content']; ?></article>
</li>
<?php $i=$i+1;?>
<?php endforeach; ?>
</ul>
<?php if ($settings['buttons']): ?><div class="next"></div><div class="prev"></div><?php endif; ?>
<?php echo ($settings['navigation'] && count($navigation)) ? '<ul class="nav">'.implode('', $navigation).'</ul>' : '';?>
<div class="caption"></div><ul class="captions"><?php echo implode('', $captions);?></ul>
</div>
</div>
http://i.stack.imgur.com/sy1ih.png
you're missing the { after the foreach and at the end of the loop.
<?php foreach ($widget->items as $key => $item) {
$navigation[] = '<li><span></span></li>';
$captions[] = '<li>'.(isset($item['caption']) ? $item['caption']:"").'</li>';
/* Lazy Loading */
$item["content"] = ($i==$settings['index']) ? $item["content"] : $this['image']->prepareLazyload($item["content"]);
?>
<li>
<article class="wk-content clearfix"><?php echo $item['content']; ?></article>
</li>
<?php
$i=$i+1;
}
?>
Your foreach syntax looks fine. That syntax is a lot easier for some people to read when embedded in HTML than the traditional braces.
Can I ask what the $this variable refers to? I don't see this instantiated anywhere in your code? Is it supposed to be $item instead?
$settings['index'] never changes within the loop, however $i does.
Change to: ($i<2)

jQuery Sortable Lists with CodeIgniter

I am attempting to make a sortable list out of list items populated from the database using the jQuery plug in but the effect is only applied to the first item presented:
<?php if(isset($bookmarks)) : foreach($bookmarks as $row) :?>
<div id="makeDrag">
<?php $fixed = preg_replace('#^[^:/.]*[:/]+#i', '', $row->URL); ?>
<li>
<div class="well">
<div><?php echo anchor('http://'.$fixed, $row->Name); ?></div>
<div><strong>Comments:</strong> <?php echo $row->Comments; ?></div>
<h4 class="btn-small">
<?php echo anchor("site/delete/$row->id", "Delete"); ?>
</h4>
</li>
</div>
<?php endforeach; ?>
I can kind of see where this is going wrong but do not know how to fix it. I would obviously like the effect to affect all the populated li not just the first one. Any help would be great. Sorry if I am unclear, I can try and rephrase things if this is confusing.
The cause is likely because you have
$('#makeDrag').sortable();
but you also have a foreach statement that creates multiple #makeDrag elements thus making your HTML invalid.
To fix this:
<?php if(isset($bookmarks)) : ?>
<ul id="makeDrag">
<?php foreach($bookmarks as $row) : ?>
<?php $fixed = preg_replace('#^[^:/.]*[:/]+#i', '', $row->URL); ?>
<li>
<div class="well">
<div><?php echo anchor('http://'.$fixed, $row->Name); ?></div>
<div><strong>Comments:</strong> <?php echo $row->Comments; ?></div>
<h4 class="btn-small"><?php echo anchor("site/delete/$row->id", "Delete"); ?></h4>
</div>
</li>
<? endforeach; ?>
</ul>
<?php endif; ?>
HTH

Categories