Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

help with function how to echo acf select label inside subfield WordPress

  • SOLVED

Sorry, got half way through a question and figured it out so I've added a new one to save a ten bucks.

Please see new question below.

I have this function i've been working on...

https://gist.github.com/jcranny/fb5fc6e454cff3cf0fcd

Please see line 27:

$type = $results_table['type'];

This is and [[LINK href="http://www.advancedcustomfields.com/"]]advanced custom fields [[/LINK]] sub select field.

The acf select field contains this...


testing : Testing Results
practice : Practice Results
qualifying : Qualifying Results
race : Race Results


Currently $type outputs the option value. Not the option label. This is standard.

To be able to output the label you have to do this... [[LINK href="http://www.advancedcustomfields.com/resources/field-types/select/"]]http://www.advancedcustomfields.com/resources/field-types/select/[[/LINK]]


$field = get_field_object('field_name');
$value = get_field('field_name');
$label = $field['choices'][ $value ];


But I'm unsure of how to apply the above to a subfield which is <strong>$results_table['type']</strong>


Can anyone help please as I need to get the field label - not the value.

Thanks
Josh

Answers (4)

2014-03-10

Bob answers:

wrote a function may it works
not able to test so you have to apply it and check do it :)

function node_modify_riders_array($rider_id)
{
// supposing that $fields contain array that you spacifed
$fields = get_field("rider_series_data", $rider_id);

foreach($fields as $field_key => $field_val) // series id 0 1 2
{
$series_id = ""; //reset series id
// get another data of particular series series, race_number, team, consturctor, machine
foreach( $field_val as $fk => $fv ){
// if key is series then value is object
if($fk == "series"){
$series_id = $fv->term_id; // get id of series and set it in variable
}else{
if( $series_id )//lets check series id exists
$new_fields[$series_id][$fk] = $fv; // if it is not object then it should be other array element
}
}

}

return $new_fields;
}


Josh Cranwell comments:

Hi Bhavesh - i'm sorry I thought I changed the question earlier as I figured it out soon after I posted, so I changed the question. Thanks for helping tho.


Bob comments:

Hi josh,
can you post screenshot of you advanced custom fields?


Josh Cranwell comments:

Certainly...

http://i.imgur.com/mgurFBp.png

Is this what you mean? As you can see my select field is within the repeater field.


Thanks
Josh


Bob comments:

May this work.

you line no 27
$type = $results_table['type'];

new code to add after line no 27
$label = $results_table['choices'][ $type ];

can you post array of $results_tables or $results_table?


Bob comments:


$results_tables = get_field('results_table', $event_id);
$results_tables_obj = get_field_object('results_table', $event_id);

if($results_tables)
{
foreach($results_tables as $results_table)
{

$hide = $results_table['hidden'];
$data = $results_table['data'];
$type = $results_table['type'];

$subField = $results_tables_obj['sub_fields'][];
$lable = $subField['choices'][$type]



In above code you can see empty bracket. If you post full array then I can understand what should be there.
hope it helps.


Josh Cranwell comments:

I think we're nearlly there but it's a step down in the array from getting caught using $lable = $subField['choices'][$type]

I printed the results <strong>$sub</strong>


Array
(
[0] => Array
(
[key] => field_531454597aaa4
[label] => Results Type
[name] => type
[_name] => type
[type] => select
[order_no] => 0
[instructions] =>
[required] => 0
[id] => acf-field-type
[class] => select
[conditional_logic] => Array
(
[status] => 0
[rules] => Array
(
[0] => Array
(
[field] => field_524033098da31
[operator] => ==
[value] =>
)

)

[allorany] => all
)

[column_width] =>
[choices] => Array
(
[testing] => Testing Results
[practice] => Practice Results
[qualifying] => Qualifying Results
[race] => Race Results
)

[default_value] =>
[allow_null] => 0
[multiple] => 0
)

[1] => Array
(
[key] => field_531de7d12be7b
[label] => Hide
[name] => hidden
[_name] => hidden
[type] => true_false
[order_no] => 1
[instructions] =>
[required] => 0
[id] => acf-field-hidden
[class] => true_false
[conditional_logic] => Array
(
[status] => 0
[rules] => Array
(
[0] => Array
(
[field] => field_531454597aaa4
[operator] => ==
[value] => testing
)

)

[allorany] => all
)

[column_width] =>
[message] =>
[default_value] => 0
)

[2] => Array
(
[key] => field_531455b17aaa5
[label] => Results Data
[name] => data
[_name] => data
[type] => repeater
[order_no] => 2
[instructions] =>
[required] => 0
[id] => acf-field-data
[class] => repeater
[conditional_logic] => Array
(
[status] => 0
[rules] => Array
(
[0] => Array
(
[field] => field_531454597aaa4
[operator] => ==
[value] => testing
)

)

[allorany] => all
)

[column_width] =>
[sub_fields] => Array
(
[0] => Array
(
[key] => field_531457695b5e3
[label] => Rider
[name] => rider
[_name] => rider
[type] => select
[order_no] => 0
[instructions] =>
[required] => 0
[id] => acf-field-rider
[class] => select
[conditional_logic] => Array
(
[status] => 0
[rules] => Array
(
[0] => Array
(
[field] => field_531454597aaa4
[operator] => ==
[value] => testing
)

)

[allorany] => all
)

[column_width] => 60
[choices] => Array
(
[] => Select rider...
[1460] => Alessandro Nocco
[1449] => Alex De Angelis
[1464] => Alex MariƱelarena
[1457] => Anthony West
[1435] => Axel Pons
[1442] => Azlan Shah
[1466] => Dominique Aegerter
[1453] => Esteve Rabat
[1446] => Franco Morbidelli
[1434] => Gino Rea
[1456] => Hafizh Syahrin
[1438] => Johann Zarco
[1436] => Jonas Folger
[1451] => Jordi Torres
[1437] => Josh Herrin
[1447] => Julian Simon
[1448] => Kohta Nozane
[1441] => Lorenzo Baldassarri
[1462] => Louis Rossi
[1468] => Luis Salmon
[1463] => Marcel Schrotter
[1455] => Mattia Pasini
[1467] => Maverick ViƱales
[1452] => Mika Kallio
[1450] => Nicolas Terol
[1445] => Randy Krummenacher
[1465] => Robin Mulhauser
[1458] => Roman Ramos
[1459] => Sam Lowes
[1439] => Sandro Cortese
[1454] => Simone Corsi
[1443] => Takaaki Nakagami
[1461] => Thitipong Warokorn
[1444] => Thomas Luthi
[1440] => Xavier Simeon
)

[default_value] =>
[allow_null] => 0
[multiple] => 0
)

[1] => Array
(
[key] => field_531481b5bedd3
[label] => Time
[name] => time
[_name] => time
[type] => text
[order_no] => 1
[instructions] =>
[required] => 0
[id] => acf-field-time
[class] => text
[conditional_logic] => Array
(
[status] => 0
[rules] => Array
(
[0] => Array
(
[field] => field_531457695b5e3
[operator] => ==
[value] =>
)

)

[allorany] => all
)

[column_width] => 40
[default_value] =>
[placeholder] =>
[prepend] =>
[append] =>
[formatting] => html
[maxlength] =>
)

)

[row_min] => 30
[row_limit] => 30
[layout] => table
[button_label] => Add Rider
)

)



Here is the code...


// results generator
function node_modify_riders_array($rider_id)
{
$fields = get_field("rider_series_data", $rider_id);

foreach($fields as $field_key => $field_val)
{
$new_fields[$field_val["series"]->term_id] = $field_val;
}
return $new_fields;
}
function results_table_generator( $post_id, $event_id ) {

$categories = wp_get_post_terms($post_id, 'category');
$series = wp_get_post_terms($event_id, 'series');
$results_tables = get_field('results_table', $event_id);
$results_tables_obj = get_field_object('results_table', $event_id);

if($results_tables)
{
foreach($results_tables as $results_table)
{

$hide = $results_table['hidden'];
$data = $results_table['data'];
$type = $results_table['type'];

$sub = $results_tables_obj['sub_fields'];
$label = $sub['choices'][$type];

if( ( 'post' == get_post_type() && $results_table['type'] == $categories[0]->slug ) || empty($categories[0]->slug ) && empty($hide) )
{

echo '<pre>';
print_r($sub); // http://wpquestions.com/question/showChronoLoggedIn/id/9397
echo '</pre>';

echo '<h4>'.$label.'</h4>'; // Still not working

echo '<div class="table-responsive">';
echo '<table class="table table-condensed table-striped table-data" style="width:100%">';
echo '<tbody>';
echo '<tr>';
echo '<th>Rider</th>';
echo '<th>Num.</th>';
echo '<th>Nat.</th>';
echo '<th>Team</th>';
echo '<th>Contstructor</th>';
// echo '<th>Machine</th>';
echo '<th>Time</th>';
echo '</tr>';

foreach($data as $row)
{

$rider_id = $row['rider'];

if (!empty($rider_id)) {

$rider_series_data = node_modify_riders_array($rider_id);
$series_id = $series[0]->term_id;

$name = get_the_title( $rider_id );
$nationality = strtoupper(get_field('rider_nationality', $rider_id));
$time = $row['time'];

$team = htmlspecialchars($rider_series_data[$series_id]["team"]);
$contstructor = htmlspecialchars($rider_series_data[$series_id]["constructor"]);
$machine = htmlspecialchars($rider_series_data[$series_id]["machine"]);
$race_number = htmlspecialchars($rider_series_data[$series_id]["race_number"]);

if ($rider_id == 1459) echo '<tr class="sam-lowes">'; else echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$race_number.'</td>';
echo '<td>'.$nationality.'</td>';
echo '<td>'.$team.'</td>';
echo '<td>'.$contstructor.'</td>';
// echo '<td>'.$machine.'</td>';
echo '<td>'.$time.'</td>';
echo '</tr>';

}
}

echo '</tbody>';
echo '</table>';
echo '</div>';

}
}
}
}


But <strong>echo '<h4>'.$label.'</h4>';</strong> is still not working?


Thanks for your help so far - sorry I had leave last night.


Bob comments:

change where you are getting sub field

$sub = $results_tables_obj['sub_fields'][0];

can you give me your skype or gmail id? It will be easy to chat there.


Bob comments:

or you can try

$label = $sub[0]['choices'][$type]


Bob comments:

Here is how I tested it.
as you said $type contain value, thus i can you value directly in my test. :)
[[LINK href="http://wp-lovers.com/wpquestions/printr_to_array.php"]]http://wp-lovers.com/wpquestions/printr_to_array.php[[/LINK]]


Josh Cranwell comments:

Hi Bhavesh

Thanks for all your help.

Worked perfect - my apologies on correspondence

2014-03-10

Thomas Varghese answers:

Best way is to use apply_filter if any data modification is required.

2014-03-10

Luis Abarca answers:

Wich one?

2014-03-10

Arnav Joy answers:

Hi Josh ,

Please check your question , its missing description.