David Nash

Wordpress Guru Sydney

WordPress: Limit Archives to Single Category

Posted on December 3, 2011

Late yesterday afternoon a client asked if I could look at a business’s WordPress installation. They had posts in several categories but only wanted to show the “Latest News” posts in the archives.

WordPress is designed around blog posts, whereas I find that many business or company sites are designed around pages (eg About Us, Contact Us) – and don’t use WordPress’ powerful blogging tools on the front page. My site is an example – the content on the home page doesn’t change that much.

This means that many of the solutions are also geared around blog-post design.

While searching I found a few examples of how to exclude a specific category from the archives, when what I needed was to include only one category in the WordPress archives – generated in the theme by wp_get_archives().

The solution is to add a filter that modifies the SQL used by wp_get_achives() to the theme’s function.php:

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

function customarchives_join( $x ) {
global $wpdb;
return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";
}

function customarchives_where( $x ) {
global $wpdb;
$include = '1'; // category id to include
return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";
}

Replace ’1′ with the category ID (wp-admin > posts > categories > edit, then in the address bar it is directly after ‘&tag_ID’) and that’s it. wp_get_archives() will now only display posts from that category.

I found this in http://wordpress.org/support/topic/wp_get_archives-and-conditional-tags?replies=6#post-1578041 (thanks Paul!).

Using PHP variables that contain hyphens

Posted on November 25, 2009

This took me a little while to work out. I’m using PHP5′s SimpleXML to parse XML into a PHP object. The XML has entries like <HELLO-THERE>.

But you can’t use $xml->HELLO-THERE because it reads the hyphen as a minus.

Instead, use $xml->{“HELLO-THERE”}

Easy!

Dynamic Font/Image Replacement in Silverstripe

Posted on March 10, 2009

I’ve recently started creating sites with SilverStripe CMS, and I’m loving it.

My client wants nicely rendered non-standard font titles that fade in and out, without using javascript.

Here’s how I’m going to accomplish it:

  1. In Silverstripe’s mysite/code/Page.php I overload the onBeforeWrite() call in my Page class. This intercepts the data before it is written to the database. I can get the title of the updated page from $this->Title
  2. I will then use PHP’s GD (graphics) library to create a PNG with a transparent background. I’ll use imagettftext() to load a font from a TTF file and write it to the empty PNG image. I can use Director::baseFolder() to work out the root of the site and store the title images in $this->ThemeDir.’/images/titles’.
  3. I will make sure that ‘images/titles/’ is writeable by the web server. I will make the filename the same as the title, with spaces converted to _ but otherwise stripped of punctuation.
  4. In the SilverStripe template I have created for the client, I will output a heading element with a span inside it. The span will contain the text of the title, so that search engines can read it.
  5. I will output inline CSS using the ‘style’ attribute of the heading element. It will look at the image file that was created in the CMS, get the width and height, and then set the title as the background.
  6. I will then apply jQuery effects to the title so that on page load it fades in, pauses for a few seconds and then fades out again. What the user sees is an image of the rendered text.

It won’t be a strain on the server as the text title is only generated when the page is saved in the CMS. It’ll still be read by search engines and non-graphics browsers, meaning that it is still accessible. But best of all it will look absolutely beautiful, without requiring a Flash plugin.

I’ve got most of the pieces of this working, I just haven’t glued it together yet. I’m looking forward to it! I’ll let you know how it goes.

Download ImageTitle.php (the ‘library’ I use to interact with GD)

Unix Time in MySQL and bash

Posted on February 4, 2009

A quick one today. I was working on a mysql database that used unix timestamp produced by PHP’s time() function.

I needed to be able to quickly convert this time to a human-readable format. In bash,

date -d @timestamp

is a quick way to convert.

In a terminal shell eg:

# date -d @1224992980
Sun Oct 26 14:49:40 EST 2008

In a MySQL client, you could also use

select date(from_unixtime(column_name)) from table_name;

Or if you want a little more flexibility in the output, for example outputting 27/02/09, you could do:

select date_format(from_unixtime(column_name), ‘%d/%m/%y’) from table_name;

This post is one of my most popular posts. Did you find the information you were after? Please tell me in the comments!

vim: Quickly assign POST variables in PHP

Posted on January 22, 2009

You’ve got a web form with lots of fields, and you want to POST them to a PHP script. Open vim, and list the INPUT tag’s NAME attributes, one per line.

<?php
firstname
lastname
address1
address2
state
postcode
?>

Now with some search-and-replace magic we can save ourself a lot of boring typing. Hit ‘escape’ to get out of insert mode, type a colon (“:”) and copy-paste this:

%s/^\(\S\+\)/\$\1 = \$_POST\[\'\1\'\];

I won’t bother explaining it unless someone asks. But what you should end up with is this:

<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$state = $_POST['state'];
$postcode = $_POST['postcode'];
?>

Perhaps not worth it when you have 6 variables like this example; but quite handy when you have 60 variables!