Tag Archive: WordPress

Custom Templates for Custom Post Types in WordPress

Building on my previous post about creating archive pages for custom post types, there’s another piece to add to the puzzle.

You archives pages will use the default index.php template. If you want to use a different template, you’ll need to hook into the template_redirect action to specify your template file. I like to use a file name like type-MY_POST_TYPE.php (although that’s easily changed), using the function below.
Continue reading

Archives for Custom Post Types in WordPress

When you create a new post type in WordPress using register_post_type(), WordPress does not automatically create a page for listing the archives of that post type. It’s possible to create one, though, without too much hassle.

For example, you can create an post type called article, and set the permalink for that post type to articles (i.e., an article will have a URL like http://example.com/articles/my-article-title).

Where do you go for a list of all articles, though? You might assume http://example.com/articles/, but you would be assuming erroneously. You could create a page with the slug articles and use a custom template for that page, but that doesn’t work with paging (e.g., http://example.com/articles/page/2/).

Instead, you need to fiddle with WordPress’s rewrite rules. (And thanks to Andrew Wilson for helping me understand this.)
Continue reading

Limit to One Post on WordPress Front Page

Let’s say you have a WordPress blog. You only want one post to appear on the front page of that blog. But, you want all other pages (e.g., the second page, category pages, monthly archives, etc.) to display more than one (be it 5, 10, or whatever you chose on your “Reading Settings” page). The first part is easy, the latter has a few pitfalls to be aware of.

First, we’re going to call query_posts to modify the query a bit. We only want this on the home page, so we wrap it in an if statement:

global $wp_query;
if ( is_home() && !is_paged() ) {
  query_posts(
    array_merge(
      $wp_query->query,
      array('posts_per_page'=>1)
    )
  );
}

is_home() will be TRUE if you’re on the main blog page; !is_paged() verifies that we’re on the first page (because is_home() is still TRUE on subsequent pages (e.g.http://example.com/page/2/)).
Continue reading

Custom Permalinks for Custom Post Types in WordPress 3.0+

Update 2010-10-04: See Understanding Rewrite Tags for a somewhat more helpful treatment of the subject.

I’ve been playing around with WordPress 3.0 to create a bug/issue-tracking plugin (i.e., using WP to track bugs, not to track WP bugs (although it could conceivably do that)). More details about the plugin will be forthcoming, but now is as good a time as any to share the solution to one problem I came across.

Creating New Post Types and Taxonomies

When you create a new post type, you use the register_post_type function. When you create a new post type through that function, WP creates a new rewrite rule base on the name you give the post type. In my case, I’ve created an issue post type, so all issues, by default, will have the permalink /issue/%issue%/ (where %issue% is replaced with the post slug). Using the rewrite argument to register_post_type, you can change the beginning of the permalink to something different (e.g., /bugs/%issue%/) should you so desire.
Continue reading

Save Time with sed

When I develop WordPress sites, I find that I end up repeating many identical tasks for each site. The phrase “repeating identical tasks” should (and does) set off alarms: This should be automated! To that end, I’m trying to learn some more command line tools for doing tasks that I currently use a GUI for.

One such task is migrating my development database to the staging/production server. Basically a mysqldump from my local database that I can then import on another server. A key thing to watch out for with WordPress, though, is the base URL for your site. If you’re serving a site from localhost while you develop it, you’ll need to change every occurrence of localhost in your database to the base URL for the new server.

sed is a command line tool for doing just that job. It runs a regular expression search on the input and outputs the replacement. Example:

$ echo "localhost" | sed "s/localhost/www.example.com/g"
www.example.org

Pipe your mysqldump through sed to have an SQL file ready for your new server.

$ mysqldump -uusername -ppassword database_name | sed "s/localhost/www.example.com/g" > database.sql

This way I avoid having to open the SQL file in a text editor, doing a global search and replace, and re-saving.