“禁止覆盖WordPress全局变量”虽然代码似乎没问题

时间:2018-05-01 11:04:45

标签: php wordpress

ERROR   Overriding WordPress globals is prohibited on line setup_postdata( $GLOBALS['post'] =& $post_object );

这会产生

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class ResizeCursorTest {
  public JComponent makeUI() {
    JTable table = new JTable(new DefaultTableModel(10, 3));

    TableColumnModel colModel = table.getColumnModel();
    colModel.getColumn(0).setMinWidth(15);
    colModel.getColumn(0).setMaxWidth(15);
    colModel.getColumn(0).setPreferredWidth(15);
    colModel.getColumn(0).setResizable(false);

    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(table));
    return p;
  }
  public static void main(String... args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new ResizeCursorTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

我正在为thmeforest.net制作一个主题

1 个答案:

答案 0 :(得分:0)

这是错误的做法。您应该对该产品使用wp_query。

  add_action('woocommerce_after_single_product_summary', 'show_cross_sell_in_single_product', 30); 

function show_cross_sell_in_single_product(){
        $crosssells = get_post_meta( get_the_ID(), '_crosssell_ids',true);
        $args = array( 
            'post_type' => 'product', 
            'posts_per_page' => -1, 
            'post__in' => $crosssells 
            );
        $products = new WP_Query( $args );
        if( $products->have_posts() ) : 
            echo '<div class="cross-sells"><h2>Cross-Sells Products</h2>';
            woocommerce_product_loop_start();
            while ( $products->have_posts() ) : $products->the_post();
                wc_get_template_part( 'content', 'product' );
            endwhile; // end of the loop.
            woocommerce_product_loop_end();
            echo '</div>';
        endif;
        wp_reset_query(); }

代码说明:当你把do_action()写成简单时,使用这个函数添加所有追加销售的产品。