Hello,
I am trying to output get_field_objects from using one variable called <strong>$field_name</strong>
See documentation..
[[LINK href="http://www.advancedcustomfields.com/docs/functions/get_field_object/"]]http://www.advancedcustomfields.com/docs/functions/get_field_object/[[/LINK]]
But I can't seem to get anything to output. The only thing that seems to be echo'ing is the <strong><li>: <span></span></li></strong>
Which means the condition get_field($field_name) is working, but no objects are being echo'ed.
See my code block below...
<?php
$field_name = "overall_length";
$field = get_field_object($field_name);
if ( get_field($field_name) ):
echo '<li>' . $field['label'] . ': <span>' . $field['value'] . '</span></li>';
endif;
?>
:/
I can't understand why, any help please. Money go's to first working answer.
Michael Caputo answers:
Try this:
<?php
$field_name = "overall_length";
$field = get_field_object($field_name);
if ( $field ):
echo '<li>' . $field['label'] . ': <span>' . $field['value'] . '</span></li>';
endif;
?>
Michael Caputo comments:
You can also test by echoing $field before the if statment. if it returns ARRAY, try print_r($field);
to see if you have the right array element.
Josh Cranwell comments:
No sorry did not work. Wierd
Michael Caputo comments:
What is the output of
print_r($field);
Michael Caputo comments:
You might need to pass in the Post id..
<?php
$post_id = get_the_ID();
$field_name = "overall_length";
$field = get_field_object($field_name, $post_id);
if ( $field ):
echo '<li>' . $field['label'] . ': <span>' . $field['value'] . '</span></li>';
endif;
?>
Josh Cranwell comments:
Arnav also suggest that - but nothing outputs.
But if just do this...
the_field("overall_length");
Josh Cranwell comments:
Tried passing the ID, absolutely nothing still.
But before I had <strong>get_field_object</strong>, I was using this in the same place...
<strong>the_field("overall_length"); </strong>
...which was outputting the value fine.
Can't understand this - technically it should work with out passing the id as it's within the loop.
'Scratching head'
Michael Caputo comments:
This is how i've used this plugin in the past:
if(get_field('FIELD ID')) :
echo get_field('FIELD ID');
endif;
Josh Cranwell comments:
Thanks but I needed the name too of the field as well as the value.
I worked it out tho, or found a compromise. Used the field_key instead.
Thanks for your time.
<?php
// Overall Length
$field_key = "field_5076850854f30";
$field = get_field_object($field_key);
if ( $field ):
echo '<li>' . $field['label'] . ': <span>' . $field['value'] . '</span></li>';
endif;
?>
Arnav Joy answers:
can you show me the output of
print_r($field);
Josh Cranwell comments:
Hi,
Tried this, nothing outputs weirdly -
<?php
$field_name = 'overall_length';
$field = get_field_object($field_name);
print_r($field);
if ( get_field($field_name) ):
echo '<li>' . $field['label'] . ': <span>' . $field['value'] . '</span></li>';
endif;
?>
Arnav Joy comments:
if there is no output then how can it echo anything.
try this
$field = get_field_object($field_name,get_the_ID());
instead of
$field = get_field_object($field_name);
Josh Cranwell comments:
Yeah that - nothing.
Does not make sense, originally in it's place, I just had this...
the_field("overall_length");
And the was showing me the value file. So I don't understand why <strong>get_field_object</strong> is not working as the documentation states.
Could it be a bug..
Josh Cranwell comments:
I worked it out, found a compromise. Used the field_key instead.
Thanks for your help tho.
<?php
// Overall Length
$field_key = "field_5076850854f30";
$field = get_field_object($field_key);
if ( $field ):
echo '<li>' . $field['label'] . ': <span>' . $field['value'] . '</span></li>';
endif;
?>
Pavel Petrov answers:
Try doing a print_r and post the result:
<?php
$field_name = "overall_length";
$field = get_field_object($field_name);
print_r($field);//you might have to add die(); after that to see the result
if ( get_field($field_name) ):
echo '<li>' . $field['label'] . ': <span>' . $field['value'] . '</span></li>';
endif;
?>