Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Konrad Pilch
Konrad Pilch
2,435 Points

After my portfolio code, anything udner related to wp breaks

HI

So after this code

        <div class="row">                   


            <?php 
              $temp = $wp_query; 
              $wp_query = null; 
              $wp_query = new WP_Query(); 
              $wp_query->query('showposts=6&post_type=portfolio'.'&paged='.$paged); 
            ?> 


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

            <div class="col-md-4 col-sm-6 portfolio-item">
              <a href="<?php echo get_field('portfolio_site_url'); ?> " target="_blank" class="portfolio-link" data-toggle="modal">
                <div class="portfolio-hover">
                    <div class="portfolio-hover-content">
                        <i class="fa fa-plus fa-3x"></i>
                    </div>
                </div>
                 <img style="background-image:url(<?php echo get_field('portfolio_image_post'); ?>);">
              </a>

              <div class="portfolio-caption">
                <h4><a href="<?php echo get_field('portfolio_site_url'); ?>" target="_blank"><?php the_title(); ?></a></h4>
                <p class="text-muted"></p>
              </div>
            </div><!-- col-* -->


            <?php endwhile;?>

            <?php 
                 $wp_query = null; 
                 $wp_query = $temp;  // Reset
            ?>

        </div><!-- /row -->
    </div><!-- /container -->
</div>

Mainly this:

 <?php 
              $temp = $wp_query; 
              $wp_query = null; 
              $wp_query = new WP_Query(); 
              $wp_query->query('showposts=6&post_type=portfolio'.'&paged='.$paged); 
            ?> 

It breaks my :

<!-- Quote -->
<div class="quote section-padding" style="background-image: url(<?php echo get_field('quote_image_background'); ?>);">
    <div class="container"> 
        <div class="row">
            <div class="col-lg-12">
              <h3><?php echo get_field('quote_title'); ?></h3>
                <h4><?php echo get_field('quote_author'); ?></h4>
            </div><!-- /col-lg-12 -->
        </div><!-- /row -->
    </div><!-- /container -->
</div><!-- /quote -->

And wont show me fields that im echo out. How come?

This code works perfeclty on my portfolio page.

1 Answer

Colin McGraw
Colin McGraw
15,337 Points

I would try a few different things here... one would be passing your loop arguments to the WP_Query constructor instead of using the query() method.

$wp_query = new WP_Query(array(
  'posts_per_page' => 6,
  'post_type' => 'portfolio',
  'paged' => $paged
));

another would be to leave your loop the same but pass the post ID as the second argument to get_field()

get_field('field_key', $post->ID);

It sounds like the underlying problem has something to do with how the $post global is set and how ACF recongnizes it, but I don't know enough about how WP_Query and ACF work under the hood to give an explanation...hope this helps though.

Konrad Pilch
Konrad Pilch
2,435 Points

Thank you but, None of it works.

Konrad Pilch
Konrad Pilch
2,435 Points

After 24hours, from some Zac video, i remembered that he said to close the WP Queries because it can cause some problems, yay.

Solution re-solverd

wp_reset_query(); ?