Support

Account

Home Forums General Issues How to call ACF fields from my functions.php file

Solved

How to call ACF fields from my functions.php file

  • I am not sure if this is possible at all.. but how can I create a function to display generic ACF while being called in different pages?

    Lets say I want to set up captions for images, so normally I would write this code within the loop:

    		<?php $image = get_sub_field('image');?>
    		<?php $title = get_sub_field('title');?>
    		<?php $caption = get_sub_field('caption');?> 
    
    		<?php if( !empty($image) ): ?>
    			<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    			<figcaption class="img-caption"><?php echo $title;?><br/><?php echo $caption;?>
    			</figcaption>
    		<?php } ?>

    But I want to re-use this code without having to copy and paste it everytime I need to use it.

    I thought that I can write a function in my functions.php file, but I need to pull the values from each of my pages somehow. If I write get_sub_field('image');?> in the functions.php file for example, my result is null, because the field its empty. And it makes sense, because while running in my functions.php is outside the loop.

    How to achieve this?

  • Hi @ratatat,

    Hmm… As per the docs, when you make a call to get_sub_field(...) method outside a WP loop, then you have to specify a $post_id as a second argument in the method (get_sub_field(...)).

  • if you have multiple layouts where you use different pieces of the whole file (like gallery)
    and wish to edit that pieces not at every single layout then you can maybe put code into a single php file. (galleyloop.php)

    and call it there where you need it like that: include_once ('galleyloop.php');
    if include_once dont work try include

    of course that works only when you call it not outside the loop. but maybe the reason you take it outside the loop was to reuse it at different layouts

  • thanks for the hint. how would I use the $post_id, exactly?

  • unfortunately, putting the code in a separate php file doesnt work, my captions are empty.

  • if you put full code(that you show us) in a separate file and only captions are empty, then problem is somewhere else.
    because you will/should see nothing or everything.

    i assume you have a flexible-field or repeater field, and inside that you have:
    1 image field (image), and 2 text-fields (title and caption)?

    or do you only have 1 image field and try to use caption and title of it? if it is like that, then use:

    $title = $image['title'];
    $alt = $image['alt'];
    $caption = $image['caption'];

    instead of get_sub_field for title and caption

  • @mediawerk thanks for your answers.

    My code is indeed a flexible field and when I put the code which I am trying to put in a separate file, into my whole code, it works. It’s just that when I put it out of the context or I try to call it from a function, it doesnt work anymore.

    I don’t know if it makes so much sense to put my entire code, but here it is:
    http://pastebin.com/vC6b4fXw

    I want to put in a separate file everything that is inside the <figcaption>tag (or create a function that will load that code). My problem so far is, that if I put this code in a template file and I call it with a normal <?php get_template_part(); ?> or with a function via functions.php for example, my caption appears empty. While if I leave it inside my main loop, the captions are there.

  • as far as i understand <?php get_sub_field($caption); ?> for flexible fields works only inside <?php if( get_row_layout() == 'full_width_image' ): ?> and that need to be inside the flexible field while loop

    i don’t see that loop inside your file, and don’t know if you have it or not

  • My code has several levels. This code I sent you before is the code for projects-image.php (see get get_template_part below) and the loop for my posts is inside projects.php, from where I am calling different files with different ACF structure, like this:

    <?php get_header(); ?>
    
    <?php
    /*
    Template Name: Projects
    */
    ?>
    
    <?php $query = new WP_Query( 
    	array( 
    		'posts_per_page' => 1, 
    		'category_name' => 'projects',
    		'orderby' => 'date',
    		'order'      => 'DESC'
    		));
    
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();?>
    
    				<div class="row">
    					<article class="projects large-12 columns ">
    						<section class="large-6 large-centered columns">
    
    							<?php if( have_rows('projects_content') ):
    							while ( have_rows('projects_content') ) : the_row(); ?>
    
    							<?php get_template_part( 'project', 'image' ); ?>
    							<?php get_template_part( 'project', 'video' ); ?>
    							<?php get_template_part( 'project', 'text' ); ?>
    
    						<?php endwhile;?>
    					<?php endif; ?>
    				</section>
    			</article>
    		</div><!-- end.row -->
    
    	<?php // Copyright ?>
    	<div class="row">
    		<?php get_template_part('/parts/copyright'); ?>
    	</div>
    
    	<?php }
    } else {
    	// no posts found
    }
    // Restore original Post Data
    wp_reset_postdata();?>
    
    <?php get_footer(); ?>
  • If I understand your answer correctly, if <?php get_sub_field($caption); ?> for flexible fields works only inside <?php if( get_row_layout() == 'full_width_image' ): ?> and that need to be inside the flexible field while loop, then there is no way to split this captions into smaller files, no?

    I can’t call the loop in the main file and call the loop again in the captions file, because it breaks everything.

  • difficult to say. because i don’t know/understand what you wish to split and why you do it.
    also i don’t know what breaks, and why it would break.

    without that info i cant say if it is possible or not. all depends on what you wish to do for what reason.

    but primary yes, without `while ( have_rows(‘projects_content’) ) : the_row();
    if( get_row_layout() == ‘full_width_image’ ): `
    you cant use get_sub_field($caption); of your image

    you may can have the loop only one time and use part of it multiple times.
    could be done with the help of own created arrays, but that is complex, and works only in certain cases

  • I managed to solve it and my error was a very basic one.

    I just had to call my field with an ID, such as:

    $title = get_field('title', $post->ID);
    $caption = get_field('caption', $post->ID);

    adding $post->ID in order to be able to reference the template part from anywhere, just because I was pulling this info from a post object.

    For those seeking the same, these have been enlightening links:
    http://www.advancedcustomfields.com/resources/how-to-get-values-from-a-media-attachment/
    http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-page/

  • Thank you! This helped me too!

Viewing 13 posts - 1 through 13 (of 13 total)

The topic ‘How to call ACF fields from my functions.php file’ is closed to new replies.