0

我创建了以下脚本以从 CJ Web api 创建一个新帖子,如果帖子尚未以健全的广告商名称退出,但脚本在不检查帖子退出与否的情况下执行此操作。IE。他们没有 if...else 效果,那么我该如何解决呢?我是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().

}
4

1 回答 1

0

有什么价值$my_query->have_posts()吗?

首先检查一下。

尝试print_r($my_query->have_posts());

然后尝试做if(){ ...}else { ....}

于 2013-04-02T09:47:39.697 回答