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

Saving PHP Variable in WordPress Loop to be Utilized Outside the Loop

What I'm trying to do is basically have a list of products (custom post type) that is looped through. Within the loop, there is an "Enquiry" button which opens up a Gravity Form in Lightbox. I want to dynamically input the product title (custom post type title) into the Gravity Forms. I've got that done, however, I can't seem to assign get_the_title() to a variable and then call that variable outside of the loop later within the Gravity Forms function.

Here is my code:

''' <?php

        $posts = get_field('selected_products');

        if( $posts ): ?>
            <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
                <?php setup_postdata($post); ?>

                    <div class="promo-image">
                        <?php if( get_field('product_image') ): ?>

                            <img src="<?php the_field('product_image'); ?>" />

                        <?php endif; ?>
                    </div>

                    <div class="promo-desc">

                        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

                        <?php $product_name = get_field('product_name'); ?>
                        <?php if($product_name) { ?>
                            <p><strong><?php echo $product_name; ?></br>
                        <?php } ?>
                        <?php $sku = get_field('sku'); ?>
                        <?php if($sku) { ?>
                            #<?php echo $sku; ?></strong></br>
                        <?php } ?>
                        <?php $msrp = get_field('msrp'); ?>
                        <?php $saleprice = get_field('sale_price'); ?>
                        <?php if($saleprice) { ?>
                            <?php if($msrp) { ?>
                                <del><span style="color:#4CC417;">MSRP: $<?php echo $msrp; ?> each</span></strong></del></br>
                            <?php } ?>
                                <span style="color:#FF0000;"><strong>SALE PRICE: $<?php echo $saleprice; ?> each</span></strong></p>
                        <?php } else { ?>
                            <?php if($msrp) { ?>
                                <span style="color:#4CC417;">MSRP: $<?php echo $msrp; ?> each</span></strong></p>
                            <?php } ?>
                        <?php } ?>
                        <?php the_excerpt(); ?>

                        <a href="#fancyboxID-1" class="fancybox button_enquiry" onclick="SAVE THE VARIABLE TO GET_THE_TITLE() HERE">
                        <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/product-enquiry.jpg"></a>

                    </div>

                    <div style="clear:both;"></div>
                    </br><hr>

            <?php endforeach; ?>
            <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
        <?php endif; ?>

        <div style="display:none" class="fancybox-hidden"><div id="fancyboxID-1">
            <?php gravity_form(2, true, false, false, array('product_enquiry' => PHP VARIABLE REFERRING TO CHOSEN TITLE GOES HERE), false); ?>
        </div></div>

'''

Your help would be much appreciated.

1 Answer

Gareth Borcherds
Gareth Borcherds
9,372 Points

So it seems you only need the one, why don't you just use the outside the loop version of this to get the title. From the documentation https://codex.wordpress.org/Function_Reference/get_the_title we read that outside of the post loop you can simply pass the ID of the post you want and echo that.

So, I would figure out which post ID you want and then pass it like the docs say.

Also, I don't know what your logic is for selecting the post that gets put in the gravity form, but you should probably put that in an if statement in the loop so that whatever variable you set keeps your post ID for outside the loop.