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.
Related
I wondered if anyone could help me combine two blocks of code I have. I have one block looping though items and the start of another block showing a count and hopefully enabling me to display the items looping through in rows by adding a div around them every two items... Heres the first bit of code, the loop:
<?php if(get_field('areas')): ?>
<?php while(has_sub_field('areas')): ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php endwhile; ?>
<?php endif; ?>
I'm using Advance Custom Fields for Wordpress and this is pulling through repeater fields... this displays them just one after the other.
Here's the code I have found to hopefully display them in rows.
<?php
$num = 1;
foreach ( $terms as $term ) {
if($num%2) {
echo '<div class="area-row">';
}
// Other Code
if($num %2) {
echo '</div>';
}
$num++
}
?>
I would like to display them in rows of two...
ONE TWO
THREE FOUR
FIVE SIX
Etc...
So, Im guessing I need to combine the code somehow... I currently have this: but it doesn't seem to work:
<?php
$num = 1;
foreach ( $terms as $term ) {
if($num%2) {
echo '<div class="area-row">';
}
if(get_field('areas')): ?>
<?php while(has_sub_field('areas')): ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php endwhile; ?>
<?php endif; ?>
if($num %2) {
echo '</div>';
}
$num++
}
?>
Okay so it looks like there was a couple of simple issues with this, you had closed the php tag after your if statment and then continued to write php without reopening the php tags. Also there is a slight logic error with the if($num%2) statements as one of these needs to be if, the other needs to be if not, so that the alternate.
Give this code a try and let me know how you get on:
<?php
if(get_field('areas')):
$num = 1;
?>
<?php while(has_sub_field('areas')):
if($num%2) {
echo '<div class="area-row">';
} ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php
if(!$num%2) {
echo '</div>';
}
$num++
endwhile; ?>
<?php endif; ?>
The main reason behind this question was for me to be able to target the third item of the looped through items, as it had padding on it that was breaking the rows that I needed to remove.
I have since found another solution that seems to be working.
By using the nth-child element, I have been able to target and remove the padding on every third item that's looped through - fixing the issue.
.single-area-item:nth-child(3n+3) {
margin-left: 0;
}
This is for rows or 2 items, if it were rows of 3 or 4 then it would need to target every 4th or 5th item respectively.
I'm still learning php, but can't get my head on this one.
For a loop in Wordpress I want to output a list with places, seperated with a comma and ending with a point.
Here's my code:
<?php if ( have_rows('subplaats') ): ?>
<section id="subplaats">
<div class="subplaats-container">
<h3 class="support">Wij bestrijden ook in...</h3>
<p>
<?php while( have_rows('subplaats') ): the_row(); ?>
<?php $plaats = get_sub_field('plaats'); ?>
<?php echo $plaats; ?>,
<?php endwhile; ?>
</p>
</div>
</section>
<?php endif; ?>
Could anyone tell me how to accomplish this? I'm using Advanced Custom Fields. Am I also doing right on hierarchical level?
You need to count the total fields in the repeater:
count(get_field('subplaats'));
then have a field counter to check if the current "counted" field is the last one.
I edited and tested your code and It's working good:
<?php
if (have_rows('subplaats')):
$all_fields_count = count(get_field('subplaats'));
$fields_count = 1;
?>
<section id="subplaats">
<div class="subplaats-container">
<h3 class="support">Wij bestrijden ook in...</h3>
<p>
<?php
while (have_rows('subplaats')): the_row();
$plaats = get_sub_field('plaats');
echo $plaats;
if ($fields_count == $all_fields_count) {
echo ".";
} else {
echo ", ";
}
$fields_count++;
endwhile;
?>
</p>
</div>
</section>
<?php
endif;
?>
Cheers!
I am using advanced custom field repeater to load some sub_fields which you can see in the below code:
<?php
if( get_field('who_made_it') ): ?>
<div id="role-wrapper">
<?php while( has_sub_field('who_made_it') ): ?>
<div class="role-item">
<?php the_sub_field('job_role'); ?>
<?php the_sub_field('description'); ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
I would like to count how many .row-item's there are and then print that number as a class on the container #role-wrapper .
So as a HTML demo of how it would look:
<div id="role-wrapper" class"roleitems-3">
<div class="role-item">
content in here
</div>
<div class="role-item">
content in here
</div>
<div class="role-item">
content in here
</div>
</div>
As specified by the docs, get_field() returns an array() of sub fields in this case. As a result, you can do a simple count():
<div id="role-wrapper" class"roleitems-<?php echo count( get_field('who_made_it') ); ?>">
I am unfamiliar with has_sub_field and the advanced custom field repeater, but it seems a simple answer would be to add a counter.
<?php
$counter = 0;
while( has_sub_field('who_made_it') ):
//do stuff
$counter++;
endwhile;
//output the counter however you like
echo('<div class="counter">Total: ' . $counter . '</div>');
?>
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));
I have the following code:
<?php $buycheck = get_post_meta($post->ID, 'buy-link', true); ?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Later in my code I want to be able to say that if buy-link does not exist - i.e. there is no data in that field - then do something, else do something different.
Not sure how to do this! Help appreciated!
(By the way, I posted this question to Wordpress Stack Exchange first. It was voted closed there because it apparently concerns PHP boolean logic more than Wordpress - https://wordpress.stackexchange.com/questions/60387/how-do-i-do-if-post-meta-does-not-exist#comment78412_60387)
<?php if($buycheck ==''){ /*stuff*/ } ?>
this will render $buycheck, and if it is empty == is equal to '' nothing.
You can set a global variable that you can check later to see if the buylink exists:
<?php
$buycheck = get_post_meta($post->ID, 'buy-link', true);
$GLOBALS['buy_link_exists'] = !empty($buycheck);
?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Then later on in your code:
<?php if ($GLOBALS['buy_link_exists'])): ?>
it exists, do one thing
<?php else: ?>
it does not exist, do something else
<?php endif; ?>
If you need the actual value, you can set a global containing the return value from get_post_meta so you can use the actual value.