【问题标题】:how to pass parameter in wordpress plugin如何在wordpress插件中传递参数
【发布时间】:2015-12-19 04:16:16
【问题描述】:

我正在尝试在函数中传递参数并在 wordpress 插件中打印输出我无法传递参数这是我的代码

global $postidd;
$postidd=$_REQUEST["postid"];
function getcontent($postidd)
{
   // do something with the args


if($postidd)
{

      $args12 = array(
                    'p'=>15,
                    'post_type' => 'offers',
                    'orderby' => 'title',
                    'order' => 'ASC'
                );
      $the_query12 = new WP_Query( $args12 );           
      if ( $the_query12->have_posts() ) : while ( $the_query12->have_posts() ) : $the_query12->the_post(); 

     return   $postidd;

      endwhile;endif;     

         return   $postidd;

}
}

【问题讨论】:

  • 你需要多解释一下这个问题。你在哪里调用这个函数?

标签: php wordpress wordpress-theming


【解决方案1】:

如果您希望函数之外的任何变量可用于您的函数,则通过使用 'use' 关键字来使用闭包,如下所示:

$postidd=$_REQUEST["postid"];
function getcontent($somevariable) use ($postidd) {
....

or 
declare it global inside the function like this:
function getcontent($somevariable) {
global $postidd


but using global is considered a bad practice

【讨论】:

    猜你喜欢
    • 2014-03-21
    • 2014-04-09
    • 2016-04-21
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多