scriptygoddess

05 May, 2007

Insert text/image block in the middle of a WordPress category page

Posted by: Jennifer In: PHP|WordPress

For a long time I resisted the urge to put ads on the site. I'm sure you've noticed I finally caved in this past year. I've tried to keep it relatively unobtrusive. In one case I was contacted directly to put an ad up on one of my category pages in the content area. I didn't want to put it at the top above the content. Ideally – I wanted it put in the middle in a separate div block, maybe after 3 or so posts, but only on the first category page. Here is the code I used to get that to work…

First, above the loop I added this:
<?php
$count = 0;
?>

This is to count the number of posts we're on.

Then just after the end of the loop call (<?php while (have_posts()) : the_post(); ?>) but before you start the code for the posts, I added this:
<?php
if ($wp_query->get("page") || $wp_query->get("paged")) {
$onFirstPage = false;
} else {
$onFirstPage = true;
}
?>

That bit of code I got basically from this post on the support boards.

Then to determine if we should call the text for the ad (in this case I wanted it displayed only on category id #29), and to then display the ad…
<?php
if (is_category('29') && $count == 2 && $onFirstPage) {
?>
...ADVERTISEMENT TEXT OR IMAGE GOES HERE...
<?
}
$count++;
?>

Related posts:

  1. Striping IMG tags from the_content in WordPress (and how to fudge page excerpts) Background: For a site I was working on, I was...
  2. Adding a block of HTML to a WordPress post One problem with the WYSIWYG editor in WordPress is that...
  3. Multiple Featured Content Galleries On a site I was working on recently, the client...

Related posts brought to you by Yet Another Related Posts Plugin.

2 Responses to "Insert text/image block in the middle of a WordPress category page"

1 | Matt

May 8th, 2007 at 12:00 pm

Avatar

Thank you very much Jennifer – this worked a treat ! :)

2 | Pete

May 25th, 2007 at 2:28 am

Avatar

Hi.
I needed pretty much exactly the same solution for exactly the same purpose.

It worked great and is an elegantly simple solution.

Thanks!
Pete

Comment Form

Featured Sponsors

About


Advertisements