如何在wordpress插件中传递参数

时间:2015-12-19 04:16:16

标签: php wordpress wordpress-plugin wordpress-theming

我试图在函数中传递参数并在wordpress插件中打印输出我无法在这里传递参数' s我的代码

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;

}
}

1 个答案:

答案 0 :(得分:0)

如果您希望函数外的任何变量可用于您的函数,请使用'使用'像这样的关键字:

$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