Upgrading to PHP 7.1

Sage

New Member
#1
I have a quick question about upgrading my theme file to PHP 7.1. I know it's easy to do, but I don't understand PHP.

It is currently in an old compatibility of 5.3 and this is the only error in 2 parts of my theme: "ERROR | Using a call-time pass-by-reference is deprecated since PHP 5.3 and prohibited since PHP 5.4."

I would ask my theme developer, but last time they did something they deleted all of my custom CSS code. Very awful support from them and I can't trust them anymore.

Both problem lines use this code setup_postdata( $GLOBALS['post'] =& $post_object );

THEME FILE #1:
Mã:
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
if ( $upsells ) : ?>
<div class="ps-section pb-50">
    <div class="ps-container">
        <div class="ps-section__header text-center">
            <h2 class="ps-section__title"><?php esc_html_e('You may also like&hellip;', 'xuper');?></h2>
        </div>

        <div class="ps-section__content">
            <div class="ps-slider--center owl-slider products" data-owl-auto="true" data-owl-loop="true" data-owl-speed="5000" data-owl-gap="30" data-owl-nav="true" data-owl-dots="false" data-owl-item="4" data-owl-item-xs="1" data-owl-item-sm="2" data-owl-item-md="3" data-owl-item-lg="4" data-owl-duration="1000" data-owl-mousedrag="on" data-owl-nav-left="&lt;i class='ps-icon-arrow-left'&gt;&lt;/i&gt;" data-owl-nav-right="&lt;i class='ps-icon-arrow-right'&gt;&lt;/i&gt;">

                <?php foreach ( $upsells as $upsell ) : ?>

                    <?php
                        $post_object = get_post( $upsell->get_id() );

                        setup_postdata( $GLOBALS['post'] =& $post_object );

                        wc_get_template_part( 'content', 'product' ); ?>

                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>
` `
Mã:
                    <?php
                        $post_object = get_post( $related_product->get_id() );

                        `setup_postdata( $GLOBALS['post'] =& $post_object );

                        wc_get_template_part( 'content', 'product' ); ?>

                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>
wp_reset_postdata();

Thanks for any help, I really appreciate it!
 

Admin

Administrator
Thành viên BQT
#2
setup_postdata( $GLOBALS['post'] =& $post_object ); is doing an assignment by reference, which is a strange way to do this.

In this line of code, you're referencing WordPress' setup_postdata function, which basically populates a bunch of WP-centric variables behind the scenes. Typically you just invoke this with: setup_postdata($post_object) and you're good to go.

And don't forget to call wp_reset_postdata() when you're loop is closed, which resets the variables for WP other loops you may run elsewhere on the site.
 
OP
OP
S

Sage

New Member
#3
My wordpress said it glitched while making the changes, but they stuck and I was able to update the PHP. Thank you!
 
Top