0

In the below code, I call fields from Advanced Custom Fields plugin and only the first two show 'home_title' and 'home_content'. After these two I run two different loops to show the latest posts in a given category. After those loops run there are 4 more fields from ACF called. 'donate_title' , 'donate_content' , 'mission_title' , 'mission_content'. Which are not showing up (not pulling any content at all).

If I move these ACF before running the loops they all show up correctly. So I imagine there is a problem with these following the loops but cannot find the reason.

<div class="main-site">
<div class="home-title-1">
<?php the_field('home_title'); ?>
</div>
<div class="home-content-1">
<?php the_field('home_content'); ?>
</div>

<div class="home-boxes-cont">
<div class="box-left">
<?php
query_posts('cat=4&posts_per_page=1');
while (have_posts()) : the_post(); ?>
    <div class="bl-img">
    </div>
    <div class="bl-title">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </div>
    <div class="bl-content">
    <?php the_excerpt(); ?>
    </div>
<?php endwhile; ?>
</div>

<div class="box-middle">
<?php
query_posts('cat=5&posts_per_page=1');
while (have_posts()) : the_post(); ?>
    <div class="bm-img">
    </div>
    <div class="bm-title">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </div>
    <div class="bm-content">
    <?php the_excerpt(); ?>
    </div>
<?php endwhile; ?>
</div>

<div class="box-right">
    <div class="br-img">
    </div>
    <div class="br-title">
    <?php the_field('donate_title'); ?>
    </div>
    <div class="br-content">
    <?php the_field('donate_content'); ?>
    </div>
</div>
</div>

<div class="mission-title">
    <?php the_field('mission_title'); ?>
</div>
<div class="mission-content">
    <?php the_field("mission_content"); ?>
</div>

5
  • Are any of those last four fields set on option pages? Commented Nov 13, 2015 at 17:53
  • They all have identical settings to the first two Commented Nov 13, 2015 at 17:57
  • Ok. Try storing them all in variables at the top of your code, and then echoing them out wherever you need them. Commented Nov 13, 2015 at 18:11
  • I tried adding it as a variable up top in: <?php $dt = get_field('donate_title'); ?> Then <?php echo '$dt'; ?> and am only getting the $dt to show Commented Nov 13, 2015 at 18:22
  • Echo $dt; Not '$dt'. Commented Nov 13, 2015 at 18:26

3 Answers 3

2

In order to get custom field data from the original post after altering the global post data with your query_posts() calls, you need to reset your post data with the wp_reset_query() function. Place this function after each loop -

<?php while (have_posts()) : the_post(); ?>

...

<?php endwhile; wp_reset_query(); ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Johnny, I was trying some of the other options but this was the easiest and got me what I needed.
0

You are altering the global wp_query variable. When you do that and the the result is not is_single() then you cannot pull any ACF settings any longer.

Either reset the wp_query to its original setting for the page or store the vars in an array before you make any wp_query changes, and retrieve them as needed later in the code.

Comments

0

While I agree with Scriptonomy, you should really just use get_posts(). This is exactly what this function is designed to do... custom loops outside the main loop. You should rarely ever need to modify the global wp_query variable.

If you still want to use the_permalink() and the_title() without passing a post id, then scroll down the page to the section labeled "Access all post data" and you'll see how to use setup_postdata() to make it easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.