Get ACF field on the different pages?
Problem: How get ACF field on the different pages, e.g. in functions.php?
Solution: In order to obtain contain ACF field on any pages, including functions.php we should input page ID to as second argument of the ACF function get_field( 'some_field', page_ID )
. We'll look at a little bit example when we need get ACF fields in AJAX query. Ok, we should do some simple steps:
Step 1: On the page, on which call AJAX:
<script>
...
var current_page_id = '<?php echo $post->ID; ?>'; // get page ID. If we need get post ID without loop use $wp_query->get_queried_object_id();
...
</script>
Step 2: in the script for set param AJAX write:
...
var data = {
...
'page_ID' : current_page_id
};
...
Step 3: in AJAX function in the functions.php
write:
...
$page_ID = ( (int)$_POST['page_ID'] );
$example = get_field( 'example', $page_ID );
...
Note: Front Page ID we can get use: get_option( 'page_on_front' )