HeyBuzz

Hey! I'm Pete, and I'm a designer... and this is my blog - about web design, for web designers. Here you'll find all kinds of tips, tricks, information and inspiration linking, in some way, to web design.

September 25th

Wordpress Trickery By Buzz

I’ve been doing a bit of WordPress development recently, and I’ve finally worked out how to do something that’s been bugging me for a while. How do you automatically insert something after the first, second or third (etc etc) post? For example, showing Google ads after the second post?

In my example, I wanted to insert my own advert (ad.php) after the third post on the homepage. In this instance, my posts are represented within a list, but it’s up to you how you do it.

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $loopcounter++; ?>

You’ll notice the Loop Counter - which counts how many times the loop is called on. Next we need to create an action when that counter reaches a certain number.

<?php if ($loopcounter == 4) { ?>
<li><?php include (TEMPLATEPATH . ‘/ad.php’); ?></li>
<?php } ?>

The above code counts when the loop is on it’s fourth rotation, and then it includes my advert. So the 4th item in the list will be my ad (which is styled accordingly). We then continue with the rest of the loop.

Limiting the post length

Whilst building this particular blog, the design (being pretty constrained) required the posts on the homepage to only be a maximum of 25 words. There are several ways to achieve this. First, and easiest is to just get the author to chuck a <!–more–> tag in after the first paragraph… but we all know what’ll happen when clients get their hands on stuff!

Secondly, you could use <?php the_excerpt; ?> - but that will call on the first 55 words (still not short enough). So I came across the following, which does the trick beautifully.

<?php the_content_rss(”, TRUE, ”, 25); ?>

This displays the post in RSS form, and defines the number of words in which to count, which in this case is 25 words. So as follows is the whole code:

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); $loopcounter++; ?>

<?php if ($loopcounter == 4) { ?>
<li><?php include (TEMPLATEPATH . ‘/ad.php’); ?></li>
<?php } ?>

<li>
<a href=”<?php the_permalink(); ?>“><?php the_title(); ?></a>
<p><?php the_content_rss(”, TRUE, ”, 25); ?></p>
<p><a href=”<?php the_permalink(); ?>“>Read more…</a></p>
</li>

<?php endif; ?>
<?php endwhile; ?>
</ul>

So combined with the above ad technique, and this limitation on words - you can achieve some quite experimental wordpress formats. Which is nice for me, because as a designer (primarily), I can suggest all this crazy new shit, safe in the knowledge that it can be achieved!

Tags: , , , , , , ,

1 Comment

  • Cool! Peshi ischo

    Alina
    Thu 6th November 2008
    reply

wanna say something?

If you've got an opinion about Wordpress Trickery - then why not say something? You can even subscribe to these comments via RSS!

:

Hey!

HeyBuzz is a collection of musings, learning's, tips and inspiration... mostly about digital stuff, as observed by Buzz.

Buzz is a sailor, skier, extreme sports enthusiast and designer living in London. He enjoys loud music, trying new things and talking crap. He doesn't really like reading though!

You can visit Buzz's sexy new design portfolio at BuzzGraphics.co.uk

Recent Posts

This is the personal blog of graphic designer, Pete Usborne. If you like what you've seen so far, then feel free to comment... if not, calm down - it’s just a blog!
View Pete’s design portfolio or Subscribe RSS