Posts Tagged ‘Wordpress’

WordPress: Custom Post Order

September 25th, 2009 No comments

I recently had a requirement for custom ordering on WordPress categories and thought that I would share how I did it.

All that is needed is this piece of code which essentially modifies the SQL statement being generated. Note that the argument assigned to orderby is the column name, hence it is case sensitive (I found out that ID breaks the naming conversion on the post table);

   if (is_category(array($myCatId))){
      query_posts($query_string . "&orderby=ID&order=DESC");

Code needs to be placed before this line

<?php if (have_posts()) : while (have_posts()) : the_post(); update_post_caches($posts); ?>
Categories: Code, General, PHP, Wordpress Tags: ,

WordPress Adsense – Ad Targeting Redirect

August 20th, 2009 4 comments

When I was a Adsense n00b I made the mistake of adding adding a witty statement in my tagline in wordpress which confused adsense in terms of the content of my site, despite all the search engine optimization I had done . The symptom was: only the main index of my site had ads targeted solely on the wordpress “tagline” (the strip of text under the main title), all other pages on my site were targeted correctly. I thought this would be a matter of simply changing the tagline…but no, google doesn’t forget! I wont go into detail about the type of ads that were being targeted, lets just say they weren’t ideal.

A simple effective work around is a header redirect (permalink friendly) on the main site index which can be prefixed to the wordpress index.php in your wordpress top level directory.


// set $redirectHome = false to disable the redirect

$redirectHome = true;
if ($redirectHome){
        // Redirects index page
        $uriStr = $_SERVER['REQUEST_URI'];
        if (($uriStr == '/') || ($uriStr == '')){
                header('Location: /?index',true,301);
        // Makes sure pages off the main index are not renamed 
        $matchAry = array();
        if (preg_match('/(\/page\/\d+\/)\?index/',$uriStr,$matchAry)){
                header('Location: ' . $matchAry[1],true,301);

 * Original WordPress index.php goes here

This works because Google thinks “/?index” is a new page (it’s not though, it’s the main index with a query string attached) and the media bot crawls it and targets it accordingly as a new page. And as long as you’ve made your amendments to your previously incorrect content, in my case the “tagline” then this time around your ads should be accurately targeted…hopefully :)

The curious thing is, it seems no matter what only my original main index is targeted incorrectly still months later. I’ve seen other posts relating to incorrect ad targeting on the main index only. It seems like Google’s media crawler doesn’t like something about WordPress, I would like to hear from others that have experienced this.

WordPress Permalinks Apache Setup

August 19th, 2009 1 comment

Thought I’d includes some instructions for setting up “WordPress Permalinks” (which don’t work by default) which means you can get rid of those ugly WordPress URI’s and replace them with something more human readable.

Assuming you have inserted the runtime mod_rewrite DSO or complied Apache with —enable-rewrite then in order to get WordPress permalinks to function you need to include the following in your Apache config file (httpd.conf):

The quoted block below needs to go inside your virtual host section for your WordPress installation. Once Apache has been restarted your permalinks menu inside “Wordpress Admin” can be used and now your new URL’s should be functional.

<Directory /www/mywordpress/>
  RewriteEngine On
  RewriteBase /www/mywordpress/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]