如何在PHP中传递参数(Wordpress,Genesis)
我正在尝试为我的多作者网站上的每个作者创建一个自定义作者页面。(我正在使用Wordpress的Genesis框架。) 有两项任务:1。从数据库2动态检索作者信息。使用钩子显示它。我对第1部分没有问题,但试图通过一个钩子传递信息,使其显示在页面上我想要的位置,这让我感到困惑 我的方法是创建一个author.php文件。这就是我到目前为止所做的:如何在PHP中传递参数(Wordpress,Genesis),php,wordpress,Php,Wordpress,我正在尝试为我的多作者网站上的每个作者创建一个自定义作者页面。(我正在使用Wordpress的Genesis框架。) 有两项任务:1。从数据库2动态检索作者信息。使用钩子显示它。我对第1部分没有问题,但试图通过一个钩子传递信息,使其显示在页面上我想要的位置,这让我感到困惑 我的方法是创建一个author.php文件。这就是我到目前为止所做的: <?php // this is the function I want to use to output my stuff. function i
<?php
// this is the function I want to use to output my stuff.
function include_author_info($author_info) {
echo $author_info;
}
// now we want to add this action to the Genesis loop
add_action( 'genesis_before_loop', 'include_author_info', 5, 1);
// now I populate the data I will feed to the function
// first: set curauth based on the current author
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
// next: store the curauth name
$user_name = $curauth->display_name;
// additionally: store the user bio for the curauth
$user_bio = $curauth->user_description;
// concatenate the name and bio into one formatted string
$formatted_user_info = "<h2> " . $user_name . "</h2><br /><p>" . $user_bio . "</p>";
// for testing purposes, I output the concatenated and formatted name/bio
echo $formatted_user_info;
// finally, I execute the action
do_action('include_author_info', $formatted_user_info);
//* Run the Genesis loop
genesis();
我仍然认为genesis\u before\u循环动作在没有参数的情况下启动。尝试在那里输出一些调试信息
function include_author_info($author_info) {
echo "In author info function - value of param [" . $author_info . "]";
}
在do_action的正上方画一条线
echo "executing action";
// finally, I execute the action
do_action('include_author_info', $formatted_user_info);
让我知道输出。根据我的研究,简单的答案是,在通过挂钩添加自定义函数时,不能将参数传递给该函数。这是不可能的
但是,可以将要传递的变量声明为全局变量,然后编写函数以访问该全局变量。我试过了,成功了。这是我的完整代码:
<?php
// now I populate the data I will feed to the function
// first: set curauth based on the current author
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
// next: store the curauth name
$user_name = $curauth->display_name;
// additionally: store the user bio for the curauth
$user_bio = $curauth->user_description;
// create a global variable my function can access
global $formatted_user_info;
// concatenate the name and bio into one formatted string
$formatted_user_info = "<h2> " . $user_name . "</h2><br /><p>" . $user_bio . "</p>";
// for testing purposes, I output the concatenated and formatted name/bio
// echo $formatted_user_info;
// now we want to add this action to the Genesis loop
add_action( 'genesis_before_loop', 'include_author_info');
// this is the function I want to use to output my stuff.
function include_author_info($author_info) {
global $formatted_user_info;
echo $formatted_user_info;
}
//* Run the Genesis loop
genesis();
您是否使用do_操作调用函数,并尝试使用genesis_before_循环钩子执行它?我猜它是通过钩子执行的,没有任何参数传递给它。如果你要通过钩子执行你的函数,您可以删除参数并在函数体中检索作者信息吗?>>您是否使用do_action调用函数,并尝试使用genesis_before_循环挂钩执行它?这是我从函数中获得的输出:在author info function中-param[]的值文本“executing action”还将与调试文本的其余部分一起显示。不过,我不确定这有什么帮助。除了do_操作之外,我不知道如何将参数传递给我的函数。非常感谢您的帮助。“作者信息中…”是否只显示一次或多次?与此相关的“执行操作”在哪里?“作者信息中…”只显示一次。它显示在genesis_前_循环位置。“executing action”行显示在页面的最顶部,在打开html标记之前。OK-如果您的操作正常,它将再次显示。这意味着你在错误的时间自动执行你正在做的事情。你为什么不试着创造你自己的动作,而不是挂在创世记上呢?我不知道如何创造我自己的动作,在身体的适当位置展示。我想我只是认为这就是《创世纪》的全部意义,它让你可以轻松地连接到他们的框架中,我很震惊你不能做一些简单的事情,比如在连接到它时传递一个参数。如果我真的搞不懂这一点,我可能会为每个作者创建单独的文件,并以这种方式硬编码该死的信息,但我真不敢相信没有更简单的方法来传递参数:-(