我为我的英语感到抱歉。我试图在我的Wordpress网站上添加一些功能,为此,我在我的功能中添加了一个功能。php主题。其思想是在自定义表中保存一些数据,并为自定义表中添加的任何元素创建wp\\u post。
我以这种方式钩住了函数:
add_action ( \'init\', \'inserimentoAutoDatabase\' );
问题是,在执行该函数时,将数据保存在自定义表中并创建新post的查询会执行两次。我报告我的代码:
function inserimentoAutoDatabase(){
//get datas from an xml file
$result = fetchData("http://dealer.drivek.it/myPortalXML/index?myPortalXMLkey=d660d1e9-8c1d-41ff-8f54-0829777a9960");
//save the xml
$fp = fopen(\'autodealerk-1.xml\', \'w+\');
fwrite($fp, $result);
fclose($fp);
//load xml file
$xml=simplexml_load_file("autodealerk-1.xml");
$i=0;
//start parsing xml content
foreach($xml->car as $auto)
{
//this echo is executed only once
echo ("test");
//not important for the question | checks some content
if($auto->km == null || $auto->km == \'\'){
$kilometri = "nuova";
$anno_registrazione = "-";
}else{
$kilometri = $auto->km;
$anno_registrazione = $auto->registrationDate;
}
$inevidenza = 0;
if($auto->tractionType){
$inevidenza = 1;
}
// create an array for the query
$insData = array(
\'id\' => $auto[\'id\'],
\'make\' => (string) $auto->make,
\'model\' => (string) $auto->model,
\'version\' => (string) $auto->version,
\'bodyType\' => $auto->bodyType,
\'fuelType\' => $auto->fuelType,
\'type\' => $auto->type,
\'dealer_name\' => (string) $auto->dealer->name,
\'gear_gearType\' => (string) $auto->gear->gearType,
\'tractionType\' => $auto->tractionType,
\'kw\' => $auto->kw,
\'doors\' => $auto->doors,
\'seats\' => $auto->seats,
\'emissionClass\' => $auto->emissionClass,
\'prices_listPrices\' => $auto->prices->listPrice,
\'exterior_color_paint\' => $auto->exterior->color . " " . $auto->exterior->paint,
\'km\' => $kilometri,
\'typewarrantyMonths\' => $auto->warranty->type . " " . $auto->warranty->warrantyMonths,
\'equipments\' => "equipaggiamenti",
\'media\' => $auto->image,
\'description\' => $auto->description,
\'registrationDate\' => $anno_registrazione
);
$wpdb->insert(\'auto_importate\', $insData);
// define the post
$my_post = array(
\'post_title\' => (string) $auto->make . (string) $auto->model . (string) $auto->version,
\'post_content\' => $auto->description,
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_type\' => "vehicles"
);
// ***** post is created twice
$post_id = wp_insert_post( $my_post );
//i break the cycle **** only for test
$i++;
if( $i == 1 ) break;
}
}
正如您在下面cicle末尾的图像中所看到的(在第一次执行后中断),我预计只有一个元素,但有两个。
有人能帮我吗???你知道这个问题吗?
提前谢谢。