我正在为一家杂志出版商写一个主题,他经常处理大量的图片。我需要他们能够将每个图像与贡献者(摄影师、插画家、艺术家等)关联起来
每个贡献者都包含在一个自定义帖子类型“jdp\\U贡献者”中,贡献者类型是一个自定义分类法“jdp\\U贡献者\\U类型”。
使用WordPress挂钩“attachment\\u fields\\u to\\u edit”,我想添加一个选择菜单,允许用户为图像选择贡献者。我让网站弹出菜单并正确保存值,但仍有一些奇怪的错误。
1) 通过媒体上传器添加新图像时,可以从(50多个)贡献者的完整列表中进行选择。
2) 通过“媒体上传器”(在“媒体库”选项卡中)编辑图像时,只有前10个贡献者可供选择。
要创建选择菜单,我正在创建一个WP\\u QUERY的新实例。WP\\u QUERY的参数状态为“posts\\u per\\u page”=-1和“nopage”设置为true。
我不知道我做得不对。
<?php
/**
* Add Photographer selection to media upload
*
* @param $form_fields array, fields to include in attachment form
* @param $post object, attachment record in database
* @return $form_fields, modified form fields
*/
function bsj_attachment_field_credit( $form_fields, $post ) {
//do_dump($post);
$form_fields[\'jdp_photographer\'] = array(
\'label\' => \'Contributor:\',
\'input\' => \'html\',
\'helps\' => \'Choose the photographer/artist for this image\',
);
//GET THE CURRENT PHOTOGRAPHER
$current_photog = get_post_meta($post->ID, \'jdp_photographer\', true);
if ( !isset($current_photog) ) {
$current_photog = 0;
}
//SETUP THE FORM FIELD
$form_fields[\'jdp_photographer\'][\'html\'] = \'<select name="attachments[\'.$post->ID.\'][jdp_photographer]" id="attachments[\'.$post->ID.\'][jdp_photographer]">\';
//OUTPUT NULL FIELD
$form_fields[\'jdp_photographer\'][\'html\'] .= \'<option value="0" \'.selected(0, $current_photog, false).\'>Choose one...</option>\';
//GET THE TERMS IN THE \'jdp_contributor_types\' taxonomy, excluding authors
$terms = get_terms( \'jdp_contributor_types\', array(\'fields\' => \'ids\', \'hide_empty\' => false, \'exclude\' => \'32\') );
//GET THE PHOTOGRAPHERS
$args = array(
\'post_type\' => \'jdp_contributors\',
\'nopaging\' => true,
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'jdp_contributor_types\',
\'field\' => \'id\',
\'terms\' => $terms
),
),
\'orderby\' => \'name\',
\'order\' => \'ASC\',
);
$photographers = new WP_QUERY($args);
//LOOP THRU THE PHOTOGRAPHERS
foreach( $photographers as $postdata ) {
setup_postdata($postdata);
$form_fields[\'jdp_photographer\'][\'html\'] .= \'<option value="\'.$postdata->ID.\'" \'.selected($postdata->ID, $current_photog, false).\'>\'.$postdata->post_title.\'</option>\';
}
wp_reset_query();
$form_fields[\'jdp_photographer\'][\'html\'] .= \'</select>\';
return $form_fields;
}
add_filter( \'attachment_fields_to_edit\', \'bsj_attachment_field_credit\', 10, 2 );
/**
* Save values of Photographer Name and URL in media uploader
*
* @param $post array, the post data for database
* @param $attachment array, attachment fields from $_POST form
* @return $post array, modified post data
*/
function jdp_attachment_field_credit_save( $post, $attachment ) {
if (isset( $attachment[\'jdp_photographer\'] ) )
update_post_meta( $post[\'ID\'], \'jdp_photographer\', $attachment[\'jdp_photographer\'] );
return $post;
}
add_filter( \'attachment_fields_to_save\', \'jdp_attachment_field_credit_save\', 10, 2 );
?>