| Subcribe via RSS

Display Single Post on Home Page in WordPress

December 11th, 2011 | Comments Off | Posted in PHP, WordPress

Most themes have some sort of post roll that lists out the most recent posts on the home page. In most cases this is the ideal way to get your readers up to date on your most recent information. Say you had a more traditional website that displays what your company is about on the home page and maybe your recent or most read blog posts are relegated to the sidebar. So how do we display just a single post in that home page body section? Pretty simply it turns out.

Just prior to the php while loop (look at the file located in Appearance > Editor > index.php) set the query_posts method equal to the id of your post. So you’d have something that looks like this.

Display Alternate Sidebar Based on Page in WordPress

October 13th, 2009 | Comments Off | Posted in PHP, WordPress

I don’t know about you, but I get tired looking at that same right sidebar on every page of my WordPress sites. Not all pages are created equal, and some need to serve up different content based on where we are in the site. For my particular case, I wanted to display custom content to the last page in a checkout process. To do this we have to first determine which page we are on by looking at the $_SERVER['REQUEST_URL']. If we were on http://aspnetprogrammer.net/hello_world, it would return the value hello_world. Next, we set the queryValue of the page we are looking for then trigger our comparison. The strstr function in php compares two strings, telling us if the second is contained within the first. If it is, we display the checkout.php sidebar. Otherwise, we let the default sidebar load as normal.

Alternating Row Color in PHP

June 25th, 2009 | Comments Off | Posted in CSS, PHP, Uncategorized

In ASP.NET setting the background color for alternating rows is very simply. Just update the property on the Gridview and whalah! Crayola just marked up your database output in pretty colors. In PHP, its not quite as simple, but not terribly complicated either. Basically, we are going to set the odd and even row background colors in our css stylesheet then increment our loop value as we cycle through the records and dynamically populate our class value on the tr for the given row.

Stylesheet

PHP Code

This is a JSON result set we are looping through. The key line to focus on here is row 9 in the PHP code. This appends the i value we’ve been incrementing via the loop to the tr class property. This gives us that clean alternating row look within minimal work.

Tags: , ,

PHP Banner Ad Rotator

June 18th, 2009 | Comments Off | Posted in PHP

I seem to be doing a lot of work in WordPress these days and you know what that means — PHP development. Prior to WordPress, I’d never even thought of what PHP code might look like. Now I’m all too familiar with it. Not a bad language I have to say. Back to the subject at hand. I have banner ads at the top of these pages. While its ok to drop an image into a page, I get rather bored looking at the same ad. So what if we wanted to display a bucket of ads, basically in random order. We’d need some kind of ad rotator. We’re going to accomplish this with two files: adrotator.php that houses our php code and ads.txt that holds our series of banner ads and corresponding links.

adrotator.php

So the first line gets our file containing our ads so line 2 can split the contents up into an array breaking when it encounters the tilda. Line 3 queries the array to get our count then selects a random place within the high and low range. Finally we pluck out the banner that lines up with our random value and print it out to the browser. Next let’s take a quick look at the ads.txt file.

ads.txt

Nothing too challenging there. Finally we have to put that banner into our php code. We attach it like any other include file:

Kudos to phpbuddy for the original code on this post was drawn from.

Tags: , ,

Disable timthumb PHP script

May 18th, 2009 | Comments Off | Posted in PHP
I have a blog template that I’m using for one of my sites (Scarlett for those who are interested), and it has this cool PHP script built in that will take your image files and dynamically resize them to fit a specific dimension you’ve set on your webpage. Well it would have been cool at least had I got it working. Godaddy seems to have some real issues in allowing the script to write out the temporary image files. It just leaves nice little blank spots where the image is supposed to be. Now I’m a savvy developer and have no issue concerning producing a couple different size variations of my images to load up to the server. What I need is to disable the timthumb script from trying to intervene. Here is some sample code where we are employing timthumb:

Here is that same code where we extract that script reference:

Tags: ,