How to get multiple custom field on wordpress? - php

i want to get multiple custom field from wordpress with one line for example. I have field "link","link1","link2","link3","link4" and "link_titull","link_titull1","link_titull2","link_titull3","link_titull4"
I have the code to get just one field and if its not filled its showing empty.
<?php $prop_det_url = get_field('link');
if($prop_det_url!=''){ ?>
> <?php the_field('link_titull'); ?>
</p>
<?php } ?>

<?php
for( $i=1; $i<=4; $i++){
$prop_det_url = get_field('link'.$i);
if( $prop_det_url != '' ){ ?>
> <?php the_field('link_titul'.$i); ?>
<?php }
}
?>

Related

Adding div class based on PHP variable from ACF repeater field?

I am looping through a repeater field in Advanced Custom Fields and displaying divs for each item in the repeater. If the index is equal to 0, I want to add a special class to just that div. Is this possible? Here's what I've tried:
<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
<?php while (have_rows('products')) : the_row(); ?>
<div class="product <?php if ($i == 0) { echo 'active-class'; } ?>"></div>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>
Unfortunately this is not working.
Instead of doing the conditional within the class, I just did it on the outside and defined two different class tags based on the condition:
<?php if (have_rows('products')): $i = 0; ?>
<div class="product-container">
<?php while (have_rows('products')) : the_row(); ?>
<div <?php if ($i == 0) { ?>class="product active"<?php } else { ?>class="product"<?php } ?> ></div>
<?php $i++; endwhile; ?>
</div>
<?php endif; ?>

how to select an specific custom field

im trying to select a custom field in this case the 10th field so i can change the input type from text to date, but i not quite sure how to accomplish this, here is my code.
<?php for ($i = 0; $i < 11; $i++)
{
?>
<?php
if($this->config->item('custom'.$i.'_name') != NULL)
{
$item_arr = (array)$item_info;
?>
<div class="field_row clearfix">
<?php
echo form_label($this->config->item('custom'.$i.'_name').':', 'custom'.$i,array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'custom'.$i,
'id'=>'custom'.$i,
'value'=>$item_arr['custom'.$i])
);?>
</div>
</div>
<?php
}
}
?>
i have 10 custom fields, i select 10th cause i need to set date picker (right now i got the form_password) , i tried this but well the 10th shows but doesn't work as desire, syntaxis errors and it outputs all 10 fields and not the ones i selected only as example
<?php for ($i = 0; $i < 11; $i++)
{
?>
<?php
if($i < "9")
{
$item_arr = (array)$item_info;
?>
<div class="field_row clearfix">
<?php
echo form_label($this->config->item('custom'.$i.'_name').':', 'custom'.$i,array('class'=>'wide')); ?>
<div class='form_field'>
<?php echo form_input(array(
'name'=>'custom'.$i,
'id'=>'custom'.$i,
'value'=>$item_arr['custom'.$i])
);?>
</div>
</div>
<?php
}
else {
echo form_password(array(
'name'=>'custom'.$i,
'id'=>'custom'.$i,
'value'=>$item_arr['custom'.$i])
);
}
}

Hello, I want to get multiple custom field from wordpress with one line

i want to get multiple custom field from wordpress with one line for example. I have field "link","link1","link2","link3","link4" and "link_titull","link_titull1","link_titull2","link_titull3","link_titull4"
I have the code to get just one field and if its not filled its showing empty.
<?php $prop_det_url = get_field('link');
if($prop_det_url!=''){ ?>
> <?php the_field('link_titull'); ?></p>
<?php } ?>
You can use for loop. See sample code below:
<?php
for( $i=1; $i<=4; $i++){
$prop_det_url = get_field('link'.$i);
if( $prop_det_url != '' ){ ?>
> <?php the_field('link_titul'.$i); ?>
<?php }
}
?>

Loop through post array

Ive got a form which adds a text fields dynamically when i want to add more records to a one to many relationship table.. for example movie has more than one video.. i can get one record to add but cant seem to get the rest to work...
form --
<div class="row clone">
<?php echo $form->labelEx($modelYoutubeVideo,'embed_code'); ?>
<?php echo $form->textField($modelYoutubeVideo,'embed_code',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($modelYoutubeVideo,'embed_code'); ?>
<?php echo $form->labelEx($modelYoutubeVideo,'description'); ?>
<?php echo $form->textField($modelYoutubeVideo,'description',array('size'=>50,'maxlength'=>250)); ?>
<?php echo $form->error($modelYoutubeVideo,'description'); ?>
</div>
<?php
$this->widget('ext.widgets.reCopy.ReCopyWidget', array(
'targetClass'=>'clone',
));
?>
Controller --
if( isset( $_POST['YoutubeVideo']['embed_code'] ) ) {
for($i=0; $i<count( $_POST['YoutubeVideo']['embed_code'] ); $i++) {
$modelYoutubeVideo = new YoutubeVideo();
$modelYoutubeVideo->embed_code = $_POST['YoutubeVideo']['embed_code'][$i];
$modelYoutubeVideo->description = $_POST['YoutubeVideo']['description'][$i];
$modelYoutubeVideo->movie_id = $model->id;
$modelYoutubeVideo->save();
}
}

Remove "," at end of foreach loop

I've created a loop to list a set of meta values. I've been able to apply a class to the last item in the list, but I'd like to remove the "," at the end of the last value. Any help would be much appreciated.
<?php $count = count($subcategory); $num = 0; ?>
<?php foreach ($subcategory as $subcategory): ?>
<p
<?php if($num == $count-1){ ?>
class="subcategory-item subcategory-last-item inline-block"
<?php } ?>
class="inline-block subcategory-item"> <?php echo $subcategory;?>,</p>
<?php $num++ ?>
<?php endforeach; ?>
I may be taking an incorrect route by worrying about adding a class to the last item. If I can remove the "," from the last item I'll be happy.
Here's a quick rewrite which may lead you to a solution:
<?php $count = count($subcategories); $num = 0; ?>
<?php $classes = 'inline-block subcategory-item'; ?>
<?php foreach ($subcategories as $subcategory): ?>
<p class="<?=$classes.($num==$count-1?' subcategory-last-item':'')?>">
<?php echo $subcategory;?>
<?php if ($num<$count-1): ?>
,
<?php endif; ?>
</p>
<?php $num++ ?>
<?php endforeach; ?>

Categories