Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I got some code from this site which is going to work great for me. If ound it in another post. What I have modified it to do is add a text box in the category edit area so that the user can input the path to an image. It is working. I have checked the database and I do see the option called "paradiso_category_fields_option" in that option is an array entitled "image" and there is a path to an image. I have displayed this image in the admin area but have not been able to get it on the front of my site. I have tried like so

 $cat_image = get_option(paradiso_category_fields_option);
 <img src="<?php echo $cat_image;?>">

this gives me

 <img src="Array" />

so I tried a foreach loop and get nothing that way either. Not sure what I am doing wrong here. Here is the line from the database

 paradiso_category_fields_option
 a:1:{i:7;a:1:{s:5:"image";s:67:"..wp-content/uploads/2010/12/menuItem1.jpg";}}

I shortened the url so that it would fit better on this page.

here is the code I am using that make everything work in the admin area

 // the option name
 define('PARADISO_CATEGORY_FIELDS', 'paradiso_category_fields_option');

 // your fields (the form)
 add_filter('edit_category_form', 'PARADISO_CATEGORY_FIELDS');
 function paradiso_category_fields($tag) {
 $tag_extra_fields = get_option(PARADISO_CATEGORY_FIELDS);

?>

 <table class="form-table">
    <tr class="form-field">
 <th scope="row" valign="top"><label for="_category_image">Full URL to image</label>    </th>
 <td><input name="_category_image" id="_category_image" type="text" size="40"      aria-required="false" value="<?php echo $tag_extra_fields[$tag->term_id]['image']; ?>" />
 <p class="description">This needs the full path to the image you want to use.      Example, <br  />http://www.mysite.com/wp-content/uploads/myImage.jpg.</p>
 <img src="<?php echo $tag_extra_fields[$tag->term_id]['image'];  ?>" alt=""  />
        </td>
    </tr>
 </table>
<?php
 }


 // when the form gets submitted, and the category gets updated (in your case the      option will get updated with the values of your custom fields above
 add_filter('edited_terms', 'update_paradiso_category_fields');
 function update_paradiso_category_fields($term_id) {
   if($_POST['taxonomy'] == 'category'):
    $tag_extra_fields = get_option(PARADISO_CATEGORY_FIELDS);
    $tag_extra_fields[$term_id]['image'] = strip_tags($_POST['_category_image']);
    update_option(PARADISO_CATEGORY_FIELDS, $tag_extra_fields);
   endif;
 }


 // when a category is removed
 add_filter('deleted_term_taxonomy', 'remove_paradiso_category_fields');
 function remove_paradiso_category_fields($term_id) {
   if($_POST['taxonomy'] == 'category'):
     $tag_extra_fields = get_option(PARADISO_CATEGORY_FIELDS);
     unset($tag_extra_fields[$term_id]);
     update_option(PARADISO_CATEGORY_FIELDS, $tag_extra_fields);
   endif;
 }

EDIT -

I have gotten closer to figuring this out. I have modified loop.php with this code

 <?php foreach ((get_the_category()) as $category){
 $cat_image = get_option('paradiso_category_fields_option'); ?>
 <img src="<?php echo $cat_image[$category->cat_ID ]['image']; ?>" />
 <?php } ?>

So that does return the two images it is suppose to but it also returns the first image a few more times. Once for each title in that specific category. Is there a way to target the main category instead of each child category?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Remember you need the get_option parameter in quotes, like this:

$option = get_option('paradiso_category_fields_option');

and to get the img url out of that database option you presented it would be:

echo $cat_image[7]['image'];
share|improve this answer
1  
named constants do not need quotes –  Milo Sep 25 '12 at 4:41
    
well, that did work. Problem is that it returned the image 3 times. I know it did that because I have three posts in that category. I was hoping it would return the image for the parent category which would be lunch and not all the posts that were posted to the lunch category. Got any suggestions for that? –  Jamie Sep 25 '12 at 4:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.