我正在尝试为我的多作者网站上的每个作者创建一个自定义作者页面 . (我正在使用Genesis Framework for Wordpress . )
有两个任务:1 . 从数据库中动态检索作者信息2.使用钩子显示它 . 我对第1部分没有任何问题,但是尝试通过钩子传递信息以使其显示在页面上我想要的位置让我感到难过 .
我的方法是创建一个author.php文件 . 这是我到目前为止:
<?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>
<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();
我知道我正在正确检索作者姓名和作者生物,并将其格式化以便正确显示,因为测试行输出了我想要的信息 . (网站在这里可见:http://difficultrun.nathanielgivens.com/author/nathaniel/)
但是,实际函数(include_author_info)绝对是nada . 这似乎是世界上最简单的事情,我只想将一个字符串传递给该函数并输出它 . 这是一个单行功能 . 但它不会起作用 . 我可以输出一个常量,如“你好,世界!”或者无论我想要去哪里,但我无法通过论证 .
我've already asked for help on the Genesis forums (no luck) and done research. I found these two pages helpful, but they didn' t解决了我的问题:can I pass arguments to my function through add_action? http://codex.wordpress.org/Function_Reference/do_action
在此先感谢您的帮助 .
2 回答
根据我的研究,简单的答案是,当您通过钩子添加该功能时,您无法将参数传递给自定义函数 . 这是不可能的 .
但是,您可以声明要作为全局变量传递的变量,然后编写函数以访问该全局变量 . 我试过了,它起作用了 . 这是我的完整代码:
我找到的文章帮助我完成了这项工作:http://wordpress.org/support/topic/pass-variable-to-function-in-add_action
我仍然认为genesis_before_loop动作在没有你的参数的情况下开始 . 尝试在那里输出一些调试信息 .
并在do_action上方添加一行
让我知道输出 .