WordPress:Customizing the Read More

来自站长百科
Xxf3325讨论 | 贡献2008年4月10日 (四) 09:32的版本 (新页面: If you have set your WordPress site to display post excerpts on the front or home page, you will want visitors to click on the title or a link to encourage them to continue reading your p...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

If you have set your WordPress site to display post excerpts on the front or home page, you will want visitors to click on the title or a link to encourage them to continue reading your post or article, right? WordPress makes this technique easy, and customizable.

The Excerpt Basics

Excerpts show up on WordPress through two methods. One, by replacing the template tag the_content() with the_excerpt(). Then the explicit excerpt you have entered in the Administration > Write > Post SubPanel will appear, or the first 55 words of the post's content. Users can then click on the title to continue reading more as you have enticed them with your summary introduction.

The most commonly used method is to keep the_content() template tag and insert a quicktag called more into your post at your desired "cut-off" point.

The quicktags are the little buttons found above the editing window in your Administration > Write > Post SubPanel. They include bold, italic, links, and others, and the famous more. Put your cursor where you want to end the excerpted content of your post and click the more quicktag button. It will insert a code at that point that looks like this:

and I told him that he should get moving or I'd be
on him like a limpet.  He looked at me with shock on 
his face and said

<!--more-->

The rest of the post continues, but when viewed on the non-single/non-permalink web page such as archives, categories, front page, and searches, the post is shown as an excerpt to the more point.

Read More Techniques

The parameters within the template tag the_content() are as follows:

<?php the_content('more_link_text', strip_teaser, 'more_file'); ?> 

The more_link_text sets the link text like "Read More". The second one, strip_teaser, sets whether or not the "more" link should be hidden (TRUE) or displayed (FALSE). The default is FALSE, which shows the link text. The last one, more_file, sets the link to the file that the "Read More" should link to, if you want it to be different. By default, it links to the current post file.

To remove the teaser:

  • Change the_content(); in your index.php to (i.e., the second parameter controls this):
the_content('','FALSE','');>
  • Include <!--noteaser--> in the post text, immediately after the <!--more-->.

Link Jumps to More or Top of Page

By default, when you click on the Read More link, the web page loads and then "jumps" to the spot where the <--more--> tag is set in the post. If you do not want that "jump", you can change the default function of how this works by editing the following line in wp-includes/template-functions-post.php

(NOTE: In WP 2.1 and later, the code is found in wp-includes/post-template.php ).

(NOTE: When you upgrade WordPress, this file will be replaced so make a note of the change so you can change it again after upgrading.):

$output .= ' <a href="'. get_permalink() 
. "#more-$id\">$more_link_text</a>";

to

$output .= ' <a href="'. get_permalink() 
."\">$more_link_text</a>";

or

$output .= ' <a href="'. get_permalink() 
.'">$more_link_text</a>';

Designing the More Tag

Seeing that you know how it works, now look at how we can make this little invitation to continue reading your post be more inviting.

By design, the_content() tag includes a parameter for formatting the <!--more--> content and look, which creates a link to "continue reading" the full post.

By default, it looks like this:

and I told him that he should get moving or I'd be on him like a limpet. He looked at me with shock on his face and said [[WordPress:#Designing the More Tag|more...]]

If you want to change the words from more.... to something else, just type in the new words into the tag:

<?php the_content('Read on...'); ?>

Or get more sophisticated and make it fun:

<?php the_content('...on the edge of your seat? Click
here to solve the mystery.'); ?>

You can style the text in the tag, too.

<?php the_content('<span class="moretext">...on the edge of
your seat? Click here to solve the mystery.</span>'); ?>

Then set the moretext class in your style.css style sheet to whatever you want. Here is an example of the style which features bold, italic text that is slightly smaller than the default text and uses the font-variant: small-caps to force the text into small capital letters. :

and I told him that he should get moving or I'd be on him like a limpet. He looked at me with shock on his face and said [[WordPress:#Designing the More Tag|...On the Edge of Your Seat? Click Here to Solve the Mystery.]]

Some people do not want the text and prefer to use an extended character or HTML character entity to move the reader on to the full post.

<?php the_content('&raquo; &raquo; &raquo; &raquo;'); ?>

Would look like this:

and I told him that he should get moving or I'd be on him like a limpet. He looked at me with shock on his face and said [[WordPress:#Designing the More Tag|» » » »]]

There is another parameter in the_content() template tag which will include the title of the post in the more text. Through the use of the_title() tag, the title of the article is included:

<?php the_content("...continue reading the story
called " . get_the_title('', '', false)); ?>
and I told him that he should get moving or I'd be on him like a limpet. He looked at me with shock on his face and said [[WordPress:#Designing the More Tag|...continue reading the story called A Tale That Must Be Told]]

Having a custom text for each post

Although the_content() is usually called from the template with a standard text as described above, it is possible to have an individual text for certain posts. Simply write <!--more Your custom text -->.

Adding An Image

The design possibilities with CSS are practically unlimited, and WordPress allows you to use images in many of their WordPress:Template Tags, including the more tag. To add an image, there are two ways to do it. Begin with the most simple -- listing it in the_content() template tag.

This examples features the image of a leaf after the "Read More" text.

<?php the_content('Read More...<img src="/images/leaf.gif" 
alt="read more" title="Read More..." />'); ?>

Notice that the code uses an ALT and TITLE in the image tag. This is in compliance with accessibility and web standards, since the image is both an image and a link. Here is what it might look like.

and I told him that he should get moving or I'd be on him like a limpet. He looked at me with shock on his face and said Read More...   leaf

You could even add a style to the image and more tag, as mentioned above, to style it even more. To use the image without the "Read More" text, just delete the text.

The second example uses the CSS background image. We have described how to use style classes in the above examples. This is a little tricker. The container's style must be set to allow the background image to show out from behind the text. If you were to use the above example as a background image, the style.css style sheet for this might look like this:

.moretext {
   width: 100px; 
   height: 45px; 
   background:url(/images/leaf.gif) no-repeat right middle;
   padding: 10px 50px 15px 5px}

The 50px padding against the right margin should make sure the text is pushed over away from the image as to not overlap it. The height ensures that the container will expand wide enough so the image is visible within the container, since a background image isn't "really there" and can not push against the container's borders. You may have to experiment with this to fit the size and shape of your own image.

You have gotten the basics. From here, it is up to your imagination.