如何在wordpress插件中传递参数 [英] how to pass parameter in wordpress plugin

查看:34
本文介绍了如何在wordpress插件中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to pass argument in function and print the output in wordpress plugin i was not able to pass parameter here's my code

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;

}
}

推荐答案

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

If you want any variable outside your function available to your function then use closures by using 'use' keyword like this:

$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

这篇关于如何在wordpress插件中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆