Support

Account

Home Forums General Issues Output Fields input through Template A to Template B

Solved

Output Fields input through Template A to Template B

  • Hi John & Team,
    I’m allowing a client to load in a Banner Ad into a certain place in their homepage layout using the following

      // Check if Banner is activated
        if (get_field('activate_banner_ad')):
          // if So, output banner markup & fields ?>
          <div class="bannerAd">
             <img src="/wp-content/uploads/2019/04/littleflower-logo.png" class="adLogo"/>
             <h2><?php the_field('banner_title');?></h2>
             <h4><?php the_field('banner_ad_date');?></h4>
             <a class="button orange" href="<?php the_field('banner_ad_button_link');?>" target="_blank"><?php the_field('banner_ad_button_text');?></a>
          </div><?php
        endif;?>

    That works wonderfully! All good there. The client also wants this same content available on another page of the site, so I created that same code into an add_action() and hooked that into the page template she wanted, but I’m not getting any output from ACF? My action/hook seems to work if I output anything else (like echo ‘hello’) but ACF is not getting output. I had assumed I could access those same fields from wherever. What am I not understanding?

    Here’s the code from my action hook

    <?php
    add_action( 'genesis_entry_content', 'banner_ad', 1 );
    function banner_ad() {
      // Check if Banner is activated
      echo 'hello';
      if (get_field('activate_banner_ad')):
        // if So, output banner markup & fields ?>
        <div class="bannerAd">
           <img src="/wp-content/uploads/2019/04/littleflower-logo.png" class="adLogo"/>
           <h2><?php the_field('banner_title');?></h2>
           <h4><?php the_field('banner_ad_date');?></h4>
           <a class="button orange" href="<?php the_field('banner_ad_button_link');?>" target="_blank"><?php the_field('banner_ad_button_text');?></a>
        </div><?php
      endif;
    }
    ?>
  • You need to supply the post ID so ACF knows where to get the values from

    You can get the ID using this

    
    $post_id = get_option('page_on_front');
    

    Then add this to your get_field() and the_field() calls

    
    get_field('field-name', $post_id)
    
    
    the_field('field-name, $post_id)
    
  • Thank you @hube2 That was just what I needed.

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

The topic ‘Output Fields input through Template A to Template B’ is closed to new replies.