Hi All
I have been looking around and only seen one useful thread about using get_field in the functions file. Because it calls after the init ACF does not seem to work.
The thread I read did say that it would be fixed in the next ACF, the thread was back in 2013.
Just wondering if this was ever fixed. If not, is there anyway of getting the get_field to work?
Why do you want to get a field in functions.php and where do you want to get the field from?
I am using JetPack’s Infinite Scroll for the post display with updated args for the display of posts.
To update the post display on different pages I want to be able to control the category of the posts that are displayed. Here is the code:
function mm_infinite_scroll_query_args($args) {
if ( get_field('page_cat') ) {
$page_cat = get_field('page_cat');
$new_args = array(
'posts_per_page' => $args['posts_per_page'],
'paged' => $args['paged'],
'orderby' => 'date',
'order' => 'DESC',
'cat' => $page_cat,
'post_type' => array( 'post', 'features', 'it_hardware', 'videos' ),
'post_status' => 'publish',
);
return $new_args;
else {
$new_args = array(
'posts_per_page' => $args['posts_per_page'],
'paged' => $args['paged'],
'orderby' => 'date',
'cat' => 5,
'order' => 'DESC',
'post_type' => array( 'post', 'features', 'it_hardware', 'videos' ),
'post_status' => 'publish',
);
return $new_args;
}
}
Because infinite scroll runs off the page the code is all in the functions.php file hence why I want to access a field on the page which links to the loop arguments.
You will need to specify the post id of the post to get the fields from.
function mm_infinite_scroll_query_args($args) {
if ( get_field('page_cat', $post_id) ) {
$page_cat = get_field('page_cat', $post_id);
Is the post ID part of the $args passed to this function?
If this would be running on multiple pages what is the best ways of getting the postID dependent on the page it is currently on?
As far as I know, infinite scrolling uses AJAX to get the content. Unless the post ID is being sent by the request on the page there probably isn’t any way to get it.
I’m not familiar at all with how JetPack’s infinite scroll works, honestly, don’t use JetPack and do my best to avoid it. If it’s not being provided as one of the arguments to the function then you’ll probably need to alter the JavaScript that’s making the request to add the post_id.
The topic ‘get_field in the functions.php file’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.