【问题标题】:How to pass argument in PHP (Wordpress, Genesis)如何在 PHP 中传递参数(Wordpress、Genesis)
【发布时间】: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 钩子执行它?>你能删除参数并在你的函数体中检索作者信息吗?

标签: php wordpress


【解决方案1】:

根据我的研究,简单的答案是当您通过钩子添加该函数时,您不能将参数传递给自定义函数。这是不可能的。

但是,您可以将要传递的变量声明为全局变量,然后编写函数来访问该全局变量。我试过了,它奏效了。这是我的完整代码:

<?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(); 

我发现帮助我完成这项工作的文章在这里:http://wordpress.org/support/topic/pass-variable-to-function-in-add_action

【讨论】:

    【解决方案2】:

    我仍然认为 genesis_before_loop 动作在没有您的参数的情况下触发。尝试在那里输出一些调试信息。

    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);
    

    让我知道输出。

    【讨论】:

    • 这是我从函数中得到的输出: In author info function - value of param [] 文本“执行操作”也与调试文本的其余部分一起显示。不过,我不确定这有什么帮助。除了 do_action 之外,我不知道如何将参数传递给我的函数。非常感谢您的帮助。
    • “在作者信息中...”是否只显示一次或多次?与此相关的“执行行动”在哪里?
    • “在作者信息中...”显示一次。它显示在 genesis_before_loop 位置。 “执行操作”行显示在页面的最顶部,在开始的 html 标记之前。
    • OK - 如果您的操作有效,它将再次显示。这意味着在错误的时间自动执行您正在做的事情。你为什么不尝试创建自己的动作而不是挂钩到创世记呢?
    • 我不知道如何创建自己的动作来显示在正文的适当位置。我想我只是认为这就是 Genesis 的全部意义所在,它让您可以轻松地连接到他们的框架中,我很震惊,当您连接到它时,您不能做像传递参数这样简单的事情。如果我真的无法弄清楚这一点,我可能只会为每个作者创建单独的文件并以这种方式硬编码该死的信息,但我无法相信没有更简单的方法来传递参数。 :-(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 2018-03-25
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多