Tải bản đầy đủ (.pdf) (10 trang)

Tìm Hiểu về Wordpress - part 10 pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.37 MB, 10 trang )

77
• Show the blogroll
In the Admin area, there is an entire area just for “links” (located in the same
area with Posts and Pages). This area was once commonly referred to as the
“Blogroll” but that term has gone a bit out of fashion. Regardless of what it’s
called, the list of links managed in this area may be easily displayed with this:
<?php wp_list_bookmarks(array(
'orderby' => 'name', // alphabetical
'order' => 'ASC', // starting at A
'limit' => -1, // unlimited, show ALL bookmarks
'title_li' => __('Bookmarks'), // include list item title
'title_before' => '<h2>', // tag before title
'title_after' => '</h2>', // tag after title
)); ?>
These are just a few example parameters, see the Codex at />• Editable text area
One of the things you may wish to include in a
sidebar is an area of text that you can edit from
the back end. There are a couple of different ways
to do this. One way would be to use a “global
custom field” (see page 66). Another way would
be to use widgets. Widgets are WordPress’ way
of allowing management of regions through the
Admin, rather than having to edit theme files. One
of the many different types of widgets is a generic
“text” widget. If you have a “widgetized” sidebar,
you can just drag this over into that area, enter
your info, and save it.
78
3.6.4 Widgets, Widgets, Widgets
“Widgetizing” a sidebar, or any other region for which you wish to have
manageable areas, is pretty easy. And because widgets are standardized, plugins


can make use of them and deliver special widgets that you can control directly
from within the Admin area. We’ll look more into widgetization in section 4.8.1,
but for now, this is the code that you would place into your theme file:
<div id="sidebar">
<ul>
<?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar()) : ?>
<li><! stuff shown here in case no widgets active ></li>
<?php endif; ?>
</ul>
</div>
Now in your functions.php file, you “register” the sidebar with a custom function:
if (function_exists('register_sidebar')) {
register_sidebar(array(
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
));
}
Now every widget you add will appear inside list tags with corresponding <h2>
headings, which should fit well into the surrounding markup. Each widget will also
have a unique ID and common class name (for potential CSS styling).
More Sidebar Ideas
For more great techniques and
ideas for crafting the perfect
sidebar, check out Section
4.4.1, “Side Content and
Useful Menu Items.”
79
3.7.1 The Search Form

WordPress provides built-in search functionality, and you should definitely share
it with your visitors. It is an expected feature of most websites, providing a useful
way for visitors to locate their favorite content.
3.7.2 Why is This a Separate File?
It is very common for a WordPress theme to have a file called searchform.php. This
is because the search form may be used in multiple theme files and in different
locations, so placing it in its own file helps to keep things modular. Just like the
sidebar, which you can include at your leisure with the get_sidebar() function, you
can include your searchform.php in any other template file by calling this function:
<?php get_search_form(); ?>
This function accepts no other parameters, but of course if you had a good reason
to rename searchform.php or keep it anywhere other than the root directory of
your theme, you could just use the standard include code instead:
<?php include(TEMPLATEPATH . '/inc/special-search-form.php'); ?>
Where might you use this?
• On the 404 page (404.php)
• In the “else” part of The Loop
• In the sidebar
3.7.3 Alternatives to WordPress Search
The fact of the matter is that the WordPress built-in search kind of sucks. It lists
things in chronological order based on whether or not it found any of your search
80
terms. When searching for your query, WordPress looks in the titles and content of
your posts and pages. If the search terms aren’t located there, WordPress will tell
you that nothing can be found.
To make things worse, there is no advanced search functionality, meaning you have
very little control as a user or as a theme developer as to how results are refined,
returned, and displayed. There are ways of hacking together a decent WordPress
search system, but it takes quite a bit of meddling.
A much easier way to improve the WordPress’ default search functionality is either

to replace it entirely with Google or install a plugin that will beef things up
for you.
Google Search />As it happens, an alternative to WordPress search is to just use Google search
instead. Not just generic Google full-web search, but rather a free service called
the Google Custom Search Engine, with which you can integrate customized, site-
specific search engines into any site. It’s easy and super-awesome.
Search API Plugin />This plugin provides a far more powerful searching system than the default
WordPress search. With the Search API plugin, you can search different types of
content, search within particular categories, use search operators like AND, OR, and
NOT, and even search using a more comprehensive “fulltext” MySQL search. As if
that weren’t cool enough, this plugin also integrates with Google Custom Search.
Search Everything
Instead of just looking
at post titles and content
to locate matching search
terms, wouldn’t it be neat if
WordPress searched through
everything in your database?
The Search Everything plugin
does exactly that, telling
WordPress to search through
comments, drafts, attachments,
and just about everything else.
/>Each of the different search choices on CSS-Tricks
activates a different Google Custom Search Engine.
81
3.8.1 The Footer
Just like headers and sidebars, footers are one of those ubiquitous design elements.
They are so commonly used that WordPress again has a special template tag for
including them into your theme:

<?php get_footer(); ?>
This function will accept only one parameter, a string, which works like the sidebar
function. Used without a parameter it will fetch the footer.php file and insert it.
When used with a parameter like so…
<?php get_footer("mini"); ?>
<?php get_footer("mega"); ?>
…the get_footer() template tag will retrieve the theme files “footer-mini.php” and
“footer-mega.php,” respectively.
3.8.2 The wp_footer() Hook
Remember the wp_head() hook? Well, the wp_footer() hook is exactly like that, only
used down in the footer instead. It tells WordPress, “the footer is right here.” All
by itself, it doesn’t do anything, it’s just a generic hook that can be used to which
scripts and other functionality may be attached.
For example, it is fairly common practice to load HTML-dependent JavaScript files
from within the footer instead of the header. The location of the wp_footer() hook
within your footer.php file will determine where the JavaScript is displayed in the
source code. Thus, a good place for this hook is just before the closing <body> tag.
<?php wp_footer(); ?></body>
82
Mini footer
Mega footer
JeCampana.com
Just a thin black bar with
a copyright and a small
illustration. Even that small
bit of text could be kept
dynamic:
&copy; <?php
echo date("Y");
bloginfo('name'); ?>

YoDiv.com
Enormous section of content
displayed with a clever
“underground” design. This
could have been accomplished
any number of ways, but
probably most practically by
using the Links area in the
Admin and using the wp_
list_bookmarks to generate the
different categories of links.
83
3.8.3 Mini Footers / Mega Footers
Like your sidebar, the design of your footer should serve the needs of your site
and it’s audience. If you don’t need the room in your footer, no need to junk it up
with unnecessary stuff. At the same time you shouldn’t be afraid to do something
interesting or innovative if you have appropriate content and the desire to do so.
3.9.1 Theme Functions
WordPress themes can include a special file, functions.php, which gives you a lot
of control and power over things that happen in your theme. Think of it like a file
that can do anything a plugin can do, without needing a plugin. Neat eh? This
allows for a lot of cool theme functionality that was not possible in the days before
this file existed. The functions.php file is also beneficial for themes that need to be
“bundled” with certain plugins.
3.9.2 Functions are for Specific Themes
Because you can accomplish similar things with plugins as you can with custom
functions (i.e., the functions.php file), some rational decisions should be made
about where to do what.
Because the functions contained within the functions.php file reside within
the theme folder itself, the code inside it depends on that particular theme

being active in order to run. Plugins on the other hand, remain operational (or
inoperational, as the case may be) regardless of which theme is active. Thus, don’t
put anything in functions.php that is required by multiple themes. Likewise, don’t
do anything with a plugin that is only relevant to the current theme.
84
Examples:
Hide WordPress upgrade notification bar Use a plugin
Add button to Post Editor Use a plugin
Load jQuery from Google Use functions.php
Replace default Gravatar image Use functions.php
In the top two examples, the desired result is related to the Admin area and so has
nothing to do with the theme itself. You should use a plugin here, because they
are theme-independent and won’t stop working when you change themes.
In the bottom two examples, those two things are specific to a theme, and thus
should happen in the functions.php for that theme.
3.9.3 Advantage Over Core Hacks
In the dark days before functions.php, modifying and customizing WordPress
functionality was often a matter of altering “core” files. There are a number of
problems with this. First and foremost, you could break something in a pretty
substantial way and have no clear path to restored functionality. Equally as
important, upgrading WordPress means upgrading core files. As in, overwriting
them. You would lose your alteration entirely unless you documented exactly
what you did; and even then you would have to trudge through and manually
re-implement your changes with every new version of WordPress (and there are
plenty, trust us). As you do this, you will also need to account for any changes in
the core files and adapt your hack accordingly. A real pain in the keyboard, if you
know what we mean. The worst part of this nasty practice is that, after going
through a few upgrades, you will probably throw in the towel and stop upgrading
85
to new versions. This of course is bad because it leaves you open to potential

vulnerabilities and missing out on the latest and greatest cool features.
Fortunately hacking the WordPress core is rarely needed these days because there
are so many hooks available to latch onto and literally rewrite content or append
new functionality.
Up next…
Now that we are familiar with the anatomy and basic functionality of a WordPress
theme, let’s dig deeper into WordPress and learn as much as we can about theme
design and development. Strap yourself in, it’s going to be a wild ride!
It may not look like a typical
WordPress site, but it is! All
the quotes seen in this book are
from Quotes on Design.

If You Must…
As bad a practice as it is to
hack the WordPress core,
there may be situations in
which doing so is the only
way to accomplish your goals
(WordPress isn’t all-powerful,
after all). If you nd yourself
contemplating a little core
hacking, be sure to check out
Chapter 5.4.3 – Hacking the
WordPress Core – for some
helpful tips.
86
My rates are as follows:
$50/hour
$75/hour if you watch

$100/hour if you help
– CLASSIC AUTOBODY SIGN

×