【发布时间】:2014-04-09 07:26:11
【问题描述】:
我正在尝试为我的多作者网站上的每个作者创建一个自定义作者页面。 (我正在使用 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><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();
我知道我正在正确检索作者姓名和作者简介,并对其进行格式化以正确显示,因为测试行输出的正是我想要的信息。 (网站可见:http://difficultrun.nathanielgivens.com/author/nathaniel/)
但是,实际的函数 (include_author_info) 绝对没有问题。这似乎是世界上最简单的事情,我只想将一个字符串传递给该函数并输出它。这是一个单行函数。但它不会起作用。我可以输出像“Hello, world!”这样的常量。或任何我想要它去的地方,但我无法传递参数。
我已经在 Genesis 论坛上寻求帮助(运气不好)并进行了研究。我发现这两页很有帮助,但它们并没有解决我的问题: can I pass arguments to my function through add_action? http://codex.wordpress.org/Function_Reference/do_action
提前感谢您的帮助。
【问题讨论】:
-
您是否使用 do_action 调用该函数并尝试使用 genesis_before_loop 钩子执行它?我的猜测是它通过钩子执行并且没有传递任何参数。如果你要通过钩子执行你的函数,你可以删除参数并在你的函数体中检索作者信息吗?跨度>
-
>>您是否使用 do_action 调用该函数并尝试使用 genesis_before_loop 钩子执行它?>你能删除参数并在你的函数体中检索作者信息吗?