views:

39

answers:

2

I have been trying to figure out how to display a advertisement on a wordpress blog every N posts..

I have found lots of plugins and guides that show you how to print a ad which just counts how many posts are on the current page.. (ex.. print ad every 3 posts)

My problem is, it seems it just counts only the page that you are currently on.. For example.. If I were to say have 10 posts per page, and want to print every 15.. It will not work.

Is there a way to count overall posts rather than what is on the current page? I essentially want to display a ad every page and a half.

A: 

Maybe something like this would work:

$page_num = $paged ? intval($paged) : 1; // current page
$item_no = get_option("posts_per_page") * ($page_num - 1); // start counting from
$show_ad_every = 15;

function show_ad() {
    global $item_no, $show_ad_every;

    if (($item_no % $show_ad_every) == 0) {
        // show the ad...
    }

    $item_no++;
}

add_action("the_content", "show_ad");
Brendon
A: 

I just ended up writing a custom field.

Bruno