Long category descriptions in wordpress

Saturday, October 21st, 2006 - Español English

Here is the outline of what I did to get some (translated) text displayed at the beginning of every category page. This is mostly a reminder to myself in order to be able to redo it in the unlikely case I upgrade Wordpress and have to go through it once again.

  1. Add the column to the categories table.
    ALTER TABLE wp_categories ADD COLUMN category_long_description LONGTEXT NOT NULL DEFAULT '' CHARACTER SET utf8 COLLATE utf8_general_ci AFTER category_description;
    Actually I intended to use category_description for this, but in my template it is used as the link’s title in the category listing on the sidebar.
  2. Create a file category.php in the theme’s directory. In my case it almost a copy of archive.php, but with the following line added:

    <div class="categorydescription"><?php category_long_description(); ?></div>
  3. Add that function (category_long_description()) to wp-includes/template-functions-category.php. Here is the code:

    /**
    * Returns the long description for a given category
    *
    * @param int $category Category identifier. Leave empty to default to current category.
    */
    function category_long_description($category = 0) {
    global $cat;
    if (!$category) $category = $cat;
    $category = & get_category($category);
    return apply_filters('category_long_description', $category->category_long_description, $category->cat_ID);
    }
  4. Tell polyglot to hook to the newly created filter to translate the text in the table. And by the way, tell it to hook to the filter category_description as well, because it should already be there. Edit plugins/polyglot.php and add:


    // category descriptions should not have <more> tags. That's why we don't use lang_picker_respect_more().
    add_filter('category_description', 'lang_picker',1);
    add_filter('category_long_description', 'lang_picker',1);

  5. Last, modify the control panel to add the form field for the categories’ descriptions. The hack is so easy that it just deserves light comment: in file wp-admin/category.php add the textarea in a couple of places, name it category_long_description, add the matching $category_long_description variable everywhere it is needed. Simple copy&paste&edit of the code for category_description will work.
  6. Done. You can check that it works (or at least it should!) browsing some of the categories on this site (I haven’t added descriptions to all of them)

Leave a Reply

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