Support

Account

Home Forums General Issues Homepage with Loop & ACF

Solving

Homepage with Loop & ACF

  • Hi.

    I just need some clarification.

    Firstly, do ACF functions have to be inside the WordPress Loop?

    Secondly, my WordPress homepage a WP loop calling the latest posts, and a repeater field. Would I put the code for the repeater on the same .php file that I call the WP loop to grab the posts, OR would I have to put it onto a template page/static page etc (if so, could you please explain as I’m confused on this point)

    I’m having a nightmare trying to get ACF to work on the homepage alongside a wordpress Loop calling the latest posts. Please advise me.

    Kind regards

  • Hi @allan.crabtree

    ACF saves / loads data to and from a $post. Keeping this in mind, it would make sense to place the template code within the theme file that renders out the post’s data.

    For a single page, this is most likely single.php or page.php
    For a loop, your theme may be loading in a partial file called content-post.php – in which case this is also where you would want to place the code.

    This of the_field as a similar function to the_content(); it is relevant only to the current post object, not the global WP site.

    Hope that helps.

    Thanks
    E

  • Hi elliot,

    There are two options in the Reading settings in WP back-end. Static page and posts page. I’ve set the static page option to my Homepage which uses a template that contains code that does the following (I’ve abbreviated it ofc but it fully works. It’ll either display ACF fields or the posts, never both):

    Gets the Posts.

    <?php while ( have_posts() ) : the_post(); ?>
    ...Do stuff

    Afterwards, displays the ACF repeater fields.

    <?php if( get_field('care_list') ): ?>
        <?php while(has_sub_field('care_list')): ?>
         ...Do stuff

    I cant set the Homepage to be a static page and posts page, but essentially I need to display both on the same page anyway. How do I do this?

  • Hi @allan.crabtree

    If you set the posts home page to appear on a static page (Homepage), then WP will change the query run to only find posts, not find the page (Homepage).

    This is a limitation with WP, not ACF.

    To get around it, you can load the data specifically from the Homepage using a $post_id parameter on all get_field / has_sub_field functions.
    You can read about this here:
    http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-another-page/

    Hope that helps.

    Thanks
    E

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

The topic ‘Homepage with Loop & ACF’ is closed to new replies.