1

Hi I am attempting to use two loops in my WP template as I am using advanced custom fields to display info from a custom post type (college)

I have tested my code (label 'loop which doesn't work' below) at the top of the template file and it pulls the correct data, however when I place it inside the template where I want it, it pulls data from the post it is on.

I suspect this problem might be to do with the function 'your_custom_loop' and needing to set up another function like this for my new loop. Help much appreciated thank you

<?php
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'your_custom_loop' );

function your_custom_loop() { ?>

<article itemtype="http://schema.org/CreativeWork" itemscope="itemscope" class="post-<?php print $pageid; ?> page type-page status-publish entry">
<div class="entry-content" itemprop="text">

<?php
do_action( 'genesis_entry_header' );
?>


// Loop which doesn't work: 
<?php

$post_object = get_field('resource_college');

if( $post_object ): 

    // override $post
    $post = $post_object;
    setup_postdata( $post ); 

    ?>
    <div>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
    </div>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

// Regular loop
<?php

if(have_posts()) : while(have_posts()) : the_post();
hyp0thetical
  • 300
  • 6
  • 19
  • Does it work if you put `global $post;` at the top of your function? I assume it's pulling in the correct value of the `resource_college` field (ie the one associated with the current post)? – Hobo Mar 10 '16 at 07:15
  • Yes it does work if placed at the top – hyp0thetical Mar 10 '16 at 07:17
  • 1
    Cool; so problem solved? – Hobo Mar 10 '16 at 07:19
  • I want it to display inside the regular template though - if I put it at the top it displays even before the header! – hyp0thetical Mar 10 '16 at 07:20
  • No, I meant "Does it work if you put the code `global $post;` after `function your_custom_loop() {` (before the `?>`)"? – Hobo Mar 10 '16 at 07:28
  • Hey I tried and it still returns the title of the current post `function your_custom_loop() { $post_object = get_field('resource_college'); if( $post_object ): // override $post $post = $post_object; setup_postdata( $post ); the_title(); the_field('field_name'); wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly endif; ?>` – hyp0thetical Mar 10 '16 at 07:39
  • That code doesn't have the string `global $post;` anywhere in it. I'm not certain it'll change anything, I'm just asking if it makes a difference if you literally insert the string `global $post;` at the top of you function. If that's still not clear I'll post an answer with the code, but I'm not certain it'll make a difference. Other possibility is `$post_object = get_field('resource_college');` isn't doing what you expect. What's `var_dump($post_object);` show? – Hobo Mar 10 '16 at 08:45

0 Answers0