Mashup of posts on your Wordpress Homepage

Wordpress logo

I’m starting to think that idea of showing a bunch of full posts on homepage isn’t always the best solution to choose, so I think we can do something with it. The goal is clear - I want to show complete content of newest posts, and only excerpts of older writings. Wordpress engine offers 2 appropriate template tags that provide showing our expected result. You might find them somewhere in your main index template file.

  • the_content that displays the contents of the current post
  • the_excerpt that shows only the post excerpt, as a replacement to the_content() within the Loop

In common use, you are able to show on your Wordpress homepage only complete post contents, OR only excerpt variants of your posts. You look on the clear concept above, and you see that we have to fix it if we want to show both of them on homepage at the same time.

Solution looks pretty simple, however I must say that it had taken a nice piece of time before I produced code that works properly. Also I had to ask my friend for some help with it, because there were some problems in showing more than one post filled with full content. Below you can see complete code solution. If you want to use it on your Wordpress template directory, open index.php file, and replace <?php the_content(__('Read more...')); ?> fragment with code listed below.

<?php if ( is_home() && ($post==$posts[0] || $post==$posts[1] || $post==$posts[2]) && !is_paged() ) : ?>
<?php the_content(__('Read more...')); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<?php endif; ?>

Also, there is one other thing that you have to add to your index.php file. It will give you a chance to be ok with the template order, by setting different limits of posts. First number for most-recent posts on your homepage, and the second for older ones that your readers see when they click Older Posts link (you can set that number somewhere within Wordpress Dashboard).

Right before opening <div class="post"> in your index.php file put the code below. Pay attention to your ability to replace my ‘7‘ with other number ;-)

<?php static $ctr = 0;
if ($ctr == "7") { break; }
else { ?>

And after closing that tag you also need to insert another piece of code served below. Be careful and count </div> tags precisely, remember that there are other DIVs used in code of your Wordpress theme!

<?php $ctr++; } ?>

Done. If you have any problems, feel free to comment that post ;-)

Tags: , ,

Commented 9 times

  1. Jez says:

    Thats pretty cool I think Ill lift that ;-)

  2. Andrea Micheloni says:

    I’m searching a way to show excerpts to search engines, and full posts to “humans”.
    That is cloaking, I know, but maybe with some cookies.. I’ll se. Or I’m going to use your solution, of course…

  3. Florchakh says:

    I’m afraid that you swapped words in your comment, because you can do it pretty easy - by requiring password for posts, or registration, by Flash, by JavaScript, by providing content as images (sick! ;) ).

    Cloaking we call showing MORE content to search engine, less to users. Like hidden links, or hidden keywords, for e.g. In fact that practice totally sucks, and definitely I don’t recommend cloaking for you, my friend…

  4. Andrea Micheloni says:

    Thanks man, I missed that, now I know it’s not “illegal” the ways to do that are so many.. I’ve just got to choose the fastest way.
    More to come about this, of course…

  5. Bertrand says:

    Thank you very much Florchakh, that was exactly what I needed.

  6. Jeux-Filles says:

    WoO great, I’m really happy to see that someone has already developped something I needed… Thanks a lot Sir ;)

  7. moserw says:

    Thanks. Exactly what I was looking for.

    http://www.nela.in/

  8. slamfest says:

    I’m trying not to use post on my homepage to but i am out of ideas

  9. Bart says:

    @slamfest - so what exactly do you want to be shown on the homepage? If it is a single page, you should find it somewhere in the Wordpress Dashboard options.

Leave a Reply