Hide Ads for Admin and Show at Only Single Post – WordPress

by Hiroshi on February 25, 2011

in WordPress

Where you show Ads at your blog? Many publishers show ads above the fold. That means above the content. And then below the content. Admin do not want to see ads because ads are for visitors and not for the admin. To further practice caution, admin can hide ads from his blog when admin himself is using the blog to avoid accidental ad click. My requirement was to hide ads from admin and show ads only when single post was being viewed. I implemented the following code.

We will use ‘Conditional Tags’ of wordpress.

<?php global $current_user;
get_currentuserinfo();
	if ($current_user->user_level == 10 ) { ?>
		// dont show ads
		<?php } else {   ?>
			<?php if (is_single()): ?>
				// show ads - put ad code here
			<?php endif; ?>
<?php } ?>

That means if user level is 10 (which is admin level), then ads won’t show up and in everybody else case if the post is single, only then ads will be shown.

If single post is not your requirement then the code becomes like this.

<?php global $current_user;
get_currentuserinfo();
	if ($current_user->user_level == 10 ) { ?>
		// dont show ads
		<?php } else {   ?>
		// show ads - put ad code here
<?php } ?>

You can use this code in singlepost/single file of theme above and below where the content tags are.

That’s it.

There are several plugins that can display ads at WordPress according to your need. You can display ads at the top of the post, at the bottom of the post and even in the middle of the post. Those WordPress Plugins are Adman and New Adman. These can fulfill the requirement to show ads above and bottom of post and you do not need to edit the code of singlepost of theme. But these will show ads to admin as well.

The above code will show the ads to only visitors and hide for admin of WordPress.

Related Posts

Previous post:

Next post: