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_McGee 和
Davey 为了根据分类法层次结构是否设置为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);