[英]Giving a widget my own id or class in Wordpress
I'm writing a Wordpress plugin with a widget.我正在编写一个带有小部件的 Wordpress 插件。 In my jQuery code i'd like to point to that widget, so I though giving it an HTML ID would be nice.
在我的 jQuery 代码中,我想指向那个小部件,所以我虽然给它一个HTML ID会很好。 (Edited, thanks for pointing out.)
(已编辑,谢谢指出。)
After doing some searching on the internet (and the Codex) I know that I can give ID's to the widgets in the theme, but it's not what I was looking for.在互联网(和 Codex)上进行了一些搜索后,我知道我可以为主题中的小部件提供 ID,但这不是我想要的。 This method has flaws.
这种方法有缺陷。 Changing the theme may cause errors (of course, I know it has to be changed in the functions.php , but it's just meh).
更改主题可能会导致错误(当然,我知道必须在functions.php 中更改它,但这只是meh)。 The other thing is that my widget got a number which may change without me knowing.
另一件事是我的小部件得到了一个可能在我不知道的情况下改变的数字。
So to be 100% sure it works and will work in the future, can I give my widget my own ID ?因此,为了 100% 确定它可以工作并且将来可以工作,我可以给我的小部件我自己的 ID吗?
I might not understand your question exactly and what "ID"
do you mean ( widget ID , or actual HTML div ID - which are actually one and the same.. ) but If you have read the codex , the example for how to give an ID is given there ..我可能不完全理解您的问题以及您的意思是什么
"ID"
(小部件 ID 或实际的 HTML div ID - 实际上是一个和相同的..)但是如果您已经阅读了代码,那么如何给出一个示例身份证在那里..
function __construct() {
parent::__construct(
'foo_widget', // Base ID
__('Widget Title', 'text_domain'), // Name
array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
Another way to do the same ( and helpul if you are talking about HTML
elements like divs
- you can assign a class
)另一种方法来做同样的事情(如果你在谈论像
divs
这样的HTML
元素,那么divs
——你可以分配一个class
)
function My_Widget() {
function My_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays nothing ', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
Note that a numerator will be automatically added to your widget´s ID based on how many instances were initiated like :请注意,分子将根据启动的实例数量自动添加到您的小部件 ID 中,例如:
foo_widget
foo_widget-2
foo_widget-3
etc ...等等 ...
EDIT I - after comments编辑我- 评论后
At any rate , IMHO it is a bad idea to hard-code a fixed ID
in a widget for the simple reason that the preference for a widget from the developer point of view is to always allow support for multiple instances .无论如何,恕我直言,在小部件中硬编码固定
ID
是一个坏主意,原因很简单,从开发人员的角度来看,对小部件的偏好是始终允许支持多个实例。 Giving an an HTML ID
anywhere on the widget will cause validation errors AND in the case of jQuery
- also JS
errors for the simple cause that if a user will have 2 widgets , it will also have a duplicated ID .在小部件上的任何地方提供一个
HTML ID
会导致验证错误,并且在jQuery
的情况下也会导致JS
错误,原因很简单,如果用户将有 2 个小部件,它也会有一个重复的 ID。
In other words - It is the exact opposite of your statement in the original question.换句话说 - 这与您在原始问题中的陈述完全相反。
So to be 100% sure it works and will work in the future, can I give my widget my own ID
所以为了 100% 确定它可以工作并且将来可以工作,我可以给我的小部件我自己的 ID
Giving a fixed hard coded ID to your widget will in fact ensure that it will NOT work 100% of the time.为您的小部件提供固定的硬编码 ID 实际上将确保它不会在 100% 的时间内工作。
The preference is always to target such issues with a class
( or with something like div[id^="my_widget_id"]
) and let wordpress "do it´s thing" ( by auto incrementing IDs ).首选总是针对
class
(或类似div[id^="my_widget_id"]
)并让 wordpress “做它的事情”(通过自动递增 ID)。
For the exact same reason - a theme should always have the same structure in the register sidebar()
function like so :出于完全相同的原因 - 主题在
register sidebar()
函数中应该始终具有相同的结构,如下所示:
<?php $args = array(
'name' => __( 'Sidebar name', 'theme_text_domain' ),
'id' => 'unique-sidebar-id',
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">', // LOOK AT THIS LINE
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>' ); ?>
This will permit the specific sidebar to auto increment
the ID of the widgets in order to avoid the above mentioned problem .这将允许特定的侧边栏
auto increment
小部件的 ID 以避免上述问题。
From the codex :来自法典:
before_widget - HTML to place before every widget(default: '') Note: uses sprintf for variable substitution
before_widget - HTML 放置在每个小部件之前(默认:'') 注意:使用 sprintf 进行变量替换
All that being said, and if you are insisting of giving a hard-coded fixed ID somewhere there ( instead of a the methods described above ) you can always put that at a nested div
or span
INSIDE your widget´s HTML output, But I would think that if you have read attentively this answer - you will avoid it now.说了这么多,如果你坚持在某个地方提供一个硬编码的固定 ID(而不是上面描述的方法),你总是可以把它放在一个嵌套的
div
或span
INSIDE 你的小部件的 HTML 输出中,但是我会认为如果你仔细阅读了这个答案 - 你现在会避免它。
Now,- since you have not included any code in your question ( which is always a bad practice here on SE ) there is little more I can do to help.现在,- 由于您没有在问题中包含任何代码(这在 SE 上总是不好的做法),所以我无能为力。 If you encounter any problems targeting the widget without an ID - I suggest you to open a new question and maybe point a link at the comments here, so myself ( and others ) can help you with it ..
如果您遇到针对没有 ID 的小部件的任何问题 - 我建议您打开一个新问题,并可能在此处的评论中指向一个链接,以便我自己(和其他人)可以帮助您解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.