Support

Account

Home Forums ACF PRO ACF on Posts Page (getoption not working)

Solved

ACF on Posts Page (getoption not working)

  • I’m trying to use ACF Pro to code a headline/page description on a WordPress page, but it just keeps showing ‘Array’ in place of the headline/description.

    I know about the <?php the_field('XYZ', get_option('page_for_posts')); ?>, but that hasn’t seemed to work for me. I’ve tried everything, but I’m at a loss- how can I make it work?

    HTML/PHP CODE:

    
        <?php /* Template Name: Blog Page Template */ ?>
        
        <?php get_header()   ?>
        
        <body>
        <!--------------- Blog Intro -------------------->
        <?php the_field('blog_introduction', get_option('page_for_posts')); ?>
            <div id="introParagraph" class=" col-xs-12 p-y-2">
                <?php
                    if (have_rows('blog_introduction')):while(have_rows('blog_introduction')):the_row();
                ?>
                <h1 class="text-xs-left col-xs-10 col-md-offset-1 m-b-1">
                    <?php
                        the_sub_field('page_header'); ?>
                </h1>
                <div class="col-md-3 col-md-offset-1 col-xs-7 m-t-1">
                    <h3 class="text-xs-left" id="headerDescription">
                        <?php
                            the_sub_field('page_description_paragraph'); ?>
                    </h3>
                </div>
                
                <?php   endwhile;
                else :
                    // no rows found
                endif;
                ?>
            </div>
    

    However, when I put that, it shows up like [this](http://i.imgur.com/rxqM0AD.png), when it should like this [this](http://i.imgur.com/UM0fZQn.png).

  • Where are these fields located in the back end? Do you edit them by editing the Home page post? Are they on an options page?

    If you are trying to get any fields from another post or from and options page then you much supply $post_id for every call. For example, this line:

    
    if (have_rows('blog_introduction')):
        while(have_rows('blog_introduction')):
        the_row();
    

    would be this:

    
    if (have_rows('blog_introduction', $post_id)):
        while(have_rows('blog_introduction', $post_id)):
        the_row();
    

    but you do not need to supply the post id in the function the_sub_field()

  • Hey @hube2 , thanks for the reply! On the backend, the fields are located in the ‘Pages’ section, under ‘Blog- Posts Page’.

  • The second part of my comment should cover it. You need to use the correct post ID in all of the function for getting or showing fields. The reason that your fields are not showing is that you’re not using the post id in the have_rows loop functions.

  • @hube2 , I think I’m still messing it up. :/ It still says ‘Array’ on the Blog Page (Posts page). Maybe I got something twisted up…

    When I added the ‘Blog Introductory’ Field, with it’s two subfields (for Headline and Page Description), I told it to show the field if ‘Page Type is equal to Posts Page’. However, when I click through to Pages > ‘Blog- Posts Page’, the field isn’t there.

    Of course, all I did was change my code directly to your recommendation, so it looks like this:

    
    <?php the_field('blog_introduction', get_option('page_for_posts')); ?>
        <div id="introParagraph" class=" col-xs-12 p-y-2">
            <?php
                if (have_rows('blog_introduction', $post_id)):
                while(have_rows('blog_introduction', $post_id)):
                the_row();
            ?>
            <h1 class="text-xs-left col-xs-10 col-md-offset-1 m-b-1">
                <?php
                    the_sub_field('page_header'); ?>
            </h1>
            <div class="col-md-3 col-md-offset-1 col-xs-7 m-t-1">
                <h3 class="text-xs-left" id="headerDescription">
                    <?php
                        the_sub_field('page_description_paragraph'); ?>
                </h3>
            </div>
    

    Forgive me, I haven’t been doing WP develoment long, but thank you for your patience!

  • see my comments in your code

    
    <?php 
      // set the post ID that you want to use and use it in all
      // of the function calls where it is needed
      // and make sure it's an integer value
      $post_id = intval(get_option('page_for_posts'));
      
      // thjis is a repeater or flex field according to the code below
      // useing the_field() will output an array because a repater
      // returns an array if you do not use a have_rows() loop
      the_field('blog_introduction', get_option('page_for_posts'));
    ?>
    <div id="introParagraph" class=" col-xs-12 p-y-2">
      <?php
        if (have_rows('blog_introduction', $post_id)) {
          while(have_rows('blog_introduction', $post_id)) {
            the_row();
            ?>
              <h1 class="text-xs-left col-xs-10 col-md-offset-1 m-b-1">
            <?php
              the_sub_field('page_header'); ?>
            </h1>
            <div class="col-md-3 col-md-offset-1 col-xs-7 m-t-1">
              <h3 class="text-xs-left" id="headerDescription">
                <?php
                  the_sub_field('page_description_paragraph');
                ?>
              </h3>
            <?php 
          } // end while
        } // end if
      ?>
    </div>
    
    
  • @hube2 I got it!!! Thanks a ton, I’ve been looking for that for weeks!!

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

The topic ‘ACF on Posts Page (getoption not working)’ is closed to new replies.