Support

Account

Home Forums General Issues Footer conditional field display is not working in blog page and single posts

Helping

Footer conditional field display is not working in blog page and single posts

  • I am going to try to explain what I want to do simply first:

    I have a sub-footer section with a simple text and call to action elements, but I just want to display this for some of the pages in my website.

    Since there’s no real way to categorize the pages in which the client wants the sub footer to be shown, I simply created a true/false field on every page for showing / hiding the sub footer element.

    It’s quite simple really, when the field is set to true on the specific page/post being displayed the sub footer shows (I created an options page where the fields are being defined), if not it doesn’t.

    So here’s my sub-footer section code:
    (php tags) if( get_field(‘sub_footer_section’) ):
    <section class=”subfooter”>
    //with a few of:
    the_field(‘field_name’, ‘options’);
    </section>
    (php tags) endif;

    This has been working perfectly on every page, but when it comes to the blog page and blog posts it won’t show up even if the checkbox is marked as true.
    Been trying to figure out what am I doing wrong here for a couple hours could really use some help.

  • The problem is that when you’re in the footer that WP/ACF doesn’t know what post to get the field from. In this case you need to supply the post ID. This is covered in the get_field() documentation http://www.advancedcustomfields.com/resources/get_field/ and this is how you can do that

    
    <?php 
      $queried_object = get_queried_object();
      if( get_field('sub_footer_section', $queried_object->ID) ):
        <section class="subfooter">
          //with a few of:
           the_field('field_name', 'options');
        </section>
      endif;
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Footer conditional field display is not working in blog page and single posts’ is closed to new replies.