2010-11-26 6 views
0

나는 지난 며칠 동안 Wordpress 테마 프로젝트를 작업 해왔고 OOP (나는 주로 PHP 스크립터가 아니라 테마 개발자)를 사용하여 동적 옵션 페이지를 올바르게 실행하는 방법에 주저하고 있습니다. 함수가 foreach 루프에서 필요한만큼 여러 번 호출 될 것입니다 때문에이 코드 예제에서 foreach 내에서 함수 호출?

<?php 
    $footerpage = new optionpage; 
    $footerpage->title = 'Footer'; 
    $footerpage->titleprint = ' Footer Options'; 
    $footerpage->slug = 'footer'; 
    $footerpage->html = array(
'1' => array(
'type' => 'textarea', 
'class' => 'large-text', 
'name' => 'html', 
'title' => 'HTML', 
'description' => 'Type in whatever HTML you\'d like to see in the footer here:', 
), 
'2' => array(
'type' => 'input', 
'class' => 'large-text', 
'name' => 'background-color', 
'title' => 'Background Color', 
'description' => ' Choose a Background Color:' 
), 

); 

    class optionpage { 

public $title; 
public $titleprint; 
public $slug; 
public $html = array(); 

...... 
...... 
...... 

public function ab_settings() { 

register_setting($this->slug, 'ab_options'); 
add_settings_section('ab_section', '', array(&$this, 'ab_do_titleprint'), 'ab_' . $this->slug . '_options', 'ab_options'); 

foreach ($this->html as $key => $html) { 
add_settings_field($key, $html['title'], array(&$this, 'ab_do_htmlprint'), 'ab_' . $this->slug . '_options', 'ab_section'); 
} 

} 


public function ab_do_htmlprint() { 
$html = $this->html[$key]; 
?> 

<p><?php echo $html['description'] ?></p> 
<<?php echo $html['type'] ?> 
id="<?php echo $html['id'] ?>" 
class="<?php echo $html['class'] ?>" 
name="<?php echo $html['name'] ?>"> 
<?php get_option ($html['name'])?> 
</<?php echo $html['type'] ?>> 
<?php 
} 

...... 
...... 
?> 

, 나는,가 호출되어있어 foreach는 식을 인식하는 기능 ab_do_htmlprint를 얻기 위해 노력하고있어.

변수 이름에 함수를 추가하는 것과 같은 여러 가지 작업을 시도했지만 동일한 코드의 여러 함수가 필요합니다. 다른 이름을 사용해야합니다. 나는 또한 다양한 변수를 참조로 전달하려고했지만, 그 중 하나도 작동하지 않았고 필요하다면 올바르게 수행하지 못했을 수도 있습니다.

어쨌든이 작업을 효율적으로 수행 하시겠습니까?

+0

나는 따라갈 수 없다. ab_do_htmlprint의 동작은 호출 된 위치에 따라 어떻게 달라야합니까? 나의 직감은 간단한 논증이지만 구체적인 내용을 이해하지 못한다. –

+0

내가 원하는 것은 ab_do_htmlprint 함수가 다차원 배열 안에있는 각 옵션의 HTML을 출력하도록하는 것이다.foreach 루프 내에서 ab_do_htmlprint가 호출 될 때마다 각 옵션이 내가 작성한 옵션 페이지에 표시됩니다. 말이 돼? – James

답변

0

로 대신

public function ab_do_htmlprint() { 
$html = $this->html[$key]; 
?> 

<p><?php echo $html['description'] ?></p> 
<<?php echo $html['type'] ?> 
id="<?php echo $html['id'] ?>" 
class="<?php echo $html['class'] ?>" 
name="<?php echo $html['name'] ?>"> 
<?php get_option($html['name'])?> 
</<?php echo $html['type'] ?>> 
<?php 
} 

HEREDOC 표기 또는 순수 PHP 문자열

예)를 사용해보십시오. add_settings_field는, 그것에 대해 매개 변수가이 수행 한 후

add_settings_field($key, $html['title'], array(&$this, 'ab_do_htmlprint'), 'ab_' . $this->slug . '_options', 'ab_section', array($key)); 

:

public function ab_do_htmlprint($key) { 
    $html = $this->html[$key]; 

+0

위 코드를 수정하려고 시도했지만 "경고 : call_user_func (배열) [function.call-user-func] : 첫 번째 인수는 유효한 콜백이어야합니다."오류가 발생합니다. – James

+0

WP가 add_settings_field의 마지막 인수가 이미 배열에 있다는 것을 인식 했으므로 결국에는 배열 코드를 제거해야했습니다. 이것은 매우 가깝기 때문에 감사합니다. – James

0

Wordpress API에 익숙하지 않으므로 몇 가지 가정을 확인하십시오. (나는 이것이 답이 아니라는 것을 알고 있지만, 가독성을 위해 논평을하고 싶지 않다)

1) ab_settings()은 어딘가에 불려 집니까?
2) 문제가있는 섹션이 있습니까?

foreach ($this->html as $key => $html) { 
    add_settings_field($key, $html['title'], array(&$this, 'ab_do_htmlprint'), 
    'ab_' . $this->slug . '_options', 'ab_section'); 
} 

3) $key 마술로 볼 수 있습니다 또는 당신은 인수로 제공해야합니까?

public function ab_do_htmlprint() { 
    $html = $this->html[$key];` 

또한, 귀하의 ab_do_htmlprint 함수의 형식은 읽기가 매우 어려운 오류의 원인이 될 수 있습니다. 당신이 ab_do_htmlprint하는 인수를 전달해야, 만약 내가 제대로 이해하고

public function ab_do_htmlprint() { 
    $html = $this->html[$key]; 
    $output = "<p>{$html['description']}</p>"; 
    $output .= "<{$html['type']} id=\"{$html['id']}\""; 
    $output .= " class=\"{$html['class']}\""; 
    $output .= " name=\"{$html['name']}\">"; 
    get_option($html['name']); 
    $output .= "</{$html['type']}>"; 
    echo $output; 
} 
+0

1) 예, 죄송 합니다만 전체 스크립트를 게시하지 않았습니다. add_action 함수를 통해 호출됩니다. – James

+0

2) 필자가 잘 모르는 섹션 일 수도 있습니다. 몇 가지 요소를 변경하는 등 몇 가지 시도를했습니다. – James

+0

3) ab_do_htmlprint 내부의 코드는 foreach 내부의 끝에 $ 키를 추가하려고 했으므로 정상적으로 작동합니다. 각 개별 함수를 올바르게 호출 할 수있었습니다. ab_do_html1(); – James

1

내가 제대로 이해한다면 당신은 당신이에 그룹에 표시 할 값의 배열을 가지고 옵션으로 관리자 화면을 엽니 다. http://swiftthemes.com/forums/showthread.php?383-SWIFT-on-a-WordPRess-MU-install/page2

추신을 :

아마 가장 짧은 예를 들어 내가 여기에 게시 것입니다 http://wordpress.stackexchange.com에 게시물 WordPress 질문 : 더 많은 WordPress 전문가!

+0

기본적으로 설정 API를 사용하고 객체 지향 관점에서이를 수행하려고하므로 새 옵션을 추가 할 때마다 add_settings_field 코드를 다시 복사하여 붙여 넣지 않아도됩니다. – James

관련 문제