Hướng dẫn tạo themes cho wordpress part 8 - Pdf 16

Chapter 4
75
How it works
The next_posts_link and previous_posts_link functions each print a link that
allows users to navigate through the archives of a site. Interestingly, previous_posts_link
navigates forward through posts chronologically, whereas next_posts_link navigates
backward through posts chronologically. While this is counterintuitive to many people,
it has yet to be changed, and probably won't be, due to concerns over legacy themes.
In addition, the lone parameter to these functions allows you to customize the text that is
displayed with the link. Simply change the parameter value to change the links' text in
your theme.
There's more
Archive navigation isn't just for big loops with multiple posts. Single pages can be navigated
through similarly, by using functions named almost exactly the same. In single.php or
page.php, insert the following wherever you want navigation links to appear:
<div class="navigation">
<div><?php previous_post_link('&laquo; %link') ?></div>
<div><?php next_post_link('%link &raquo;') ?></div>
</div>
Displaying an edit link for posts
After you publish a post, you'll often want to go back and update facts as more becomes
available on a developing story, or to correct spelling and grammar errors that you've noticed
after reading through it. Rather than force your theme's users to log in and search to nd the
post to edit, you can provide a link to the editing page directly in your theme.
How to do it
Open a template le where you are displaying posts in the The Loop. Decide where you want
the edit link to appear. It can appear anywhere inside The Loop. Place your cursor at that point
and insert the following code:
<?php edit_post_link('Edit this entry','','.'); ?>
How it works
The edit_post_link function detects the currently logged in user's role and capabilities.

?>
Replace the string meta_name in the above code sample with the name of the meta key eld
that you wish to display the value for.
How it works
WordPress stores meta information in the post meta table in the WordPress database. When
you call get_post_meta in your theme, it looks for metadata named by the string passed as
the second parameter to the function related to the post specied by the ID passed in the rst
parameter. The third parameter tells WordPress to return only one result, so that you can print
it immediately.
There's more
A user can enter multiple post meta values for a single key. In order to retrieve and fetch all of
these, use the following code:
<?php
global $post;
$post_meta = get_post_meta( $post->ID, 'meta_name' );
foreach( $post_meta as $meta_item ) {
echo $meta_item . '<br />';
}
Template Tags
78
Replace the string 'meta_name' in the above code sample with the name of the meta key eld
that you wish to display the value for.
Here the omission of the third parameter instructs WordPress to return all post meta with
the key meta_name as an array. After that, you iterate over each post meta item and print it,
followed by a line break.
Displaying a post author's avatar
Visitors are more likely to engage with your post authors if they can identify them quickly and
easily. One of the best ways to allow this is to build support for displaying avatars directly into
your theme.
This technique is best for multi-author blogs where authors for different posts vary. If your blog

Displaying comments on a post
Displaying the latest comments on your blog
Highlighting the post author's comments
Alternating the style for comments
Displaying threaded comments properly
Introduction
Comments are one of the most important parts of blogging. In fact, some say that a blog isn't
really a blog without comments. As such, it is very important to pay attention to the way that
comments are styled and the way that they function in your theme.
This chapter starts with the basic tenets of comment display and formatting, and
later expands on some really interesting things that you can do with the WordPress
comment system.
Displaying a comment form on a post
Because comments are so important on a blog, it stands to reason that the best place to start
is with how to display an appropriate comment form on each post. The comment form allows
the appropriate visitors to leave comments with their name, e-mail address, and URL. The form
presented in this recipe is the standard comment form that the default WordPress theme uses.






Comments
82
How to do it
The rst thing you need to do here is to open your single content template, either
single.php or page.php, and insert the appropriate template tag. Scroll to the
end of your post display code, and insert the following function call:
<?php comments_template(); ?>

frequent use case for displaying comments is to list all of the comments on a single post, and
there is a template tag that does this quickly and easily. In this recipe, we'll take a look at
the template tags used to display comments and where you would use them.
How to do it
First, you'll need to decide where you want to display your comments. The most obvious place
to use comment template tags would be in a custom comments template. Alternatively, you
could just list comments directly on a post without concern for the other things that a
comments template provides (appropriate inputs and more).
Here we'll use the default
comments.php le as an example. Copy the comments.php
le from the default theme to your custom theme's directory. Scroll down to line 28 (as of
WordPress 2.9) of the le and see that the following code is present:
<ol class="commentlist">
<?php wp_list_comments(); ?>
</ol>
Here you're using the wp_list_comments function, which iterates over the comments on a
post and renders the appropriate output based on the comment display callback. A default
callback is used if you do not explicitly pass one.
How it works
wp_list_comments is a special template tag that looks at the comments for the current post
and displays them by using a special callback function. Calling the template tag without any
parameters tells it to use the default options, which will generally produce markup compatible
with your theme. The output is, by default, a list of <li> elements containing various comment
metadata as well as the comment author's avatar. You can see an example of a few comments
in the following screenshot from the default theme:


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

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