0

is there a way I can get

<?php the_field("do_stuff")?>

to go in the middle of a multiple loop like this:

            <?php query_posts('cat=4&posts_per_page=5'); ?>

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

                <h1><?php the_title() ;?></h1>      
                <?php the_post_thumbnail(); ?>
                <?php the_excerpt(); ?>

            <?php endwhile; else: ?>

                <p>Sorry, there are no posts to display</p>

            <?php endif; ?>

            <hr>

            <?php rewind_posts(); ?>

            <?php query_posts('cat=5&posts_per_page=5'); ?>

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

                <h2><?php the_title() ;?></h2>      

                <?php the_excerpt(); ?>

            <?php endwhile; else: ?>

                <p>Sorry, there are no posts to display</p>

            <?php endif; ?>

I tried putting it above rewind_posts and just below it but it does not show up? The field code is part of the "Advanced Custom Fields" plugin and I would like the user to be able post what ever they want in between the two post sections.

Thanks

1

You should not be using query_posts at all, it is just problematic and is prone to failure

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination)

I don't even thing that you need to run a custom query at all here, let alone two. I think what you are trying to do here is to split the actual query in two just for the sake of adding your custom field in between post one and two

I would just revert back to the main query and use it as is. The built-in loop counter $wp_query->current_post is available anywhere in your template, so you can use this to count your posts

This is untested, but you can most probably do something like this. Just add this after your loop. *Remeber, the loop counter starts at zero, not one

if( 0 == $wp_query->current_post ) {
    // Add whatever you need to add to appear after post one
}

If you need to paginate and need this after each first post on each page, I would think something like this will work

if( 0 == $wp_query->current_post || 0 == $wp_query->current_post%JUST_ADD_YOUR_POSTS_PER_PAGE_VALUE_HERE  ) {
    // Add whatever you need to add to appear after post one
}

EDIT

The dynamics has change dramatically, so my first approach won't work. You will need to run two custom loops just on the fact that both are quite different. I would suggest using WP_Query in stead of query_posts. You might even use get_posts as another alternative.

Just a very important note here, whenever you run custom queries, you have to reset them, otherwise they will influence any other queries that comes after, and I think that might have been your problem in the first place.

Try something like this

       <?php $query_one = new WP_Query('cat=4&posts_per_page=5'); ?>

        <?php if ( $query_one->have_posts() ) : while ( $query_one->have_posts() ) : $query_one->the_post(); ?>

            <h1><?php the_title() ;?></h1>      
            <?php the_post_thumbnail(); ?>
            <?php the_excerpt(); ?>

        <?php endwhile; else: ?>

            <p>Sorry, there are no posts to display</p>

        <?php endif; ?>
        <?php wp_reset_postdata(); // VERY VERY IMPORTANT?>

        <hr>
        <?php // PLACE YOUR FIELD HERE ?>


        <?php $query_two = new WP_Query('cat=5&posts_per_page=5'); ?>

        <?php if ( $query_two->have_posts() ) : while ( $query_two->have_posts() ) : $query_two->the_post(); ?>

            <h2><?php the_title() ;?></h2>      

            <?php the_excerpt(); ?>

        <?php endwhile; else: ?>

            <p>Sorry, there are no posts to display</p>

        <?php endif; ?>
        <?php wp_reset_postdata(); // VERY VERY IMPORTANT?>
  • oops sorry I meant to use <?php query_posts('cat=5&posts_per_page=5'); ?> instead of <?php query_posts('showposts=3&offset=1&post_type=post'); ?> does that still matter as I am trying to get the field between two different category posts? Thanks, – Nsokyi Sep 18 '14 at 15:11
  • Ok, this changes things. You should also update your question with that info. One question, on which page or template is this – Pieter Goosen Sep 18 '14 at 15:13
  • Changed it now. – Nsokyi Sep 18 '14 at 15:17
  • And the template you are using this code in, like index.php – Pieter Goosen Sep 18 '14 at 15:20
  • it's a template page that is showing two custom category posts. When I use the code above I can get the two cat posts to show OK just not the Field part. – Nsokyi Sep 18 '14 at 15:24
2

If you're trying to get a field associated with the page you're performing these extra queries on, you have to first reset the global $post after any custom queries, or you will be attempting to fetch the field from whatever post was last in the last loop you ran.

wp_reset_postdata();
the_field("do_stuff");

If you don't pass a second argument to the_field it will use whatever is in the global $post->ID. You can also pass an explicit post ID you'd like to fetch fields from:

$the_id_of_my_page = 42;
the_field( "do_stuff", $the_id_of_my_page );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.