Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何将用户定义的CSS类添加到自定义小部件呈现代码中?_Html_Css_Wordpress_Plugins_Widget - Fatal编程技术网

Html 如何将用户定义的CSS类添加到自定义小部件呈现代码中?

Html 如何将用户定义的CSS类添加到自定义小部件呈现代码中?,html,css,wordpress,plugins,widget,Html,Css,Wordpress,Plugins,Widget,我通过扩展WP\u widget创建了一个小部件,效果很好。 还向管理员添加了一个名为className的字段 但是我找不到任何可以将自定义CSS类名注入小部件的呈现代码的钩子(当然,我希望该类应用于整个小部件) //在小部件之前。 echo$before_小部件; 如果($标题) {echo$before_title.$title.$before_title;} 如果($headline) {echo'.$headline.';} 如果($说明) {echo''.$description.''

我通过扩展
WP\u widget
创建了一个小部件,效果很好。 还向管理员添加了一个名为
className
的字段

但是我找不到任何可以将自定义CSS类名注入小部件的呈现代码的钩子(当然,我希望该类应用于整个小部件)

//在小部件之前。
echo$before_小部件;
如果($标题)
{echo$before_title.$title.$before_title;}
如果($headline)
{echo'.$headline.';}
如果($说明)
{echo''.$description.'

';} if($className) {echo'类名:'.$className.

';} //在widget之后。 echo$after_小部件;
此文件的HTML输出如下所示:

<aside id="eppz-page-widget-2" class="widget widget_eppz-page-widget">
    <h3 class="widget-title">Title</h3>
    <h4>Headline!</h4>
    <p>Some description that can be added optionally to the widget.</p>
    <p>Class name: featuredPageWidget</p>
</aside>

标题
大字标题
一些可以选择性地添加到小部件的描述

类名:featuredPageWidget


现在我真的想在所有应用于
的类之后附加
className
变量。我可以用一些字符串函数来实现,但我真的希望有一种简单的WordPress方法来实现这一点。

因为我真的不想在小部件注入之前覆盖默认模板
,所以我想出了一个奇怪的解决方法,仍然不涉及模板实现

我使用占位符作为类名,然后在呈现代码中简单地替换它

设置只读类属性:

function getClassNamePlaceHolder()
{ return 'EPPZPageWidgetClassNamePlaceHolder'; }
Set作为构建时小部件的类名:

$widgetOptions = array
(
    'classname' => $this->getClassNamePlaceHolder(),
    'description' => 'Lovely widget description for humans.'
);
在呈现时用小部件参数替换占位符:

function widget($args, $instance)
{

    // ...

    // Exchange default class name to custom.
    $classNamePlaceHolder = $this->getClassNamePlaceHolder();
    $before_widget_withCustomClassName = str_replace($classNamePlaceHolder, $customClassName, $before_widget);

    // Before widget.
    echo $before_widget_withCustomClassName;
就在那里。 无论在小部件
实现之前实际的
是什么,这种方法都可以安全地为整个小部件设置自定义类名(请参见下面的
MyCustomClassName
):

<aside id="eppz-page-widget-2" class="widget MyCustomClassName">
    <h3 class="widget-title">eppz! page widget</h3>
    <h4>Headline!</h4>
    <p>Some content goes here.</p>
</aside>

爱普斯!页面小部件
大字标题
这里有一些内容


首先在构造函数中添加一个自定义占位符类

<?php
public function __construct() {
   $widget_ops  = array(
      'classname'                   =>; 'widget_text eaa __eaa__', //__eaa__ is my placeholder css class
      'description'                 =>; __( 'AdSense ads, arbitrary text, HTML or JS.','eaa' ),
      'customize_selective_refresh' =>; true,
   );
   $control_ops = array( 'width' =>; 400, 'height' =>; 350 );
   parent::__construct( 'eaa', __( 'Easy AdSense Ads &amp; Scripts', 'eaa' ), $widget_ops, $control_ops );
}
?>

然后将其替换为基于如下小部件选项选择的类

<?php
if ( $instance['no_padding'] ) {
   $args['before_widget'] = str_replace( '__eaa__', 'eaa-clean', $args['before_widget'] );
}
?>

我们使用占位符来限制before_小部件的html可能影响插件功能的方式

有关示例的详细信息,请访问

<?php
if ( $instance['no_padding'] ) {
   $args['before_widget'] = str_replace( '__eaa__', 'eaa-clean', $args['before_widget'] );
}
?>