我写了一个插件,它读取csv文件并创建新产品。该插件仅在我创建一个产品时有效,但当我在 while
中添加Insert()
时插件无法正常工作。我想先创建所有产品。也许这与add_action有关...请帮忙。
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
function CreateProduct($line) {
$data = explode('";"', $line);
define(POST_NAME, $data[2]);
define(cena_netto_pw, $data[5]);
$post = get_page_by_title( POST_NAME, 'OBJECT', 'product' );
$product_ID = $post->ID;
$post_data = get_post($product_ID);
function hbt_create_post() {
$my_post = array(
'post_title' => POST_NAME,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' =>'product'
);
$product_ID = wp_insert_post( $my_post );
}
if(!isset($post))
hbt_create_post();
return $error_obj;
}
function Import() {
$file = PLUGIN_DIR.'test.csv';
$open = fopen($file, 'r');
while (!feof($open)) {
$line = fgets($open);
CreateProduct($line);
}
fclose($open);
}
add_action('admin_init', 'Import' );
?>
while循环代码
while (!feof($open)) { $line = fgets($open); CreateProduct($line); }
此代码不起作用。仅在
时才有效$line = fgets($open); CreateProduct($line);
答案 0 :(得分:0)
尝试
fgetcsv($open)
代替
fgets($open)