This tutorial allows you to add a text field to fill the URL of an image for each of your categories.

Add this in your functions.php file:

[sourcecode language="php"]add_action('init', 'my_category_module');[/sourcecode]
[sourcecode language="php"]function my_category_module() {
 add_action ( 'edit_category_form_fields', 'add_image_cat');
 add_action ( 'edited_category', 'save_image');
 }[/sourcecode]

The function to add the extra field :

[sourcecode language="php"]function add_image_cat($tag){
 $category_images = get_option( 'category_images' );
 $category_image = '';
 if ( is_array( $category_images ) && array_key_exists( $tag->term_id, $category_images ) ) {
 $category_image = $category_images[$tag->term_id] ;
 }
 ?>
 <tr>
 <th scope="row" valign="top"><label for="auteur_revue_image">Image</label></th>
 <td>
 <?php
 if ($category_image !=""){
 ?>
 <img src="<?php echo $category_image;?>" alt="" title=""/>
 <?php
 }
 ?>
 <br/>
 <input type="text" name="category_image" id="category_image" value="<?php echo $category_image; ?>"/><br />
 <span>This field allows you to add a picture to illustrate the category. Upload the image from the media tab WordPress and paste its URL here.</span>
 </td>
 </tr>
 <?php
 }[/sourcecode]

Function to save :

[sourcecode language="php"]
function save_image($term_id){
 if ( isset( $_POST['category_image'] ) ) {
 //load existing category featured option
 $category_images = get_option( 'category_images' );
 //set featured post ID to proper category ID in options array
 $category_images[$term_id] =  $_POST['category_image'];
 //save the option array
 update_option( 'category_images', $category_images );
 }
 }
}
[/sourcecode]

To display the image put this in “category.php” or “archive.php” depending on your theme, it’s better to choose “category.php”,  and add this code before the WordPress loop :
:

[sourcecode language="php"]
<?php
 if(is_category()){
 $category_images = get_option( 'category_images' );
 $category_image = '';
 if ( is_array( $category_images ) && array_key_exists( get_query_var('cat'), $category_images ) ){

$category_image = $category_images[get_query_var('cat')] ;
 ?>
 <img src="<?php echo $category_image;?>"/>
 <?php
 }
 }
 ?>
[/sourcecode]

Before applying the changes in this tutorial, make sure your WordPress hosting provider you are using is good enough to offer the right amount of bandwidth that can support the new changes.

You May Also Like

Why WORDPRESS is better?

You might have started your own online business. Well, to make it work, you need to own a site. However, if you do not know much about programming languages, then it would benefit you a lot to make use of the simple and easy ones. In this matter, it would be a very good idea […]

Don’t lose your visitors when your database is unavailable

When your WordPress blog has trouble connecting with the database, it returns a nice “Error Establishing a database connection”. Instead of losing visitors, db-error.php might be the useful solution. You should create and place it in the wp-content folder. After that paste this short code in it. [sourcecode language=”php”]<?php // db-error.php $host=$_SERVER[‘HTTP_HOST’]; $uri=urlencode($_SERVER[‘REQUEST_URI’]); header("Location: http://google.com/search?q=cache:$host$uri"); […]

Can Ghost, the new comer win a significant market share?

For some years now, WordPress (WP) has established itself in the blogging world, as the leader in web content creation and management. Born in 2003, the software which is used with PHP has won a multitude of bloggers. It certainly takes some knowledge in the field of computer science to use WordPress properly. But, past […]

What’s New In WordPress 4.3

  WordPress is the most widely used content management system in the world. It is an excellent platform for beginners to launch their blog without needing expert knowledge on HTML. WordPress is also useful for sole proprietors who want their business to have an online presence but at the same time do not want to […]