2014-01-23 1 views
0

나는 WordPress 용으로 만든이 짧은 코드로 적절한 코드를 출력하는 데 문제가있다. 내 짧은 코드 [onecol] 두 개의 값 중 하나를 사용하여 특성을 받아들이려고 노력하고있어 "첫 번째"및 "마지막", 그리고 아무 속성이 입력되지 않으면 나는 그것을 기본값을 사용하고 싶습니다. 그리고 나는이와 비슷한 출력을 렌더링 할 :

<div class="onecol first clearfix">Hello World</div> 

을 그리고 나는 단축 코드는 다음과 같이 할 :

function one_col($atts, $content = null) { 
    extract(shortcode_atts(array(
     'class' => 'col' 
    ), $atts)); 

    $depth = 0; 
    $indent = str_repeat("\t", $depth); 

    return "\n$indent<div class=\"onecol " . esc_attr__($class) . " clearfix\">\n" . $content . "$indent</div>\n"; 
} 

add_shortcode('onecol', 'one_col'); 
:

[onecol col="first"]Hello World[/onecol] 

이것은 내가 지금까지 가지고있는 코드입니다

지금까지 출력 :

<div class="onecol col clearfix>Hello World</div> 

내가 뭘 잘못 했니?

+0

HTML의 들여 쓰기 시간을 낭비하지 마십시오. 공백은 의미가 없다. – Phil

답변

0

단순히 속성 이름을 잘못 지정하면됩니다. 이것은 PHP 5.4 배열 구문과 PHP 5.3 폐쇄를 사용하여 ...

add_shortcode('onecol', function($atts, $content = null) { 
    extract(shortcode_atts([ 
     'col' => 'first' // this is your default "col" attribute value 
    ], $atts)); 

    return sprintf('<div class="onecol %s clearfix">%s</div>', 
     htmlspecialchars($col), do_shortcode($content)); 
}); 

을이보십시오. 이전 버전에 머물러있는 경우 작동하도록하려면 무엇이든 대신 사용할 수 있습니다.

관련 문제