Solved

Get ACF field value in custom code

  • 18 May 2021
  • 6 replies
  • 259 views

Hello, i’m trying to do something so easy that I can’t figure out why it is not working.

  • On my homepage I have a “typewriter_1” ACF text field
  • I want to get that value in javascript in order for the client to not have to deal with JS code to change the sentences.
  • I tried “echo get_field” but it ain’t working.
  • The outputed value stays <?php the_field('typewriter_1'); ?> 
  • What am I doing wrong?! My client wants a typewriter effect (with blinking cursor) I know Elementor has something almost like that but my client really wants a blinking cursor.

 

icon

Best answer by António Carreira 19 May 2021, 11:01

View original

6 replies

Userlevel 5
Badge +3

How are you placing that “typewriter_1” ACF field on the homepage? As a page custom field? You might need to add the page ID to the function:
<?php the_field(‘typewriter_1’, 6); ?>

Where 6 is the page ID.

You might also want to try to get the value in Javascript without the quotes (although most of the times it should have the quotes):

var typewriter_1 = <?php the_field(‘typewriter_1’, 6); ?>;

Have you tried to get the value in PHP before moving to JS?

Hey António!

Yes it is placed as a custom field and not used by Elementor directly. I tried putting my page ID (which is 2) but nothing gets outputted.

  • Is it because the custom code portion of Elementor is not meant for this type of declaration
  • Is it because i’m outside of the regular loop?

I’ve done this a couple of times in regular themes that I made from scratch for some reasons it is not working right now :\ haha

 

 

Hi Antonio, I tried your solutions but the value doesn’t get outputted, I still get <?php the_field('typewriter_1', 2); ?> as the outputted string.

  • Is it because i’m outside of the loop?
  • Even if i put something else like <?php echo get_template_directory_uri(); ?> I don’t get what I want as a value.
  • They are regular custom field that are not being used by Elementor directly.

I’ve done this on themes that I built from scratch so I was curious as to what I am doing wrong here haha :)

 

Userlevel 5
Badge +3

Where are you adding the JS code?

It looks like PHP is being blocked.

Try to output a string like <?php echo “Hello World”; ?> to check if PHP is working or not.

Userlevel 7
Badge +6

Hi @Alex Michel 👋  Please click “Best Answer” if you feel it has been given.

Where are you adding the JS code?

It looks like PHP is being blocked.

Try to output a string like <?php echo “Hello World”; ?> to check if PHP is working or not.

You were absolutely right, the “custom code” portion of Elementor doesn’t parse PHP code. I ended up using Woody code snippets and their universal code snippet which handles PHP. Thanks a lot for your time my friend!

Reply