0

I have a custom field 'first_name' created by ACF advanced custom fields. I use this code to get the value from the field: the out put would be something like Mark. What I need exactly is getting only the first letter from the first name. I tried to use

$string = the_field('first_name');
echo $string[0];

&

$string = the_field('first_name');
echo substr($string, 0, 1); 

&

$myStr = the_field('first_name'); 
$result = substr($myStr, 0, 1); 
echo $result;

but both methods give the full name not only the first letter. Any suggestions please?

1 Answer 1

0

Try this....

<?php

$string = get_field('first_name');
echo $string[0];

?>

I am not that much familiar with ACF. But my answer is based on their function reference here http://www.advancedcustomfields.com/resources/the_field/

Hope this helps.

Sabbir H

2
  • More explaination about this: the_field() will echo field content while get_field() will return field content for further use :)
    – Tan Nguyen
    Commented Aug 25, 2015 at 15:39
  • I am glad that it worked.
    – Sabbir H
    Commented Aug 25, 2015 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.