Php Wordpress:在Wordpress posts表中添加一个字段
我正在开发一个web应用程序,需要向wordpress posts数据库添加额外的信息,但wordpress posts表没有额外的列 所以我插入了我自己的领域favicon、image、domain用于存储文章的URL,因为我从JSON提要下载了文章 但是,当我尝试将帖子插入数据库时,会插入帖子,但在填充默认字段时,我的额外列保留为空 下面是我如何插入数据库的Php Wordpress:在Wordpress posts表中添加一个字段,php,json,wordpress,Php,Json,Wordpress,我正在开发一个web应用程序,需要向wordpress posts数据库添加额外的信息,但wordpress posts表没有额外的列 所以我插入了我自己的领域favicon、image、domain用于存储文章的URL,因为我从JSON提要下载了文章 但是,当我尝试将帖子插入数据库时,会插入帖子,但在填充默认字段时,我的额外列保留为空 下面是我如何插入数据库的 <?php require(dirname(__FILE__)."/../../../wp-config.php"); $j
<?php
require(dirname(__FILE__)."/../../../wp-config.php");
$json_feed = "http://digitalrand.net/api/url_data/?key=xxxxxx&pass=xxxxxxx%";
$json = file_get_contents($json_feed);
$obj = json_decode($json, true);
foreach($obj as $article_array){
$url = $article_array['url'];
$domain = $article_array['domain'];
$favicon = $article_array['favicon'];
$title = $article_array['title'];
$category = $article_array['category'];
$large_summary = $article_array['summary'];
$sum = implode(',',$large_summary);
$images = $article_array['images'];
$i = reset($images);
$post = array(
'post_title' => $title,
'post_content' => $sum,
'post_status' => 'publish',
'post_type' => 'post',
'comment_status' => 'closed',
'post_category' => $category,
'post_template' => 'content.php'
);
wp_insert_post ($post, $wp_error);
$post_id = $wpdb -> insert_id;
$extra = array('post_image', 'post_favicon', 'post_domain');
$data = array($image, $favicon, $domain);
add_post_meta($post_id, $extra, $data, true);
?>
我做错了什么,因为帖子正在插入,但帖子元没有插入到数据库中
我在主主题的content.php文件中添加了以下内容,所有帖子都显示在该文件中
<?php
$post_Id = get_the_ID();
$favicon_values = get_post_meta($post_id, 'post_favicon', true);
echo $favicon_values;
?>
这就是我为每篇文章显示favicon_值的方式吗?您应该在wp_postETA表中插入favicon、domain和others字段,这比更改wp_posts表的结构更好
$post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id withing add_post_meta or update_post_meta
<?php add_post_meta( $post_id, 'post_favicon', $favicon, true ) || update_post_meta( $post_id, 'post_favicon', $favicon ); ?>
$post\u id=wp\u insert\u post($post,$wp\u error);
//现在,您可以使用$post\u id和add\u post\u meta或update\u post\u meta
您的代码应该是这样的。我从$post数组中删除了图像、favicon和域…请检查编辑。已将post_元字段数据添加到数组中:额外,并将要插入到数组中的数据:$data。我已经检查了wordpress codex和add_post_meta(如果按您定义的方式使用),如果该键不存在,则添加新的自定义字段,否则使用该键更新自定义字段的值。所以我想我不必从phpmyadmin在posts_meta表中创建字段,对吗?我如何将新的post_元数据显示到内容页?当显示wp_posts中的内容时,您将获得post_id,通过post id和meta_键,您可以轻松地找到数据。示例:检查我对如何显示post_meta内容的编辑
$post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id withing add_post_meta or update_post_meta
<?php add_post_meta( $post_id, 'post_favicon', $favicon, true ) || update_post_meta( $post_id, 'post_favicon', $favicon ); ?>