2014-03-06 1 views
1

저자는 다중 작성자 사이트에서 각 작성자에 대한 사용자 정의 작성자 페이지를 작성하려고합니다. (저는 Wordpress 용 Genesis Framework를 사용하고 있습니다.)PHP (Wordpress, Genesis)에서 인수 전달 방법

두 가지 작업이 있습니다. 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)는 절대적으로 nada입니다. 그것은 세계에서 가장 쉬운 것 같아요, 그냥 그 함수에 문자열을 전달하고 그것을 출력하고 싶습니다. 이것은 한 줄짜리 기능입니다. 하지만 작동하지 않습니다. "Hello, world!"와 같은 상수를 출력 할 수 있습니다. 또는 내가 원하는 곳이면 어디든간에, 그러나 나는 그 논점을 통과 할 수 없다.

저는 이미 창세기 포럼 (운이 없다)에 대한 도움을 요청했으며 연구를 수행했습니다. 나는이 두 페이지가 도움이되었다고하지만, 내 문제가 해결되지 않은 : 사전에 어떤 도움 can I pass arguments to my function through add_action? http://codex.wordpress.org/Function_Reference/do_action

감사합니다.

+0

당신은 do_action와 함수를 호출하고 또한 genesis_before_loop 후크를 실행하려고합니까? 내 추측은 후크를 통해 실행 중이고 전달 된 매개 변수가 없습니다. 후크를 통해 함수를 실행하려고하면 매개 변수를 제거하고 함수 본문에서 작성자 정보를 검색 할 수 있습니까? – clhereistian

+0

>> do_action을 사용하여 함수를 호출하고 genesis_before_loop 훅으로 실행하려고합니까? << 내가 조사한 바에 따르면 매개 변수를 전달하려는 경우 add_action과 do_action을 먼저 수행해야합니다 여기서 매개 변수는 실제로 do_action에서옵니다. 그래서, 그게 내가하려고하는 것입니다. >> 매개 변수를 제거하고 함수 본문에서 작성자 정보를 검색 할 수 있습니까?<< 3 주 전에 시작한 곳이기는하지만 그 방법으로는 전혀 작동하지 않았습니다. $ curauth 라인이 함수의 맥락에서 작동하지 않거나 디스플레이 문제인지 여부는 알 수 없습니다. –

답변

2

내 연구에 따르면 간단한 대답은 사용자 정의 함수에 매개 변수를 전달할 수 없다는 것입니다. 그것은 불가능합니다.

그러나 전역 변수로 전달할 변수를 선언 한 다음 해당 전역 변수에 액세스하는 함수를 작성할 수 있습니다. 나는 그것을 시험해 보았다.

<?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

0

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

나 출력을 알려주십시오.

+0

이것은 함수에서 얻은 출력입니다. 저자 정보 함수 - param []의 값 "실행중인 작업"텍스트가 나머지 디버그 텍스트와 함께 나타납니다. 그러나 이것이 어떻게 도움이되는지 잘 모르겠습니다. do_action이 아닌 다른 함수에 매개 변수를 전달하는 방법을 알지 못합니다. 정말 감사드립니다. –

+0

"저자 정보 있음"은 한 번 또는 두 번만 표시됩니까? 이와 관련하여 "행동을 취하는"것은 어디에 있습니까? – clhereistian

+0

"저자 정보 있음 ..."은 한 번만 표시됩니다. genesis_before_loop 위치에 표시됩니다. "실행중인 작업"행은 시작 HTML 태그 앞에 페이지 맨 위에 표시됩니다. –