如何在里面使用Function变量遍历PHP类

时间:2016-03-30 07:31:39

标签: php wordpress

目标:我的目标是创建一个多次执行的函数,并将ascript添加到页面的页脚。

这有效,但现在我希望它能用于变量。如何将它们放入第二个函数($ A,$ B,$ C)并给它们输入? 这是我的代码:

class myClass {
    //Put it in the footer
    public function __construct() {
        add_action( 'genesis_after_footer', array( $this, 'myFunction' ) );
    }


    //myFunction
     public function myFunction($A, $B, $C) {

    echo'<script>
     $(document).ready(function() {


      $("#gallery_'.$A.'").owlCarousel({

          autoPlay: false, //Set AutoPlay to 3 seconds

          items : '.$B.',
          itemsDesktop : [1199,'.$B.'],
          itemsDesktopSmall : [979,'.$B.'],
          itemsTablet: [768,'.$C.']

      });

    });
    </script>';

    }
}

要运行我现在使用的功能:

$run = new WP_Docs_Class();

我希望有类似的东西:

$run = new WP_Docs_Class($A, $B, $C);
or
$run = new WP_Docs_Class(120, 9, 2);

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

只需将它们存储到类变量

class myClass {
    private $A, $B, $C;
    //Put it in the footer
    public function __construct($A, $B, $C) {
        $this->A = $A;
        $this->B = $B;
        $this->C = $C;
        add_action( 'genesis_after_footer', array( $this, 'myFunction' ) );
    }


    //myFunction
     public function myFunction() {

    echo'<script>
     $(document).ready(function() {


      $("#gallery_'.$this->A.'").owlCarousel({

          autoPlay: false, //Set AutoPlay to 3 seconds

          items : '.$this->B.',
          itemsDesktop : [1199,'.$this->B.'],
          itemsDesktopSmall : [979,'.$this->B.'],
          itemsTablet: [768,'.$this->C.']

      });

    });
    </script>';

    }
}

答案 1 :(得分:0)

试试这个;)

在WP中,我们使用do_action

调用添加的操作
class myClass{

  //Put it in the footer
  public function __construct(){
    add_action('genesis_after_footer', array(
      $this,
      'myFunction'));
  }

  //myFunction
  public function myFunction($data){

    echo'<script>
     $(document).ready(function() {
      $("#gallery_' . $data[0] . '").owlCarousel({
          autoPlay: false, //Set AutoPlay to 3 seconds
          items : ' . $data[1] . ',
          itemsDesktop : [1199,' . $data[1] . '],
          itemsDesktopSmall : [979,' . $data[1] . '],
          itemsTablet: [768,' . $data[2] . ']
      });
    });
    </script>';
  }

}

在课程代码中调用此更新后

do_action( 'genesis_after_footer', array( $A, $B, $C ) );

OR

do_action( 'genesis_after_footer', array( 120, 9, 2 ) );