【问题标题】:How can I add tags to a category in WordPress?如何将标签添加到 WordPress 中的类别?
【发布时间】:2013-01-21 20:10:33
【问题描述】:

我希望能够按不同的字段对我的类别列表进行排序。我认为标签(或任何任意字段)是做到这一点的好方法。但我看不到如何将标签添加到类别。

想法?

【问题讨论】:

    标签: wordpress tags categories


    【解决方案1】:

    来自这个 WordPress StackExchange 问答:

    Add custom taxonomy fields when creating a new taxonomy

    这些不是标签,而是保存在wp_options 表中的自定义字段。并使用类别 ID 保存和检索它们,例如 get_option( "taxonomy_$t_id" )
    查看完整的问答以了解其他解决方案。修改后的代码:

    add_action( 'category_edit_form_fields', 'extra_edit_tax_fields_wpse_29322', 15, 2 );
    add_action( 'category_add_form_fields', 'extra_add_tax_fields_wpse_29322', 10, 2 );
    add_action( 'edited_category', 'save_extra_taxonomy_fields_wpse_29322', 10, 2 );
    add_action( 'create_category', 'save_extra_taxonomy_fields_wpse_29322', 10, 2 );   
    
    // Edit existing taxonomy
    function extra_edit_tax_fields_wpse_29322( $tag ) 
    {
        // Check for existing taxonomy meta for term ID.
        $t_id = $tag->term_id;
        $term_meta = get_option( "taxonomy_$t_id" ); 
        ?>
        <tr class="form-field">
        <th scope="row" valign="top">
            <label for="cat_Image_url"><?php _e( 'Category Navigation Image URL' ); ?></label>
        </th>
            <td>
                <input type="text" name="term_meta[img]" id="term_meta[img]" value="<?php echo esc_attr( $term_meta['img'] ) ? esc_attr( $term_meta['img'] ) : ''; ?>">
                <p class="description"><?php _e( 'Enter the full URL to the navigation image used for this category.' ); ?></p>
            </td>
        </tr>
        <?php
    }
    
    // Add taxonomy page
    function extra_add_tax_fields_wpse_29322( $tag ) 
    {
        ?>
        <div class="form-field">
            <label for="cat_Image_url"><?php _e( 'Category Navigation Image URL' ); ?></label>
            <input type="text" name="term_meta[img]" id="term_meta[img]" value="">
            <p class="description"><?php _e( 'Enter the full URL to the navigation image used for this category.' ); ?></p>
        </div>
        <?php
    }
    
    // Save extra taxonomy fields callback function.
    function save_extra_taxonomy_fields_wpse_29322( $term_id ) 
    {
        if ( isset( $_POST['term_meta'] ) ) 
        {
            $t_id = $term_id;
            $term_meta = get_option( "taxonomy_$t_id" );
            $cat_keys = array_keys( $_POST['term_meta'] );
            foreach ( $cat_keys as $key ) 
            {
                if ( isset ( $_POST['term_meta'][$key] ) )
                    $term_meta[$key] = $_POST['term_meta'][$key];
            }
            // Save the option array.
            update_option( "taxonomy_$t_id", $term_meta );
        }
    }   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多