How to show custom static content at top of each category page?

I want to display custom static content, essentially an image and a small description, at the top of each category page (different for each category and sub category), displayed above a list of titles of the category posts with excerpts.

Is there an automated solution/plugin available for this? I need an automated way to insert text/image too because I want to pass this to my content writer and he will take care of the new categories. A rich text editor where I can insert image and text, which will be assigned to a category and can be shown on top of the category page.

Related posts

Leave a Reply

4 comments

  1. Simply call the category_description() – this displays the description that you can enter for each category that you add in the admin UI under “category” (or post tag or each other custom taxonomy).

    You can as well use the underlying API function:

    term_description( $category, 'category' );
    
  2. Here’s raw code for the DIY people.

    /** Add New Field To Category **/
    function extra_category_fields( $tag ) {
        $t_id = $tag->term_id;
        $cat_meta = get_option( "category_$t_id" );
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="meta-content"><?php _e('Landing Page Content'); ?></label></th>
        <td>
            <div id="catContent">
                <style type="text/css">.form-field input {width: auto!important;}</style>
                <?php wp_editor($cat_meta['content'], 'cat_landing', array(
                        'textarea_name' =>  'cat_meta[content]',
                        'textarea_rows' =>  15,
                )); ?>
            </div>
            <span class="description"><?php _e('Landing Page Content, Edit This Like You Would A Page.'); ?></span>
        </td> 
    </tr>
    <?php
    }
    add_action('category_edit_form_fields','extra_category_fields');  
    
    /** Save Category Meta **/
    function save_extra_category_fileds( $term_id ) {
        global $wpdb;
        if ( isset( $_POST['cat_meta'] ) ) {
            $t_id = $term_id;
            $cat_meta = get_option( "category_$t_id");
            $cat_keys = array_keys($_POST['cat_meta']);
                foreach ($cat_keys as $key){
                if (isset($_POST['cat_meta'][$key])){
                    $cat_meta[$key] = $_POST['cat_meta'][$key];
                }
            }
            update_option( "category_$t_id", $cat_meta );
        }
    }
    add_action ( 'edited_category', 'save_extra_category_fileds');
    

    With these you can actually do more, such as adding an ordering field or whatever your heart desires.

    In the action call you can change:

    add_action('category_edit_form_fields','extra_category_fields'); to your specific

    taxonomy by changing category to your taxonomy name as so:

    add_action('YOUR-TAX-HERE_edit_form_fields','extra_category_fields');
    add_action ( 'edited_YOUR-TAX-HERE', 'save_extra_category_fileds');
    

    Finally to get your meta content you can use this:

    $category_id = get_cat_ID();
    if($category_id != 0){
        $cat_meta = get_option( "category_$category_id");
        echo apply_filters('the_content', $cat_meta['content']);
    }
    
  3. I would use a plugin like ACF to create fields for the image and description of a category since a non technical person will be using it. Then you can query the taxonomy’s fields using the information found here. You can even use the_description() and just one custom field for uploading the image.