我已经创建了以下脚本来创建一个来自CJ web api的新帖子,如果帖子已经没有以合理的广告客户名称退出,但脚本会在不检查帖子退出的情况下执行此操作。 即。他们不是......否则效果,所以我该如何解决?我是PHP的新手,所以请忽略我的noobsense,谢谢。
foreach ($data->products[0] as $product)
{
// Sanitize data.
$price = number_format((float)$product->price, 2, '.', ' ');
$image = '<a href="'.$product->{'buy-url'}.'"><img src="'.$product->{'image-url'}.
'" style="float: right"/></a>';
$pd = $image.$product->description .'<a href="'.$product->{'buy-url'}.
'">...For more details and to buy it click here</a>';
$post_slug = $product->{'advertiser-name'};
$args=array(
'cat' => $_GET['cat'],
'name' => $post_slug,
'post_type' => 'post',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if($my_query->have_posts()) {
echo 'post already exits';
wp_reset_query(); // Restore global post data stomped by the_post().
}
else{
$p = array('post_title' => $product->{'advertiser-name'},
'post_content' => $pd,
'post_status' => 'publish',
'post_author' => 1,
'post_category' =>array($_GET['cat']));
$pr = wp_insert_post( $p, $wp_error );
echo $pr;
wp_reset_query(); // Restore global post data stomped by the_post().
}
答案 0 :(得分:0)
任何值都来自$my_query->have_posts()
?
先检查一下。
尝试print_r($my_query->have_posts());
然后尝试在if(){ ...}else { ....}