管理显示附加到特定类别的图像,独立于其父类别。现在希望将新上传的图像动态分配给一个类别。在 mySQL 中可以手动执行此操作:
INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES ('1000', '1', '0');
将以下内容添加到 functions.php 时,会产生上传错误:
function image_category() {
$wpdb->insert('wp_term_relationships', array(
'object_id' => '1000',
'term_taxonomy_id' => '26',
'term_order' => '0'
));
}
显然,一旦成功,它将用新图像 ID 的变量替换 1000。
也试过:
$object_id = 1000;
$term_taxonomy_id = 26;
$term_order = 0;
$wpdb->query( $wpdb->prepare(
"
INSERT INTO $wpdb->wp_term_relationships
( object_id, term_taxonomy_id, term_order )
VALUES ( %d, %s, %s )
",
array(
10,
$object_id,
$term_taxonomy_id,
$term_order
)
) );