Home » API, Admin, Display, Headline, Oodle, PHP

Display the Oodle API with Wordpress using Custom Fields

23 May 2008 One CommentEmail This Post Email This Post

There is very little documentation available on the Oodle API. The official Yahoo discussion group is useful to an extent but there isn’t much going on over there. There is, however, some decent documentation at the Oodle developer center.

*Disclaimer: I am not an expert programmer. Far from it- everything I’ve learned I taught myself through trial and error and the powers of Google search. Hopefully that fact will make these articles even more useful to a lot of people because I will be able to relate to folks who are struggling through things just like I am.

Here is how I was able to coax Oodle into dynamically displaying classified ads for each Wordpress Post.

First, some notes on my website structure will help elucidate just what is going on here and why I am doing what I am doing.

The site is a local page displaying, among other things, items that are for sale in the area.

I have the site broken down into Categories which are very similar to the classified sections in a newspaper. For example, I have a Motorcycles Category that contains Posts for brands of motorcycles, like these:

http://www.Metro-East.com/Motorcycles/Kawasaki-Motorcycles

http://www.Metro-East.com/Motorcycles/Ducati-Motorcycles

Here is what I do:

I have a special Template that I use for each Category. This means that I can edit the Motorcycles Template and then every Post in the category uses that Template. (There are a lot of other conditional things I have going on with these pages, but we can get into that in another article, as none of it is important here.)

So, for example, in the Kawasaki-Motorcycles Post I insert my content (in this case, eBay listings) and then the Category Template wraps around that Post.

Next, I want the Category Template to display some classified ads from Oodle that relate to the Post content, which in this case is Kawasaki Motorcycles.

*Note: The reason I am dealing with the Category Template is that I only want to edit one file (in this case the Motorcycles Category Template) and have it work its magic for every Post in that Category. The other reason is that without using some fairly nasty plugins you cannot execute PHP in Wordpress Posts. You can only execute PHP in Templates.

So the question is, “How do I get Oodle to know what is in the content of the Post and then display relevant classified ads?”

The solution I arrived at was to use Custom Fields.

As you will see, this method will allow us to dynamically list Oodle classified ads based on the Post content and best of all it will be as easy as typing a few extra words when we create our Post.

In this case, I:

  1. Went into the Edit page for my Kawasaki-Motorcycles Post and scrolled down to Custom Fields.
  2. Typed “oodlekeyword” (without the quotes) into the Key section and typed “kawasaki” (without the quotes) into the Value section.
  3. Clicked ‘Add Custom Field’.

Having done this I had successfully told Wordpress that the “oodlekeyword” for the Kawasaki-Motorcycles Post was “kawasaki”. Wordpress then stored this information so that I could use it in the future.

(*Note: I chose to use “oodlekeyword” but you could use anything you want… like “oodle” “oodlead” “classified” or “banana”, just as long as you are consistent with its usage later.)

Next, I pasted my Oodle API code into my Motorcycles Category Template in the location I wanted my classified ads to appear. (In this case, directly beneath the Post content so as to create a near-seamless set of listings.)

I have highlighted the line where I injected the Custom Fields information into the code.

// calls the oodle class available from Oodle
include(”oodle_api_php5.php”);

//set number of pics to show
$showlimit=100;

//Set your date you want to check
$startdate=strtotime(’-4 weeks’);
$enddate=strtotime(’now’);

//*************** Settings - Customize them to fit your needs!

$oodleApi = new OodleApi();
// prepare a “get()” method call to the Oodle API
$filters['partner_id'] = ‘Your-API-Key-From-Oodle-Goes-Here’;
$filters['region'] = ‘usa’;
$filters['from'] = ‘0′;
$filters['to'] = ‘200′;
$filters['category'] = ‘vehicle/motorcycle’;
$filters['q'] = get_post_meta($post->ID, oodlekeyword, TRUE);
$filters['filters'][] =
array(’type’=>’distance’,'params’=>array(’value’ => ‘50′, ‘units’ => ‘mi’, ‘zip’ => ‘62234′));
$filters['create_time'][] =
array(’type’=>’create_time’,'params’=>array(’low’ => $startdate,’high’
=> $enddate));
$filters['filters'][] =
array(’type’=>’source’,'params’=>array(’exclude’=>array(’ebay’)));
$sort['sort'][] = array(’key’=>’create_time’,'reverse’=>false);

$response=$oodleApi->make_request(’get’,$filters,$sort);

//this loop sorts
//through the results and only shows those with
//pictures, and limits the number shown to 30…of course you can
//change this however you like…

$piccount=0;
foreach ($response['items'] as $key=>$ad) {
$picprinted=false;
foreach($ad as $key=>$val) {
if (($ad['thumb']) and
(!$picprinted) and ($piccount<$showlimit)) {

echo ‘<div style=”width:620px;margin-left:7px;margin-bottom:10px;border-top:1px solid #CCCCCC;padding-top:5px;”><span style=”float:right;width:495px;” align=”left”><b>’.$ad['title'].’</b><br/>$’.$ad['price'].’<br/>’.$ad['locationcity'].’<br/><a href=”‘.$ad['url'].’” target=”_blank”><font color=”gray”><u>See Full Details</u></font></a></span><span style=”width:125px;”><a href=”‘.$ad['url'].’” target=”_blank”><img src=”‘.$ad['thumb'].’” title=”From: ‘.$ad['locationcity'].’ on ‘.$ad['source'].’ “border=”0″/></a></span><br/><br/><b>Description:</b><br/><span style=”width:620px;”>’.$ad['sbody'].’</span></div>’;
$picprinted=true;
$piccount++;
}

}

}

This highlighted line tells Oodle to display the ads that not only adhere to all of the other $Filters I have used, but also to narrow the search down even more by only showing ads which relate to the Custom Field “oodlekeyword”. Having already set the “oodlekeyword” to “kawasaki” for Kawasaki-Motorcycles, Oodle knows to only display ads that match “kawasaki”.

Now this is where the magic comes into play. Without changing a single line of code, if you click on any Motorcycle Post it will automatically display the relevant classified ads as long as you took a couple of seconds to specify the Custom Field when you wrote the Post.

If you click on the Ducati Motorcycles Post Oodle will display Ducati Motorcycle ads because I entered the Custom Field “ducati” in the post and we told Oodle to display only those items which match the Custom Field, and so forth for each Post in the Category.

Now all that remains is to alter the Oodle API code for other Category Templates and to remember to include a Custom Field value for the key “oodlekeyword” when I write each post. (Every post in the Video Games Category, for example, will use the Video Games Category Template so I will edit only that one file to use very similar Oodle API code, changing it only to reflect a new category. Thus, $filters['category'] = ‘vehicle/motorcycle’; will become $filters['category'] = ‘sale/entertainment/video_game‘; as per the Oodle category list.)

I hope I did an adequate job of explaining this. I probably over-explained some parts and under-explained others, so I apologize for that. Please offer any questions or tips in the comments below.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • NewsVine
  • Propeller
  • Reddit
  • Slashdot
  • SphereIt
  • StumbleUpon
  • Technorati
  • blinkbits
  • BlinkList
  • Furl
  • Ma.gnolia
  • Spurl

One Comment »

  • Oodle Blog » Blog Archive » Props to Oodle API Developers said:

    [...] rolled up their sleeves, revised some Wordpress page templates, and inserted extra code to “dynamically [display Oodle ads] for each Wordpress Post“. They used the PHP5 client library that we posted on developer.oodle.com to call the Oodle [...]

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.