我们开发了一个具有自定义小部件类的主题。
class ams_button_widget extends WP_Widget {
...
public function widget( $args, $instance ) {
$btn_text = apply_filters( 'button_text', $instance[ 'button_text' ] );
$btn_link = apply_filters( 'button_link', $instance[ 'button_link' ] );
$btn_color_class = apply_filters( 'button_color_class', $instance[ 'button_color_class' ] );
if( isset( $args[ 'before_widget' ] )) { echo $args['before_widget']; }
if( isset( $btn_text ) && !empty( $btn_text )) {
if( isset( $args[ 'before_button_text' ] )) { echo $args[ 'before_button_text' ]; }
?>
在主题的Button Widget中有两个Top Bar Widgets实例,但我需要修改的一个实例是:
Download Latest Update
我一直在搜索和阅读WordPress代码,试图找到我需要做的事情的实例:将变量的值附加到btn_text中。
在Widgets部分中,这个特定的btn_text被设置为Download Latest Update。我有一个函数,它收集一些我想在默认btn_text之后附加的版本信息,即。Download Latest Update **v1.0.11(2019)**,但我运气不好。
我是修改了类,还是在我的functions.php中创建了一个函数来处理它,如果是的话,有人能给出一些解释吗?
谢谢你的投入!
发布于 2019-05-01 21:39:23
在
Widgets
部分中,这个特定的btn_text
被设置为Download Latest Update
。我有一个函数,它收集一些我想在默认btn_text
之后附加的版本信息,即。Download Latest Update **v1.0.11(2019)**
小部件有一个名为button_text
的过滤器,您可以利用它来定制按钮文本,而不必更改小部件class
:
$btn_text = apply_filters( 'button_text', $instance[ 'button_text' ] );
Download Latest Update
如果是这样,那么调用收集版本信息的函数--确保将version_func
更改为正确的函数名:
add_filter( 'button_text', function( $text ){
if ( 'Download Latest Update' === $text ) {
$text .= ' **' . version_func() . '**';
}
return $text;
} );
PS:我建议您使用唯一的按钮文本,以避免更改(或干扰)其他按钮。或者,由于过滤器名称是quite,所以它是泛型的,并且可能被其他小部件、class
、__es或其他代码应用。
中启用短代码
[my-version-func]
;例如,Download Latest Update **[my-version-func]**
。class
/code我不推荐这个选项,但无论如何,这里有一个例子:
代之以:
$btn_text = apply_filters( 'button_text', $instance[ 'button_text' ] );
有了这个:
$btn_text = trim( $instance['button_text'] );
if ( 'Download Latest Update' === $btn_text ) {
$btn_text .= ' **' . version_func() . '**';
}
$btn_text = apply_filters( 'button_text', $btn_text );
https://wordpress.stackexchange.com/questions/336819
复制