Full Post or Excerpt: Can You Display Both?
There is a bit of a debate about whether to display full text or excerpts on the main page of your blog. I sit in the middle of this debate: I like displaying full text for the most recent post, but prefer excerpts for everything else. I get this balance by manually inserting a “more” tag into the previous post once I publish a new one. Since I only publish 3 times a week, and check my blog on a daily basis, this manual effort isn’t a big deal. I would, however, prefer to automate this manual task which I recently did by altering my index.php file in the following manner.
Add a counter and small conditional statement to the “while post loop”
My display requirements are simple: display the full text of the newest post and only the excerpt for all remaining posts. I can easily get the desired results by adding a counter to “while post loop” and increment it through each pass. Then, I just use the value of the counter to determine what to display. Here is how I did it:
1. I found the “while post loop” statement that controls the display of each post:
< ? php while (have_posts()) : the_post(); ? >
This loop causes all of your posts up to the setting specified in “show at most” (found on the Options - Reading menu) is reached.
2. Right before that “while post loop” statement I declare a counter variable and set it to zero:
< ? php $counter=0 ? >
3. Inside the “while post loop” I found the statement that displays the content of the post:
< ? php the_content('Read the rest of this post »'); ? >
4. I built a simple conditional if statement around this line of code:
< ? php if($counter <= 0) { the_content('Read the rest of this post »'); $counter++; } else { the_excerpt();} ? >
This conditional statement tests the value of $counter, if it is less than or equal to zero, then the complete text gets displayed and $counter incremented by one; however, if the value is greater than zero, then the excerpt gets displayed, which is exactly what happens during the second iteration of the “while post loop.”
If I decide later that I want to display the full text for the two most recent posts, then I just change the test value from zero to one. That’s why I test to see if $counter is less than or equal to. Currently, I really don’t need to do more than test for equality but decided to test for “less than” to give myself the flexibility in the future if I decide to do more with this conditional statement.
Don’t forget to test it first
Please keep in mind that all PHP code displayed in this post have extra spaces inserted in order to avoid display problems. If you decide to use this code, please remove the spaces if you cut-n-paste the code snippets. Also, I’ve implemented this conditional test on my WordPress test server (I recommend you do the same). I haven’t moved it over to this public facing blog yet because I still want to tweak a few things. Once I’ve sorted out those details, I’ll upload the changes and won’t need to manually insert “more” tags any longer. Oh how I love automating manual tasks!
Share, Bookmark, or Email this post
|
|
If you liked this post, subscribe to TechTraction's RSS feed or TechTraction's email feed
Filed under: Blogging Related

[...] Full Post or Excerpt: Can You Display Both? - Bret Swedeen writes a nifty little wordpress trick for you all. [...]
Excellent Bret.
I’ll be testing this on my site.
Great!! thanks for share.
David, make sure to let me know how the tweak works for your site. I’m curious to find out if others find it easy to implement.
Andrew, thanks for the positive feedback.