2014-03-28 33 views
0

我是PHP和Wordpress的新手。我在我的一個插件文件中有以下代碼。它用於爲類別生成一個下拉菜單。我想爲它添加一個html屬性。我想添加html屬性「autofocus」給它,這樣當頁面加載時這個選擇框默認具有焦點。語法 - 將html屬性添加到下拉類別

<?php 

    wp_dropdown_categories(array( 

     'name'   => 'question-category', 

     'id'   => 'question-category', 

     'taxonomy'  => 'dwqa-question_category', 

     'show_option_none' => __('Select a question category','dwqa'), 

     'hide_empty' => 0, 

     'quicktags'  => array('buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close'),    

    )); 

?> 

我遇到了應該添加這個語法的問題。請指教。

回答

2

你最好的選擇將是到echo屬性設置爲false,然後查找和替換返回的HTML的部分。

<?php 

    $questioncategory = wp_dropdown_categories(array( 

     'echo'  => false, 

     'name'   => 'question-category', 

     'id'   => 'question-category', 

     'taxonomy'  => 'dwqa-question_category', 

     'show_option_none' => __('Select a question category','dwqa'), 

     'hide_empty' => 0, 

     'quicktags'  => array('buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close'),    

    )); 

    $questioncategory = str_replace('<select', '<select autofocus', $questioncategory); 
    echo $questioncategory; 

?> 
+0

爲我工作!只有改變我不得不刪除你在變量中提到的「 - 」。感謝隊友:) – Yasser

+0

啊是好拿起:)感謝您的編輯 – haxxxton

0

嘗試添加此:

'autofocus'  => 'autofocus' 

所以

<?php 

    wp_dropdown_categories(array( 

     'autofocus'  => 'autofocus', 

     'name'   => 'question-category', 

     'id'   => 'question-category', 

     'taxonomy'  => 'dwqa-question_category', 

     'show_option_none' => __('Select a question category','dwqa'), 

     'hide_empty' => 0, 

     'quicktags'  => array('buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,spell,close'),    

    )); 

?> 
+0

這是我嘗試的第一件事。不起作用。 – Yasser