問題描述

在外觀 – > 視窗小工具選單中有一個小工具的列表,您可以在側邊欄中進行拖放。

這些自定義小工具的 HTML /PHP 程式碼在哪裡?

我一直在 WordPress 的功能參考,但找不到任何東西。當然,這些小工具必須從 HTML /PHP 模板中拉出來。

我想知道的原因是預設的小工具標題是<h3> 標籤,我想將它們更改為<h5> 標籤。另外我還需要新增一些<hr /> 等等。

我檢視了 theme/includes/widgets.php 檔案,但沒有發現任何內容。

我正在使用二十一一的副本來修改我的主題。

theme/sidebar.php 中的程式碼用於 (!dynamic_sidebar()),但是我的側邊欄是動態的,因此這段程式碼是無用的。

最佳解決方案

WordPress Widgets API 是如何建立各種小工具並註冊的側邊欄。

建立新視窗小工具時,可以將變數新增到任何從 register_sidebars 引數獲取值的視窗小工具。

args (string/array) (optional)
Builds Sidebar based off of ‘name’ and ‘id’ values. Default: None
name – Sidebar name.
id – Sidebar id.
before_widget – HTML to place before every widget.
after_widget – HTML to place after every widget.
before_title – HTML to place before every title.
after_title – HTML to place after every title.

例:

<?php
    add_action( 'widgets_init', 'prefix_register_sidebars' );
    function prefix_register_sidebars() {
         $args = array(
         'name' => 'My Sidebar',
         'id'   => 'my-sidebar',
         'before_widget' => '<div id="%1$s" class="widget %2$s">',,
         'after_widget'  => '</div><hr />',
         'before_title'  => '<h5 class="widgettitle">',
         'after_title'   => '</h5>'
         );
      register_sidebars( $args );
   }

示例小工具:

class MY_Widget extends WP_Widget {
    function my_widget( $args, $instance ) {
         $widget_ops = array(
         'description' => 'My Widget Description'
         );
        parent::WP_Widget(false, 'My Widget Name', $widget_ops );
    }
    function widget() { // This controls the display of the widget
    $title = 'My Widget Title';

    echo $before_widget;  // Outputs the the 'before_widget' register_sidebars setting
    echo $title;         //Will be wrapped in the 'before_title' and 'after_title' settings
    echo '<p>This is my widget output</p>';
    echo $after_widget;  //Outputs the 'after_widget' settings
    }
}
add_action( 'widgets_init', 'prefix_register_widgets' );
function prefix_register_widgets() {
    register_widget( 'my_widget' );
}

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。