Tìm Hiểu về Wordpress - part 9 - Pdf 16

67
3.4.1 The WordPress Loop
The loop is the one thing that is absolutely core to understanding how WordPress
works. In its most basic, generalized form, the loop looks like this:
<?php
// The Loop
if (have_posts()) : while (have_posts()) : the_post();

endwhile; else:

endif;
?>
As veteran developers know, a “while” loop is a standard concept in any
programming language, and its use here is just standard PHP. First the loop makes
sure that there are some posts to display (the “if” statement). If that is true, it
begins the loop. Then, the function “the_post()” sets the stage for WordPress to
use inner-loop functions, which we will explore soon. Once the_post() has been
iterated the specified number of times, “have_posts()” turns to false and the loop
stops.
Yikes! That is sounding pretty abstract. Perhaps we better break things down so we
don’t lose each other.
Bad analogy!
Bad analogy!
68
3.4.2 The Loop in Plain English
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>"
rel="bookmark" title="Permanent Link to <?php the_
title_attribute(); ?>"><?php the_title(); ?></a></h2>

the Author who wrote it.
The full content of the Post is displayed.
More meta information about the post is displayed: all the
tags and categories given to this Post and the number of
comments, which is a link to the commenting area.
End of the loop
If there are older or newer posts available, display links to
them.
No posts? (a failsafe)
Better tell the people.
All done.
69
3.4.3 The Loop Just Knows…
As mentioned, the loop is simply a dressed-up “while” loop. While there are posts
available in the database, display the posts. In theory, it’s simple and utilitarian.
But what might remain confusing is just how this while loops knows exactly what
to loop. While… what? Well, without you having to tell it, the basic loop function
already knows what its query is going to be! To see for yourself what the query
string is, you can echo it to the web page by adding this little snippet directly
before the loop:
<?php echo $query_string; ?>
If we were to place this snippet above our index.php loop at the Digging into
WordPress site, the following information would be displayed on the home page:
posts_per_page=5&what_to_show=posts&orderby=date&order=DESC
In plain English, that reads: “Show five Posts in descending date order.” Likewise,
if we echo that $query_string variable from our archive.php file, and then visit the
“JavaScript” category archive, we see this:
posts_per_page=10&what_to_show=posts&orderby=date&order=DESC&category_
name=javascript
In plain English: “Show ten Posts from the javascript category in descending

$post. Much of this object data is used by functions that use it in more elaborate
ways, but the $post object provides “raw” data that is sometimes incredibly useful.
• $post->ID - returns the ID of post; useful for other functions that need an ID.
• $post->post_content - the actual post content, including all markup; useful
when you need to process the content of a post before outputting it.
• $post->post_modied - returns the datestamp of the last time the post was
updated.
• $post->post_name - returns the slug of the post.
In addition to these, there are many more. See http://digwp.com/u/399 for reference.
71
3.4.5 Some Common “Outside Loop” Functions
Some functions are built to return more global and/or generic information that
doesn’t have anything to do with any one particular Post. As such, they are meant
to be used in templates outside of the loop.
Here is a number of common and frequently used “outside-loop-only” functions:
• wp_list_pages() - displays a list of links to your static pages
• next_posts_link() - displays a link to older posts in archive views
• wp_tag_cloud() - displays a tag cloud of all your tags
• get_permalink() - returns the permalink of a post for use in PHP
Of course this is just a tiny sampling of all the functions available. The point we are
trying to drive home here is that some functions are dependent on being inside the
loop to function properly, and some do not.
3.5.1 Comments
Comments may be one of the reasons you are using WordPress to begin with. It
is a powerful system with lots of options and management possibilities. We are
going to go much more in-depth into comments in Chapter 7, but comments are
definitely part of the anatomy of a theme, so let’s get familiar now.
3.5.2 The comments.php File
In general, WordPress themes neatly compartmentalize the commenting
functionality into a single file, comments.php, which is responsible for the following:

all the content stuff. Your page.php might be incredibly similar to your single.php
file, but may omit this function because you don’t wish to have commenting on
your static content. What if the day comes along though where you wish to include
comments on Pages? You can simply create a new page template and include the
comments_template() function. Easy.
So, do you need to create a new special page template for every little trivial
change like this? Well, no, you don’t have to. How about another common
example. Say you want some of your Pages to display your regular sidebar, and
hide it from some of your other Pages. Apart from this difference, all of your pages
will be exactly the same. You could create a special page template called something
like page-nosidebar.php and omit the <?php get_sidebar(); ?> template tag. But
that’s a lot of repeated code for such a trivial change.
A better approach is to use a custom field to designate if you want to have a
sidebar or not. Then in the template, look for that custom field and behave
accordingly. Here is how this could look in your index.php file:
<?php // conditional sidebar display
if (!get_post_meta($post->ID, "noSidebar", true)) {
get_sidebar();
} ?>
You could use this exact same technique
for anything that you wish conditionally to
include or not include on a page template,
without having to create multiple templates.
The screencasts on CSS-Tricks
are Pages, not Posts. They
have their own special template
which, among other things,
includes user comments.
Screenshot showing how to set the
custom eld for this example

their main content area when
viewing their homepage. When
viewing an individual post,
the “middle” sidebar is gone,
leaving a wider main
content area.
75
Yet despite all of the stuff they contain, sidebars are typically much narrower than
the main content area. Sidebars are generally placed to the left or right of the
main content (e.g., your posts), but may actually appear anywhere. It all depends
on the structure and styling of your theme.
3.6.3 Popular Sidebar Functions
The particular requirements for the site you are building should dictate what you
put in a sidebar. Just throwing that out there, because you should never make
design decisions based on something like, “this other blog I saw had a tag cloud
so my blog should have a tag cloud.” Your blog should have a tag cloud if you use
a lot of tags to classify your Posts and you think your audience will benefit from
being able to navigate your site in that way. That being said, there are a number of
popular functions that can be useful in the sidebar setting. Let’s take a look:
• List recent posts
Perhaps your homepage design displays only one Post at a time. Or perhaps it
lists several, but you want to give people access to the latest ten Posts. There
is a special function for displaying such a configuration. In typical WordPress
fashion, it is a custom function that accepts a number of parameters that can be
useful in lots of situations.
<?php wp_get_archives(array(
'type' => 'postbypost', // or daily, weekly, monthly, yearly
'limit' => 10, // maximum number shown
'format' => 'html', // or select (dropdown), link, or custom
'show_post_count' => false, // show number of posts per link

These are just a few example parameters, see the Codex at http://digwp.com/u/31.
Example from:
http://webdesignerwall.com


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status