custom widget on the footer

时间:2017-07-05 作者:Wagner Moreira

我搜索了很多关于自定义小部件的信息,似乎它可以显示在侧栏中,但我想在主题的页脚中显示它。这只是一个版权自定义文本,我想添加到小部件中,以便在WP admin中轻松更改它。我遵循了本教程:http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-wordpress-widget/

我有以下结构:

theme-folder
----includes
--------widgets
------------copyright.php


//functions.php

  include_once( \'includes/widgets/copyright.php\' );

  if (function_exists("register_sidebar")) {
      register_sidebar();
  }
版权所有。php文件:

<?php

function register_my_widget() {// function to register my widget
    register_widget( \'My_Widget\' );
}

function My_Widget() {
    $widget_ops = array( \'classname\' => \'example\', \'description\' => __(\'A widget that displays the authors name \', \'example\') );
    $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'example-widget\' );
    $this->WP_Widget( \'example-widget\', __(\'Example Widget\', \'example\'), $widget_ops, $control_ops );
} 

add_action( \'widgets_init\', \'register_my_widget\' );// function to load my widget

class my_widget extends WP_Widget {// The example widget class
    // constructor
    function __construct() {
    parent::__construct(
    false,
    __( \'My Widget\', \'my-widget\' ),
    array( \'description\' => __( \'My sample widget\', \'my-widget\' ) ) );
    }
}


function widget( $args, $instance ) {// display the widget
    extract( $args );
    $title = apply_filters(\'widget_title\', $instance[\'title\'] );
    $name = $instance[\'name\'];
    $show_info = isset( $instance[\'show_info\'] ) ? $instance[\'show_info\'] : false;

    echo $before_widget;

    // Display the widget title 
    if ( $title )
        echo $before_title . $title . $after_title;

    //Display the name 
    if ( $name )
        printf( \'<p>\' . __(\'Hey their Sailor! My name is %1$s.\', \'example\') . \'</p>\', $name );

    if ( $show_info )
        printf( $name );

    echo $after_widget;
}

function update( $new_instance, $old_instance ) {// update the widget
    $instance = $old_instance;

    //Strip tags from title and name to remove HTML
    $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
    $instance[\'name\'] = strip_tags( $new_instance[\'name\'] );
    $instance[\'show_info\'] = $new_instance[\'show_info\'];

    return $instance;
}

function form() {// and of course the form for the widget options
    //Set up some default widget settings.
    $defaults = array( \'title\' => __(\'Example\', \'example\'), \'name\' => __(\'Bilal Shaheen\', \'example\'), \'show_info\' => true );
    $instance = wp_parse_args( (array) $instance, $defaults );

    //Text Input 
    // <p>
    //     <label for="echo $this->get_field_id( \'name\' );"><?php _e(\'Your Name:\', \'example\');</label>
    //     <input id="echo $this->get_field_id( \'name\' );" name="echo $this->get_field_name( \'name\' );" value="echo $instance[\'name\'];" style="width:100%;" />
    // </p>

}

?>
我需要对html进行注释,因为它无法识别,而且我不知道如何调用页脚中的小部件。php,以前有人尝试过这样做吗?

1 个回复
SO网友:Arpan Sagar

在主题函数的小部件操作挂钩中添加此代码。php

<?php
register_sidebar( array(
    \'name\' => \'Custom Footer\',
    \'id\' => \'custom-footer\',
    \'description\' => \'Appears in the footer area\',
    \'before_widget\' => \'<aside id="%1$s" class="widget %2$s">\',
    \'after_widget\' => \'</aside>\',
    \'before_title\' => \'<h3 class="widget-title">\',
    \'after_title\' => \'</h3>\',
    ) );
?>
要获取主题页脚小部件,请使用以下代码:

<?php
    if(is_active_sidebar(\'custom-footer\'))
    {
        dynamic_sidebar(\'custom-footer\');
    }
?>

结束

相关推荐

Admin-ajax.php在子域上无法正常工作

我试图将产品价格(从数据库中提取)乘以cookie存储的货币,对应于切换语言。为了实现这一点,我使用了AJAX脚本和几种方法。问题在于方法currencyRates();尽管在测试环境中,一切都很正常(我使用的是子文件夹而不是子域),但在生产级方法上currencyRates(); 只是返回值1(在方法开始时初始化,当var_dump $rate 在foreach循环中,它给了我期望的值,但当我尝试返回时,它总是在开始变量处初始化的值$rate = 1). 发生了什么事?子域如何影响这种类型的脚本?数据库