【问题标题】:How in insert image path in Wordpress database that will display product image in wp admin not on front end?如何在 Wordpress 数据库中插入图像路径以在 wp admin 中显示产品图像而不是在前端?
【发布时间】:2014-07-05 19:22:15
【问题描述】:
$insert = $wpdb->insert( $prefix."posts", array("post_title" => $posTitle,"post_content" => $postContent,"post_status" => "publish","post_type" =>"product"));

// select products ID
$select = $wpdb->get_results("SELECT ID AS productsId FROM ".$prefix."posts WHERE post_title='".$posTitle."'");
//echo $select[0]->productsId; die();
// insert Image in pots and product attribute meta posts table
$insertImg = $wpdb->insert($prefix."posts",array("post_status" =>"inherit","post_type" => "attachment","guid" => $postGuid,"post_parent" => $select[0]->productsId ));

我需要一个小的,我需要插入哪个表和列来在 wordpress admin 中显示产品图像,我在 wordpress 中通过查询插入产品而不是通过管理员插入产品。

【问题讨论】:

  • 任何人都知道它的简单解决方案
  • 管理区在哪里?在帖子列表中?
  • 产品 -> 编辑产品 -> 右侧产品图片(特征图片)
  • 我有一个实时图像路径cardkingdom.com//product_images/1/9/4/6/6/8/40161_large.jpg 我想存储此路径并在管理员中显示为产品图像
  • 是的!我的重点是通过查询插入产品而不是手动编写代码,但图像在管理员中对我不起作用,所以我需要在数据库中使用存储图像并在管理员中显示

标签: wordpress


【解决方案1】:

wordpress 特色图片实际上是一个名为_thumbnail_id 的元字段,因此它位于wp_postmeta 表中

示例:

meta_id post_id meta_key meta_value
874 3767 _thumbnail_id 3841

在上面的例子中,meta_value实际上是附件的ID..

您在查询中所做的,实际上只是插入了一个附件

$insertImg = $wpdb->insert($prefix."posts",array("post_status" =>"inherit","post_type" => "attachment","guid" => $postGuid,"post_parent" => $select[0]->productsId ));

现在您需要通过添加相关的post_meta 使该附件成为特色图片

现在,老实说,我不太擅长直接 SQL 查询(只是基础知识),但如果您在 WP 上运行整个过程 - 好吧,它有不同的方法来实现。

1 - set_post_thumbnail( $newpostid, $attach_id ); 其中的参数非常不言自明,

2- update_post_meta($post_id, '_thumbnail_id', $attach_id); - 也是不言自明的..

实际上两者都会做同样的事情..

现在,就像我之前说的那样 - 我已经完成了很多次你描述的这样的过程,但不是直接查询..

对我来说通常的流程是只使用 wp 内置函数,例如:

$newpost = array( 'post_title' => $fullname, 
                        'post_content' =>'',
                        'post_status' => 'pending',
                        'post_category' => array(5),
);

    // Create the post
    $newpostid = wp_insert_post($newpost);

// $filename should be the path to the file ( upload directory for example ) 
            $filename = $dest_path;

            // Check the type of tile. We'll use this as the 'post_mime_type'.
            $filetype = wp_check_filetype( basename( $filename ), null );

            // Get the path to the upload directory.
            $wp_upload_dir = wp_upload_dir();

            // Prepare an array of post data for the attachment.
            $attachment = array(
                    'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ), 
                    'post_mime_type' => $filetype['type'],
                    'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
                    'post_content'   => '',
                    'post_status'    => 'inherit'
            );

            // Insert the attachment.
            $attach_id = wp_insert_attachment( $attachment, $filename, $newpostid );

            // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
            require_once( ABSPATH . 'wp-admin/includes/image.php' );

            // Generate the metadata for the attachment, and update the database record.
            $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );

            // And now make it a featured image..
            set_post_thumbnail( $newpostid, $attach_id );

我希望这会让你走上正轨..

【讨论】:

  • 我已经做了什么我已经使用查询 1 插入数据。我插入 post_title='abcd',post_content='abcd' post_status='publish',post_type='product' 的帖子 2.插入post_parent='我在步骤 1 中创建的上述帖子的 ID',guid='cardkingdom.com//product_images/1/9/4/6/6/8/…' 3. 在第二步中插入 postmeta meta_key='_thumbnail_id' 和 meta_value= ID craete。
  • 这对我很有用你知道如何从外部 url 上传图片并将其保存到上传文件夹我做了一些成功的事情,但图片格式不正确并包含错误
  • 好吧,首先,如果解决了,您可能希望接受标记为已解决的答案(您在答案左侧看到的小V)或投票赞成或两者兼而有之.. 如果您还有其他问题,请照原样发布(新问题)
猜你喜欢
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多