0

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']; ?>
3
  • I am not sure what is being asked. Can you clarify the question?
    – s_ha_dum
    Jan 27, 2013 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, 2013 at 15:11
  • Thank you, that helps. Please edit the question with that information.
    – s_ha_dum
    Jan 27, 2013 at 15:13

2 Answers 2

0

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.

4
  • 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, 2013 at 15:28
  • That looks like it worked.
    – s_ha_dum
    Jan 27, 2013 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, 2013 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, 2013 at 15:47
0

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.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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