如何在上传前检查图像附件是否存在

时间:2017-03-30 作者:Yogensia

我试图通过编程将图像上传到WP,检查是否已经存在同名图像。我在插件中,在管理面板中通过add_submenu_page().

我使用foreach() 每个帖子上传2张图片,我根据帖子的slug给他们起个名字,但只有一张图片在WP中被正确找到并省略,另一张总是被重新上传到WP,即使它存在。

以下是相关代码:

// sample vars
$slug = \'my-post-slug\';
$card[\'img\'] = \'http://example.com/img1.jpg\';
$card[\'imgGold\'] = \'http://example.com/img2.jpg\';
// end sample vars

$images = \'\';
$images[] = $card[\'img\'];
$images[] = $card[\'imgGold\'];
foreach ( $images as $url ) {

    // check if current url is for regular or gold image
    if ( $card[\'imgGold\'] == $url ) {
        $desc = $slug .\'-gold\';
    } else {
        $desc = $slug;
    }

    // check if attachment already exists
    $attachment_args = array(
        \'posts_per_page\' => 1,
        \'post_type\'      => \'attachment\',
        \'name\'           => $desc
    );
    $attachment_check = new Wp_Query( $attachment_args );

    // if attachment exists, reuse and update data
    if ( $attachment_check->have_posts() ) {

        echo \'Attachment <strong>\'. $desc .\'</strong> found, omitting download...<br>\';

        // do stuff..

    // if attachment doesn\'t exist fetch it from url and save it
    } else {

        echo \'Attachment <strong>\'. $desc .\'</strong> not found, downloading...<br>\';

        // handle image upload from url and assign to post
        $src = media_sideload_image( $url, $post_id, $desc, \'src\' );

        // add post meta
        if ( $card[\'imgGold\'] == $url  ) {
            add_post_meta( $post_id, \'imgGold\', $src );
        } else {
            add_post_meta( $post_id, \'img\', $src );
        }

    } // end attachment exists

} // end foreach image
黄金形象是按预期工作的形象。常规的总是重新上传。不知道为什么,非常感谢您的帮助,谢谢!

1 个回复
最合适的回答,由SO网友:Yogensia 整理而成

好的,我终于找到问题了!我试着给附件和母帖分配相同的slug,显然WordPress不会这样做。The "name" in the media library for the attachment was correct but the actual slug was getting a -2 at the end.

下面是固定代码making the image slugs unique:

// sample vars
$slug = \'my-post-slug\';
$card[\'img\'] = \'http://example.com/img1.jpg\';
$card[\'imgGold\'] = \'http://example.com/img2.jpg\';
// end sample vars

$images = \'\';
$images[] = $card[\'img\'];
$images[] = $card[\'imgGold\'];
foreach ( $images as $url ) {

    // check if current url is for regular or gold image
    if ( $card[\'imgGold\'] == $url ) {
        $desc = $slug .\'-img-gold\';
    } else {
        $desc = $slug .\'-img\';
    }

    // check if attachment already exists
    $attachment_args = array(
        \'posts_per_page\' => 1,
        \'post_type\'      => \'attachment\',
        \'name\'           => $desc
    );
    $attachment_check = new Wp_Query( $attachment_args );

    // if attachment exists, reuse and update data
    if ( $attachment_check->have_posts() ) {

        echo \'Attachment <strong>\'. $desc .\'</strong> found, omitting download...<br>\';

        // do stuff..

    // if attachment doesn\'t exist fetch it from url and save it
    } else {

        echo \'Attachment <strong>\'. $desc .\'</strong> not found, downloading...<br>\';

        // handle image upload from url and assign to post
        $src = media_sideload_image( $url, $post_id, $desc, \'src\' );

        // add post meta
        if ( $card[\'imgGold\'] == $url  ) {
            add_post_meta( $post_id, \'imgGold\', $src );
        } else {
            add_post_meta( $post_id, \'img\', $src );
        }

    } // end attachment exists

} // end foreach image

相关推荐

ADD_THEME_SUPPORT(‘CUSTOM-HEADER-UPLOADS’);功能有什么作用?

同时向functions.php 文件中,我发现了以下内容:add_theme_support(\'custom-header-uploads\');这到底是做什么的?我删除了条目functions.php 但对网站没有什么不同。我最初认为这与上传标题图像的能力有关,但这取决于add_theme_support(\'custom-header\');在执行搜索时,我无法在Codex中看到任何有意义的信息。这是某种贬值的特征还是我忽略了什么?