在wordpress中为每个类别添加图标

时间:2015-02-02 18:50:31

标签: php wordpress

我尝试使用以下代码在类别名称左侧添加图标;

/** 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">Simge Kodu</label>
  <input name="category_custom_order" id="category_custom_order" type="text" value="" size="40" aria-required="true" />
  <p class="description">Dökümanda bulunan simgelerden birini seçip kodunu buraya girin.</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">Simge Kodu</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">Dökümanda bulunan simgelerden birini seçip kodunu buraya girin.</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'] );
    }
}
?>

之后,我在主题中添加了以下代码。我想使用get_option函数来获取属于每个类别的图标。但是,我无法获得类别ID,因此无法正常工作。我怎样才能获得类别ID?我的意思是,在代码中,get_option('category_custom_order_{menu ID}' ), {menu ID}应返回每个类别ID。你能帮忙吗,谢谢。

<?php
        $defaults = array(
            'theme_location'  => 'menu',
            'menu'            => 'Üst Menü',
            'container'       => '',
            'container_class' => '',
            'container_id'    => '',
            'menu_class'      => 'menu',
            'menu_id'         => '',
            'echo'            => true,
            'fallback_cb'     => 'wp_page_menu',
            'before'          => '',
            'after'           => '',
            'link_before'     => '<i class="fa '.get_option('category_custom_order_{menu ID}' ).' fa-fw"></i>&nbsp; ',
            'link_after'      => '',
            'items_wrap'      => '%3$s',
            'depth'           => 0,
            'walker'          => ''
        ); 
        wp_nav_menu( $defaults );
        ?>

0 个答案:

没有答案