4

I have created a group field in ACF to display on posts, pages and custom post type. Please see below screenshots.

enter image description here enter image description here

And here's the code I'm trying for front-end.

 <?php $footerCTA = get_field('footer_cta');
 echo $footerCTA['title']; ?>

The code above doesn't output anything on the front-end. Am I missing here something?

Thanks.

shutupchigo
  • 703
  • 1
  • 7
  • 19

2 Answers2

4

try this:

if( have_rows('footer_cta') ):

while( have_rows('footer_cta') ) : the_row(); 

    ?>
    <p><?php the_sub_field('title'); ?></p>
    <?php

endwhile;

endif;

?>

Khalil DaNish
  • 547
  • 5
  • 18
  • did you add them in your WordPress Post/Page loop? for display custom fields in the page, you should add the_field() function inside the wp loop so for displaying subfield you should do the same. (add this loop in your main wordpress post/page loop). – Khalil DaNish Aug 16 '18 at 05:34
  • what about the location/rules did you set the correctly? – Khalil DaNish Aug 16 '18 at 07:22
0

Try using.

 echo the_field('footer_cta');

Other Way.

You can do this by adding a second parameter to the get_field or the_field functions. This second parameter will contain the correct ID which can be found using get_option('page_for_posts') like so

<h1><?php the_field('footer_cta', get_option('page_for_posts')); ?></h1>

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">
        <?php if ( have_posts() ) : ?>

            <?php /* The loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            <?php endwhile; ?>



        <?php else : ?>
            <?php get_template_part( 'content', 'none' ); ?>
        <?php endif; ?>

        </div><!-- #content -->
    </div><!-- #primary -->
Samir Sheikh
  • 2,283
  • 2
  • 19
  • 37