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 found this code for custom field add category editor.

I added custom field to the Category.

e.g. My field name is Hastag and i have a Sport category. And i want to call display Sport's custom field(I want to call current category custom field). How can i do this?

CODE:

// the option name
define('MY_CATEGORY_FIELDS', 'my_category_fields_option');

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

    ?>

<table class="form-table">
        <tr class="form-field">
            <th scope="row" valign="top"><label for="_ce4-categoryTitle">Full Category Title</label></th>
            <td><input name="_ce4-categoryTitle" id="_ce4-categoryTitle" type="text" size="40" aria-required="false" value="<?php echo $tag_extra_fields[$tag->term_id]['my_title']; ?>" />
            <p class="description">The title is optional but will be used in place of the name on the home page category index.</p></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_my_category_fields');
function update_my_category_fields($term_id) {
  if($_POST['taxonomy'] == 'category'):
    $tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
    $tag_extra_fields[$term_id]['my_title'] = strip_tags($_POST['_ce4-categoryTitle']);
    update_option(MY_CATEGORY_FIELDS, $tag_extra_fields);
  endif;
}


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

And im trying call with this code:

<?php
if ( is_category() ) {
    $current_cat = get_query_var('cat');
}
 echo $tag_extra_fields[$current_cat->term_id]['my_title']; ?>
share|improve this question
    
I am not sure what is being asked. Can you clarify the question? –  s_ha_dum Jan 27 '13 at 15:05
    
I added custom field to the Category. e.g. My field name is Hastag and i have a Sport category. And i want to call display Sport's custom field(I want to call current category custom field). How can i do this? –  Genxer Jan 27 '13 at 15:11
    
Thank you, that helps. Please edit the question with that information. –  s_ha_dum Jan 27 '13 at 15:13
add comment

2 Answers 2

You are saving your extra fields to the *_options table so you need to pull them back out of that table. You can copy and cobble together bits of the existing code to do that.

if ( is_category() ) {
    $current_cat = get_query_var('cat');
}
$tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
// var_dump($tag_extra_fields); // debug
if (isset($tag_extra_fields[$current_cat])) {
    var_dump($tag_extra_fields[$current_cat]);
}

Of course, you don't really want var_dump. You will want to echo proper markup. I don't know exactly what markup, but that should get you started.

For example, to output the title as an <h1> tag:

echo '<h1>'.$tag_extra_field['my_title'].'</h1>';

This is assuming that I have interpreted the array structure correctly. If that doesn't work uncomment the var_dump line and post the output in the question-- code formatting in comments is almost non-existent.

share|improve this answer
    
I edited my code with your answer: '<?php if ( is_category() ) { $current_cat = get_query_var('cat'); } $tag_extra_fields = get_option(MY_CATEGORY_FIELDS); // var_dump($tag_extra_fields); // debug if (isset($tag_extra_fields[$current_cat])) { var_dump($tag_extra_fields[$current_cat]); } echo $tag_extra_fields[$tag->term_id]['my_title']; ?>' Code output : 'array(2) { ["my_title"]=> string(15) "#hastagbusiness" ["my_description"]=> string(0) "" } ' –  Genxer Jan 27 '13 at 15:28
    
That looks like it worked. –  s_ha_dum Jan 27 '13 at 15:34
    
I want just this #hastagbusiness but code output: array(1) { [51]=> array(2) { ["my_title"]=> string(15) "#hastagbusiness" ["my_description"]=> string(0) "" } } array(2) { ["my_title"]=> string(15) "#hastagbusiness" ["my_description"]=> string(0) "" } –  Genxer Jan 27 '13 at 15:44
    
Yes, that is what var_dump does and that is why I explained that "... you don't really want var_dump. You will want to echo proper markup." There is an example of that markup in my answer. –  s_ha_dum Jan 27 '13 at 15:47
    
Thank you for answer @s_ha_dum –  Genxer Jan 27 '13 at 15:48
add comment
up vote 0 down vote accepted

I edit your code. And it worked right now. Thank you @s_ha_dum

CODE:

if ( is_category() ) {
    $current_cat = get_query_var('cat');
}
$tag_extra_fields = get_option(MY_CATEGORY_FIELDS);
if (isset($tag_extra_fields[$current_cat])) {
    echo $tag_extra_fields[$current_cat]['my_title']; //i added this line.
}
share|improve this answer
add comment

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.