Pt-br:Widgets em Temas

来自WordPress中文文档
跳转至: 导航搜索

wordpress.org.cn 模板:Ajuda 模板:Traduz

Eu sou um autor de temas. Por que esses emails com gente me pedindo para atualizar o tema?

Você deveria ficar feliz que eles gostam do seu tema o suficiente para te contatar em vez de mudar para algum outro tema. Parabéns por deixar os usuários felizes! Nos desculpe se isso soa alarmante. Isso machuca-nos mais que a você. Por favor conserte seu tema. Widgets são agora uma parte padrão da instalação do Wordpress então para alcançar a mais ampla audiência seu tema devia ter suporte a eles, assim como suportar as sidebars antigas.

I give in. How do I fix up my theme?

First you have to ask yourself, “Do I know anything about my theme? Does it use an unordered list to create the sidebar?” (If you can’t answer that, you’ll need in-depth help on this task and that usually means paying somebody a lot of money or asking very nicely at the forums. Better yet, you can learn HTML. Sorry, we don’t teach that here.)

Here is an example of good sidebar markup:

<ul id="sidebar">
 <li id="about">
  <h2>About</h2>
  <p>This is my blog.</p>
 </li>
 <li id="links">
  <h2>Links</h2>
  <ul>
   <li><a href="http://example.com">Example</a></li>
  </ul>
 </li>
</ul>

Notice that the entire sidebar is an unordered list and the titles are in <h2> tags. Not every theme is built this way and it’s not necessary to do so, but it’s currently the most common sidebar markup in the themes we surveyed so we adopted it. The element with id=”links” is the equivalent of one basic widget.

The Widgets API offers a few functions to use in your template just like template tags. These functions let WordPress replace your theme’s sidebar with a dynamic one while still falling back on the old sidebar in case you remove all the widgets.

Here is an example of a basic sidebar upgrade using the same markup as above:

<ul id="sidebar">
<?php if ( !function_exists('dynamic_sidebar')
        || !dynamic_sidebar() ) : ?>
 <li id="about">
  <h2>About</h2>
  <p>This is my blog.</p>
 </li>
 <li id="links">
  <h2>Links</h2>
  <ul>
   <li><a href="http://example.com">Example</a></li>
  </ul>
 </li>
<?php endif; ?>
</ul>

See? We just added two lines to the template and now it’ll display a dynamic sidebar if possible, otherwise display the old sidebar. Removing all the widgets from the sidebar in the admin interface will cause the old sidebar to be displayed.

Now there is one more thing to be done to the theme. Assuming you are using WordPress 2.0 or higher, this change will be made within functions.php in your theme’s directory.

includeonlydiv style=clear:both; background-color:#ffffff; border:1px solid #c6d9e9; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;NOTE: Some of the following examples involve modifying your theme's functions.php. Incorrectly modifying this page can stop your admin pages from appearing. It's a good idea to take a backup of your functions.php before making any changes./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude

Here is an example of functions.php for a theme that does not yet have such a file (no blank lines at the beginning or end of the file, please):

<?php
if ( function_exists('register_sidebar') )
    register_sidebar();
?>

That’s it, just four lines. This code tells the plugin that your theme will need exactly one dynamic sidebar. At this point, your admin interface should have an extra item in the Presentation menu: Sidebar Widgets. Try dragging some widgets from the palette on the left into the box marked Sidebar 1 and saving your changes. Got it working? Fantastic.

My sidebar isn’t a list. What do I do?

We knew you’d ask that. You’ll have to discover your sidebar’s design pattern, then use some extra parameters to tell the plugin how to format them to work with your theme. We’ll work through one example.

Here’s the original markup:

<div id="sidebar">
 <div class="title">About</div>
 <p>This is my blog.</p>
 <div class="title">Links</div>
 <ul>
  <li><a href="http://example.com">Example</a></li>
 </ul>
</div>

Yes, we’ve seen markup like this. It’s the second most common sidebar design pattern, which is why we chose it for the example. The first difference is that the sidebar is not built inside a <ul> tag. That means we should not be wrapping any of our widgets in <li> tags. The second difference is that our titles are wrapped in <div class=”title”> instead of <h2> tags.

We prefer to change the markup to our ul/li/h2 standard, but the API is powerful enough that we don’t have to. Instead, we fix these issues by adding some parameters to the code in functions.php:

<?php
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<div class="title">',
        'after_title' => '</div>',
    ));
?>

And here is the sidebar.php markup with our special template tags inserted:

<div id="sidebar">
<?php if ( !function_exists('dynamic_sidebar')
        || !dynamic_sidebar() ) : ?>
 <div class="title">About</div>
 <p>This is my blog.</p>
 <div class="title">Links</div>
 <ul>
  <li><a href="http://example.com">Example</a></li>
 </ul>
<?php endif; ?>
</div>

That’s it. Your HTML markup is taken care of.

Well, mostly taken care of. The default before_widget is a little bit more than just <li>. It includes an id and class. Well, sort of, but this is where it gets complicated. The default before_widget includes sprintf directives %1$s and %2$s, which are replaced by the id and class, respectively. The id is generated by sanitizing the widget name (which is why you should name your widget carefully: you don’t want duplicate id’s in one HTML document!) and the class is generated from the widget’s callback. This ensures all Text and RSS widgets, for instance, have unique id’s and similar classnames. Additionally, there is a “widget” class for each widget.

So, if you want your theme to be most flexible you should use this instead of the empty strings shown above:

    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',

Now your HTML markup is REALLY taken care of.

The HTML looks good but the page looks awful in the browser!

Yeah, we knew that would happen. Your theme was probably written before widgets were born so the author didn’t know she should make the stylesheet flexible enough to handle new markup in the sidebar. If you know some CSS, you should be able to handle the problems with a few new rules at the end of your stylesheet. Look in your blog’s markup for the selectors (id and/or class) belonging to each widget you want to style.

If CSS is a mystery to you, we regret that we can’t offer you any help. As much as we’d like to help you with this, it just isn’t possible due to the wild variations of themes. Contact your theme’s author and ask her to update the theme for better compatibility with widgets.

The search widget is still ugly. I want my theme’s original search box as a widget.

The widgets are CSS-selectable so that you can style them very specifically. However, the markup might not be to your liking. Many themes will look better if they supply their own widgets to replace some of the basic widgets, such as Search and Meta. It’s usually best to copy the existing markup from sidebar.php into a new widget in functions.php, then use the registration functions to replace the standard widget with the custom one.

You can do this with any part of the theme’s sidebar, or all of them. Here’s an example of how to do this:

function widget_mytheme_search() {
?>
    << PASTE YOUR SEARCH FORM HERE >>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget(__('Search'), 'widget_mytheme_search');

I have a theme with more than one sidebar. How do I make them all dynamic?

Oh, that’s easy. Instead of register_sidebar() you should use register_sidebars(n) where n is the number of sidebars. Then place the appropriate number in the dynamic_sidebar() function, starting with 1. (There are several other ways to use these function. See the API).

You can even give your sidebars names rather than numbers, which lets you maintain a different set of saved sidebars for each theme. But if you need to know so much about the plugin, why aren’t you reading the API?

Related

Widgets API: is_active_sidebar, register_sidebars, register_sidebar, unregister_sidebar, is_dynamic_sidebar, dynamic_sidebar, register_sidebar_widget, unregister_sidebar_widget, wp_register_sidebar_widget, wp_unregister_sidebar_widget, wp_get_sidebars_widgets, wp_set_sidebars_widgets, is_active_widget, the_widget, register_widget, unregister_widget, register_widget_control, unregister_widget_control, wp_register_widget_control, wp_unregister_widget_control, wp_convert_widget_settings, wp_get_widget_defaults, wp_widget_description 模板:Codex-pt