Ask your WordPress questions! Pay money and get answers fast! (more info)

Advanced Custom Fields help with repeater field sub select fields WordPress

  • SOLVED

Hello,

I am using [[LINK href="http://www.advancedcustomfields.com/"]]advanced custom fields[[/LINK]].

I have a [[LINK href="http://www.advancedcustomfields.com/docs/field-types/repeater/"]]repeater field[[/LINK]]. Inside this repeater field, I have a [[LINK href="http://www.advancedcustomfields.com/docs/field-types/select/"]]select field[[/LINK]].


<?php if(get_field('promotions_codes' )): ?>

<div class="btn-group">

<?php while(has_sub_field('promotions_codes' )): ?>

<?php

$field = get_field_object('promotions_codes' );
$value = get_sub_field('select_promotion' );
$label = $field['choices' ][ $value ];

?>

<a class="btn" href="<?php echo $value; ?>" title="<?php echo $label; ?>" target="_blank"><?php echo $label; ?></a>

<?php endwhile; ?>

</div>

<?php endif; ?>



I have simply created a repeater field called <strong>promotions_codes</strong>

Then the sub select field in my repeater is called <strong>select_promotion</strong>

My select field options are...


http://promo.38849.com : Low Rate Finance
http://promo.56956.com : Free Accessories
http://promo.85155.com : Extended Warranty



The code above seems to work fine, but the only thing that I can't seem to echo is the <strong>$label</strong>.

So my question is, can anyone help me see what I'm doing, and how can I echo the <strong>$label</strong> ?


So I asked Elliot at ACF, to see if he could help me understand where I am going wrong.

[[LINK href="http://support.advancedcustomfields.com/discussion/comment/12843#Comment_12843"]]See discussion here.[[/LINK]]

He kindly replied, but I am struggling to understand what he means. This is what he said...

<blockquote>
The problem is with your $label variable. You need to get the choices from the sub field, not the parent field.

What you can do is use $field to look at the 'sub_fields' array. Your sub field will be in there.

Then you can find the 'choices' from your sub_field.

Just use print_r($field) to understand how the data is setup
</blockquote>

So I did what he said and printed $field, and this what I got...


Array (
[key] => field_5076967aedf64
[label] => Promotions Codes
[name] => promotions_codes
[type]=> repeater
[order_no] => 0
[instructions] =>
[required] => 0
[conditional_logic] => Array (
[status] => 0
[rules] => Array (
[0] => Array (
[field] => null [operator] => ==
)
)
[allorany] => all
)
[sub_fields] => Array (

[field_4] => Array (
[choices] => Array (
[http://promo.38849.com] => Low Rate Finance
[http://promo.56956.com] => Free Accessories
[http://promo.85155.com] => Extended Warranty
)
[label] => Promotion
[name] => select_promotion
[type] => select
[instructions] =>
[column_width] =>
[default_value] =>
[allow_null] => 1
[multiple] => 0
[order_no] => 0
[key] => field_4
)

)
[row_min] => 0
[row_limit] =>
[layout] => table
[button_label] => Add Promotion
[value] => Array (
[0] => Array (
[select_promotion] => http://promo.38849.com
)
)
)


Though I'm not sure how to get the sub field object from this?


I tried this...


<?php
$field = get_field_object('promotions_codes');
$subField = $field['sub_fields'];
$value = get_sub_field('select_promotion');
$label = $subField['choices'][ $value ];
?>


But I am still having no such luck echo'ing the $label of my select sub field.


Can anyone help please.

Thanks

Answers (1)

2013-01-11

paul de wouters answers:

shouldn't it be $field['sub_fields']['field_4']['choices']


Josh Cranwell comments:

Ahhh thanks.

I tried this did not work...

<?php
$field = get_field_object('promotions_codes');
$value = get_sub_field('select_promotion');
$label = $field['sub_fields']['field_4']['choices'][ $value ];
?>

But I tried this and it did work...

<?php
$field = get_field_object('promotions_codes');
$subField = $field['sub_fields']['field_4'];
$value = get_sub_field('select_promotion');
$label = $subField['choices'][ $value ];
?>

So thank you!