从类别中的自定义字段中获取值

时间:2014-09-11 22:17:36

标签: php wordpress

我在管理类别界面中创建了一个名为自定义顺序的自定义字段。它显示在管理员中。但是现在我在获取来自该字段的信息时遇到了困难,并在index.php中回应了它。

如果我跑

$thisCat = get_category( 29);
    print_r($thisCat);

我没有从自定义字段中获取任何信息。

echo get_post_custom_values('category_custom_order', 29);没有回应任何事情。

我应该如何从category_custom_order获得价值?

以下是functions.php中自定义字段的代码:

<?php
/** Add Custom Field To Category Form */
add_action( 'category_add_form_fields', 'category_form_custom_field_add', 10 );
add_action( 'category_edit_form_fields', 'category_form_custom_field_edit', 10, 2 );

function category_form_custom_field_add( $taxonomy ) {
?>
<div class="form-field">
  <label for="category_custom_order">Custom Order</label>
  <input name="category_custom_order" id="category_custom_order" type="text" value="" size="40" aria-required="true" />
  <p class="description">Enter a custom order value.</p>
</div>
<?php
}

function category_form_custom_field_edit( $tag, $taxonomy ) {

    $option_name = 'category_custom_order_' . $tag->term_id;
    $category_custom_order = get_option( $option_name );

?>
<tr class="form-field">
  <th scope="row" valign="top"><label for="category_custom_order">Custom Order</label></th>
  <td>
    <input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : ''; ?>" size="40" aria-required="true" />
    <p class="description">Enter a custom order value.</p>
  </td>
</tr>
<?php
}

/** Save Custom Field Of Category Form */
add_action( 'created_category', 'category_form_custom_field_save', 10, 2 ); 
add_action( 'edited_category', 'category_form_custom_field_save', 10, 2 );

function category_form_custom_field_save( $term_id, $tt_id ) {

    if ( isset( $_POST['category_custom_order'] ) ) {           
        $option_name = 'category_custom_order_' . $term_id;
        update_option( $option_name, $_POST['category_custom_order'] );
    }
}

1 个答案:

答案 0 :(得分:1)

你应该能够以这种方式得到它,顺便说一下,它不是一个自定义字段,而是一个选项。

$category_id = 29;
$category_custom_order = get_option( 'category_custom_order_' . $category_id );

echo $category_custom_order;