Support

Account

Home Forums Front-end Issues ACF fields not displaying on 'Posts Page'

Solved

ACF fields not displaying on 'Posts Page'

  • I have a page called “Blog”. It’s set as the page to display the posts (duh…)
    I have created two fields for Sidebar control:
    – use_sidebar 1/0 (true/false)
    – choose_sidebar (10 different choices).
    The fields are in a meta box and visible when:
    Page Type is equal to Posts Page
    or
    Post Type is equal to post
    or
    Post Type is equal to page

    page.php contains this:
    <?php if($use_sidebar) { get_sidebar(); } ?>

    sidebar.php contains this:

    <?php
    $use_sidebar = get_field('use_sidebar');
    $choose_sidebar = get_field('choose_sidebar');
    dynamic_sidebar($choose_sidebar);
    ?>

    It works just fine on Pages. I can choose sidebars and they show up as expected. However on the “Blog” page, i.e. the page that display the posts loop, does not show a sidebar at all. I’ve added echo $choose_sidebar; to the “Blog”-page in order to see if anything returned. It returns null.

    It works fine on individual posts (single.php contains the variables and the if-statement for get_sidebar() )

    I’m not very new to WordPress, but PHP isn’t my strongest area of expertise.. 🙂

    Thankful for any help.

  • Hi @Patrik

    When you select a page to act as the ‘Blogs’ archive page, the global $post object does not point to the page called Blog, instead it points to the loop of posts.

    This means that ACF will load the data (via get_field) from the wrong place (not page called Blog).

    To get around this, you will need to specify the Blog page ID in the get_field function.

    something like:

    
    <?php
    
    $post_id = false;
    
    if( is_home() )
    {
    	$post_id = 123; // specif ID of home page
    }
    
    $use_sidebar = get_field('use_sidebar', $post_id);
    $choose_sidebar = get_field('choose_sidebar', $post_id);
    dynamic_sidebar($choose_sidebar);
    ?>
    
  • Ok so there you are using is_home() to find the homepage, but is there a similar function to find the posts archive page?

    Thx.

    Edit: Oh wait, my bad. Confused it with is_front_page() 🙂

  • This worked great. I finally have a sidebar again after some 4 hours of ripping my hair (not really – i don’t have hair). So thanks!

    While I’m at it though – is there a way of doing this more automatically? Can WP find the ID of the posts page instead of me setting it explicitly like this?

  • Hi @Patrik

    That’s more of a question for google and the WP codex.

    Cheers
    E

  • I have been trying to solve this same issue today so thought I would post my code in case other were having a similar issue

    <?php 
    $slug = get_page_by_path( 'blog' ); 
    if (is_single()) { ?>
           <h1><?php echo the_excerpt(); ?></h1>
    <?php } elseif (is_home()) { ?>
           <h1><?php the_field('page_heading', $slug->ID); ?></h1>
    <?php } else { ?>
           <h1><?php the_field('page_heading'); ?></h1>
    <?php } ?>
  • This reply has been marked as private.
  • OH my god you saved me a huge headache! It took me forever to find any documentation on this problem. Thank you!!!

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

The topic ‘ACF fields not displaying on 'Posts Page'’ is closed to new replies.