发布后获取精选图片网址

时间:2016-02-27 05:57:57

标签: php wordpress

我试图在帖子发布后获取精选图片网址但不知何故它返回一个空网址。

但如果我要获得post_title,那就没关系。

function update_google_map($post_id) {

    // If this is just a revision, don't send the email.


    global $wpdb;

    if (wp_is_post_revision($post_id))
        return;

    $post_title = get_the_title($post_id);

    $post_content1 = get_post_field('post_content', $post_id);
    $post_content = substr($post_content1, 0, 150);

    $post_url = get_permalink($post_id);

    $feat_image_url = wp_get_attachment_url(get_post_thumbnail_id($post_id));

    $google_location = get_post_meta($post_id, 'g_location', $single = true);

    $google_loc = explode(",", $google_location);

    $chk_result = $wpdb->get_results("SELECT post_id FROM wp_wpgmza WHERE post_id = '" . $post_id . "'");

    if (count($chk_result) > 0) {
        $wpdb->update(
                'wp_wpgmza', array(
            'description' => $post_content,
            'pic' => $feat_image_url,
            'link' => $post_url,
            'lat' => $google_loc[0],
            'lng' => $google_loc[1],
            'title' => $post_title,
            'map_id' => 1
                ), array('post_id' => $post_id), array(
            '%s',
            '%s',
            '%s',
            '%s',
            '%s',
            '%s',
            '%d'
                )
        );
    } else {
        $wpdb->query("INSERT INTO wp_wpgmza (post_id, map_id, description, pic, link, lat,lng, anim, title, infoopen) "
            . "VALUES ('" . $post_id . "', 1, '" . $post_content . "', '" . $feat_image_url . "', '" . $post_url . "', '" . $google_loc[0] . "', '" . $google_loc[1] . "', 0, '" . $post_title . "', 0)");
    }

}

add_action('publish_post', 'update_google_map', 50);

如果我要使用save_post hook

更新同一篇文章
add_action('save_post', 'update_map', 50);

在Wordpress编辑器发布后,我能够再次获得精选图片网址。同样适用于$ google_location。当它尝试从postmeta表中检索时它也是空的。

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

请替换您使用过的代码,并检查您的功能图像为publish_post

    function update_map($post_id) {

    global $wpdb;

    $thumbnail_id = get_post_thumbnail_id($post_id);
    $url = wp_get_attachment_url($thumbnail_id);

    if ($url != '') {
        wp_mail('abc@gmail.com', 'Yes', 'Yes');
    }

    wp_mail('abc@gmail.com', 'No', 'No');
}

add_action('publish_post', 'update_map', 50);

因为$url不是数组,所以它是字符串,因此您无法使用!empty

相关问题