Specifying a Default Primary Category


Published on July 25, 2005 at 1:28 PM EST
In the Tutorials category.

While the below method works fine, a simpler and more robust approach exists in the free Default Category plugin I wrote.

category.gif

When running Movable Type out-of-the-box, a primary category isn’t picked when you start a new entry. Having a default category can help ensure that entries are categorized correctly and—if one category is most popular—eliminate a little work, too.

It’s a quick and simple hack to the edit_entry.tmpl file to make this happen. It’s also another good opportunity to use alternate templates (instructions are available).

Choose a Default Category

First, you’ll need to pick a category to be the default, and select the HTML that goes with it. I decided to setup the “Elsewhere in Our Life” category as the default, and will use this as an example.

http://www.eatdrinksleepmovabletype.com/upload/2005/10/view-source-thumb.gif

Log into MT and click “New Entry,” just as though you were going to start something new. Right-click on the page and choose “View Source,” “View Page Source,” or similar. Scroll down until you find the category listing, which’ll look something like in the thumbnail at the right. Find the category you want to be the default and copy that line. Mine looks like this:

<option value="31">Elsewhere in our Life</option>

“Elsewhere in our Life” is the name of the category, and “31” is the number MT has applied to it.

Set the Default Category

Next, you’ll need to open your edit_entry.tmpl template file. I recommend setting up alternate templates for this, but if you don’t want to you’ll find edit_entry.tmpl in the folder tmpl/cms/.

Search for “Primary Category” or scroll down to line 183. You’ll see some code that looks like this:

<TMPL_IF NAME=DISP_PREFS_SHOW_CATEGORY>
<label for="category_id"><MT_TRANS phrase="Primary Category"></label><br />
<select name="category_id" id="category_id" tabindex="2" onchange="doAddCategory(this)">
<TMPL_LOOP NAME=CATEGORY_LOOP>
<TMPL_IF NAME=CATEGORY_LABEL>

This is where you’ll add your default category. In the example below, I’ve bolded the addition.

<TMPL_IF NAME=DISP_PREFS_SHOW_CATEGORY>
<label for="category_id"><MT_TRANS phrase="Primary Category"></label><br />
<select name="category_id" id="category_id" tabindex="2" onchange="doAddCategory(this)">
<option value="31">Elsewhere in our Life</option>
<TMPL_LOOP NAME=CATEGORY_LOOP>
<TMPL_IF NAME=CATEGORY_LABEL>

Save the template. Go back to the MT interface and the New Entry page. You should see your default category in place!