我有一个表单,用户可以上传一个文件,创建一个带有附件的帖子。我想用post-id获取此附件,因为这是我唯一拥有的东西。有可能吗?
非常感谢。
ArbreMojo。
我有一个表单,用户可以上传一个文件,创建一个带有附件的帖子。我想用post-id获取此附件,因为这是我唯一拥有的东西。有可能吗?
非常感谢。
ArbreMojo。
是的使用此代码可以获取附件id
if(isset($_FILES[\'myimage\']) && ($_FILES[\'myimage\'][\'size\'] > 0)) {
// Get the type of the uploaded file. This is returned as "type/extension"
$arr_file_type = wp_check_filetype(basename($_FILES[\'myimage\'][\'name\']));
$uploaded_file_type = $arr_file_type[\'type\'];
// Set an array containing a list of acceptable formats
$allowed_file_types = array(\'image/jpg\', \'image/jpeg\', \'image/gif\', \'image/png\');
if (in_array($uploaded_file_type, $allowed_file_types)) {
$upload_overrides = array(\'test_form\' => false);
$uploaded_file = wp_handle_upload($_FILES[\'myimage\'], $upload_overrides);
if (isset($uploaded_file[\'file\'])) {
$file_name_and_location = $uploaded_file[\'file\'];
$file_title_for_media_library = $imagetitle;
$attachment = array(
\'post_mime_type\' => $uploaded_file_type,
\'post_title\' => \'Uploaded image \' . addslashes($file_title_for_media_library),
\'post_content\' => \'\',
\'post_status\' => \'inherit\'
);
// Run the wp_insert_attachment function. This adds the file to the media library and generates the thumbnails. If you wanted to attch this image to a post, you could pass the post id as a third param
$attach_id = wp_insert_attachment($attachment, $file_name_and_location);//here you get attachment id
require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
$attach_data = wp_generate_attachment_metadata($attach_id, $file_name_and_location);
wp_update_attachment_metadata($attach_id, $attach_data);
update_post_meta($post_id, \'pluginimage\', $attach_id);//update post data by attachment id
$upload_feedback = false;
}
}}
我们正在尝试优化一个流量大的WP站点,并正在寻找一种只需一次查询即可获取这三个WP功能数据的方法,以减少查询和;DB服务器上的负载:$wpid=get_the_ID(); $title=get_the_title($different_post_id); $perma=get_the_permalink($different_post_id); 这可能吗?EDIT——调整了上面的示例代码,以正确指示在本例中,我们正在获取不同post\\u id的标题和永久链接,而不是get\\u