向自定义分类管理元数据添加描述

时间:2016-06-17 作者:Yazmin

我创建了自定义分类法:

add_action( \'init\', \'create_collection_taxonomies\', 0 );
function create_collection_taxonomies() {
    $labels = array(
        \'name\'                       => _x( \'Collection Tags\', \'taxonomy general name\' ),
        \'singular_name\'              => _x( \'Collection Tag\', \'taxonomy singular name\' ),
        \'search_items\'               => __( \'Search Collection Tags\' ),
        \'popular_items\'              => __( \'Popular Collection Tags\' ),
        \'all_items\'                  => __( \'All Collection Tags\' ),
        \'parent_item\'                => __( \'Parent Collection Tag\' ),
        \'parent_item_colon\'          => __( \'Parent Collection Tag\' ),
        \'edit_item\'                  => __( \'Edit Collection Tag\' ),
        \'update_item\'                => __( \'Update Collection Tag\' ),
        \'add_new_item\'               => __( \'Add New Collection Tag\' ),
        \'new_item_name\'              => __( \'New Collection Tag Name\' ),
        \'menu_name\'                  => __( \'Collections Tags\' ),
    );

    $args = array(
        \'description\'           => \'Used to select promo/video for display in app.\',
        \'hierarchical\'          => true,
        \'labels\'                => $labels,
        \'show_ui\'               => true,
        \'show_admin_column\'     => true,
        \'query_var\'             => true,
        \'rewrite\'               => array( \'slug\' => \'CollectionTag\' ),
    );

    register_taxonomy( \'CollectionTag\', array(\'collection\', \'videos\', \'promos\'), $args );
}
我添加了description 认为它将出现在WP admin中的分类元框中的参数;然而,情况似乎并非如此。

我确实找到了this post from 2011, 我说我必须使用jQuery才能将描述添加到框中,但我们可以定义一个描述却无法使用它,这似乎很奇怪。

我是否误解了我在Codex? 如何使此描述显示在该分类法的元框中?

2 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

查看创建元盒的文件,没有任何东西可以真正挂钩,从而允许我们添加描述,因此我们需要使用jQuery。由于脚本非常小,我选择不将其放入外部文件,而是使用admin_footer 挂钩:

/**
 * Prepend taxonomy descriptions to taxonomy metaboxes
 */
function append_taxonomy_descriptions_metabox() {
    $post_types = array( \'page\' );          // Array of Accepted Post Types
    $screen     = get_current_screen();     // Get current user screen

    if( \'edit\' !== $screen->parent_base ) { // IF we\'re not on an edit page - just return
        return;
    }

    // IF the current post type is in our array
    if( in_array( $screen->post_type, $post_types ) ) {
        $taxonomies = get_object_taxonomies( $screen->post_type, \'objects\' );   // Grab all taxonomies for that post type

        // Ensure taxonomies are not empty
        if( ! empty( $taxonomies ) ) : ?>

            <script type="text/javascript">

              <?php foreach( $taxonomies as $taxonomy ) : ?>

                var tax_slug = \'<?php echo $taxonomy->name; ?>\';
                var tax_desc = \'<?php echo $taxonomy->description; ?>\';

                // Add the description via jQuery
                jQuery( \'#\' + tax_slug + \'div div.inside\' ).prepend( \'<p>\' + tax_desc + \'</p>\' );

              <?php endforeach; ?>

            </script>

        <?php endif;
    }
}
add_action( \'admin_footer\', \'append_taxonomy_descriptions_metabox\' );
我们有一个$post_types 要测试的数组,因此我们不会将其添加到每个帖子类型或每个页面,而只添加到特定页面。我们可以对分类法执行同样的操作,但在这个场景中我没有这样做。

<小时>

BONUS MATERIAL

下面将向Admin taxonomy页面(您可以在其中添加新术语)添加描述。我们用了一个更简单的钩子add_action( \'{$taxonomy}_pre_add_form\' ):

/**
 * Prepend taxonomy description to Add New Term form
 */
function CollectionTag_admin_edit_description( $tax_slug ) {

    // Grab the Taxonomy Object
    $tax_obj = get_taxonomy( $tax_slug );

    // IF the description is set on our object
    if( property_exists( $tax_obj, \'description\' ) ) {
        echo \'<h2>Description</h2>\';
        echo apply_filters( \'the_content\', $tax_obj->description );
    }
}
add_action( \'CollectionTag_pre_add_form\', \'CollectionTag_admin_edit_description\' );

SO网友:Christopher Sample

Dynamic versions of the accepted answers:

要使用Howdy{$taxonomy}\\u pre\\u add\\u formaction的动态版本向每个公共分类法添加描述,而不为每个分类法编写描述,请使用以下方法:

/**
 * Prepend taxonomy description to Add New Term form for each taxonomy
 */
add_action(\'init\', \'MCMLXV_add_taxonomy_decriptions\');
function MCMLXV_add_taxonomy_decriptions()
{
    $args = array(
        \'public\'   => true,
        \'_builtin\' => false
    );
    $taxonomies = get_taxonomies($args, \'names\', \'and\');
    foreach ($taxonomies as $key => $taxonomy) {
    // Create a dynamic anonymous function foreach of our taxonomies 
    // and pass the taxonomy name variable to the anonymous function
        add_action($taxonomy . \'_pre_add_form\', function ($taxonomy) {
            // Grab the Taxonomy Object
            $tax_obj = get_taxonomy($taxonomy);

            // IF the description is set on our object
            if (property_exists($tax_obj, \'description\')) {
                echo \'<h2>Description</h2>\';
                echo apply_filters(\'the_content\', $tax_obj->description);
            }
        });
    }
}
将以上答案组合为Howdy_McGeeDavey 为了根据分类法层次结构是否设置为false(即标记或自定义标记分类法)进行动态,并使js变量唯一,我使用了以下方法:

<script type="text/javascript" id="mythemehere_taxonomy_metabox_description">
    <?php foreach ($taxonomies as $taxonomy) { ?>
        <?php if ($taxonomy->hierarchical == false) { ?>

            // Unique variables per tax
            var tax_slug_<?php echo $taxonomy->name; ?> = \'<?php echo $taxonomy->name; ?>\';
            var tax_desc_<?php echo $taxonomy->name; ?> = \'<?php echo $taxonomy->description; ?>\';

            // jQuery targeting of hierarchal false taxonomies (tags)
            jQuery(\'#tagsdiv-\' + tax_slug_<?php echo $taxonomy->name; ?> + \' div.inside\').prepend(\'<p>\' + tax_desc_<?php echo $taxonomy->description; ?> + \'</p>\');

        <?php } else { ?>

            // Unique variables per tax
            var tax_slug_<?php echo $taxonomy->name; ?> = \'<?php echo $taxonomy->name; ?>\';
            var tax_desc_<?php echo $taxonomy->name; ?> = \'<?php echo $taxonomy->description; ?>\';

            // jQuery targeting of hierarchal true taxonomies (categories)
            jQuery(\'#\' + tax_slug_<?php echo $taxonomy->name; ?> + \'div div.inside\').prepend(\'<p>\' + tax_desc_<?php echo $taxonomy->name; ?> + \'</p>\');

        <?php } ?>
     <?php } ?>
</script>
这将输出如下内容:


<script type="text/javascript" id="mythemehere_taxonomy_metabox_description">
    var tax_slug_portfolio_category = \'portfolio_category\';
    var tax_desc_portfolio_category = \'this is your unique description\';
    // Add the description via jQuery
    jQuery(\'#\' + tax_slug_portfolio_category + \'div div.inside\').prepend(\'<p>\' + tax_desc_portfolio_category + \'</p>\');
                                                                                 
    var tax_slug_portfolio_tag = \'portfolio_tag\';
    var tax_desc_portfolio_tag = \'this is your unique description\';
    // Add the description via jQuery
    jQuery(\'#tagsdiv-\' + tax_slug_portfolio_tag + \' div.inside\').prepend(\'<p>\' + tax_desc_portfolio_tag + \'</p>\');
以上和我的代码中的所有答案都假设您已经在register\\u分类法中设置了描述和层次结构($args)。省略本例中所有不必要的参数:

    $args = array(
        \'hierarchical\' => true,
        \'description\' => \'this is your unique description\',
    );
    register_taxonomy(\'portfolio_category\', array(\'portfolio\'), $args);

相关推荐

如何在WordPress开发中添加带有ACF自定义字段ID的自定义metabox字段

我是wordpress开发的新手,我在我的项目中安装了高级自定义字段插件,并创建了两个文本字段名称&;我还创建了一个插件,可以在帖子中创建一个带有文本框的元框。现在在帖子中,我将获得自定义字段名称(&A);电子邮件和我的自定义元框旁边将出现,但我必须将我的元框附加到名称字段旁边,即在名称字段和电子邮件字段之间。我的metabox代码如下。请任何人帮帮我//Creating the custom meta box function my_notice_meta_box() {