从帖子取消附加图像

时间:2012-06-11 作者:Darren

我目前在我正在开发的主题中有一个自定义的帖子类型,它将用于公文包项目。到目前为止,我已经实现了允许将图像附加到公文包帖子的功能。

我现在需要从帖子中分离图像的最佳方法:我当前的代码是:

    $images =& get_children(\'post_type=attachment&post_mime_type=image&post_parent=$post->ID\');
            $numberOfImages = count($images);
            if($numberOfImages == 1) {

            }
            else if($numberOfImages > 1)
            {
                foreach($images as $attachment_id => $attachment)
                {
                    $imageAttributes = wp_get_attachment_image_src($attachment_id);
                    ?>
                    <div class="element">
                        <img alt="" height="100" src="<?php echo $imageAttributes[0]; ?>" width="150" />
                        <a href="#" title="Unattach this media item.">Unattach</a>
                    </div>
                    <?php
                }
            }
这将循环浏览附加的图像,并将其显示在带有关联“Unattach”按钮的元框中。我不知道我需要链接到什么/哪里才能将图片从帖子中分离出来。感谢您的帮助。

1 个回复
SO网友:brasofilo

这可以使用Ajax调用来完成,Ajax调用将查询数据库并将父级设置为“零”。

首先,创建一个元框,将调用unattach 函数并注册此WP\\u Ajax函数。

<?php
/*
Plugin Name: Unattach Meta Box
Version: 0.3
Author: brasofilo
Plugin URI: http://wordpress.stackexchange.com/q/54822
*/

add_action( \'add_meta_boxes\', \'wpse_54822_add_custom_box\' );
add_action( \'admin_head\', \'wpse_54822_script_enqueuer\' );
add_action( \'wp_ajax_wpse_54822_custom_query\', \'wpse_54822_unattach\' );
add_action( \'wp_ajax_nopriv_wpse_54822_custom_query\', \'wpse_54822_unattach\' );

function wpse_54822_add_custom_box() 
{
    add_meta_box(
        \'wpse_54822_sectionid\',
        __( \'Page Attachments\' ), 
        \'wpse_54822_inner_custom_box\',
        \'page\'
    );
}

function wpse_54822_inner_custom_box() 
{
    global $post;
    $images = get_children( \'post_type=attachment&post_mime_type=image&post_parent=\' . $post->ID );
    if( count($images) > 0 )
    {
        foreach( $images as $attachment_id => $attachment )
        {
            $imageAttributes = wp_get_attachment_image_src( $attachment_id );
            ?>
            <div class="element" id="container-<?php echo $attachment_id; ?>">
                <img alt="" height="100" src="<?php echo $imageAttributes[0]; ?>" width="150" />
                <a href="javascript:void(0);" id="dettach-<?php echo $attachment_id; ?>" class="dettach" title="Unattach this media item." >Unattach</a>
            </div>
            <?php
        }
    } 
    else
    {
        echo \'No images attached.\';
    }
}

function wpse_54822_script_enqueuer() 
{
    global $current_screen;
    if( \'page\' != $current_screen->id )
        return;
    wp_register_script( \'scripts.js\', plugin_dir_url(__FILE__).\'script.js\' );
    wp_enqueue_script( \'scripts.js\' );
    wp_localize_script( \'scripts.js\', \'wp_ajax\', array( 
        \'url\' => admin_url( \'admin-ajax.php\' ),
        \'nonce\' => wp_create_nonce(\'nonce_wpse_54822\')
    )); 
}

function wpse_54822_unattach()
{
    check_ajax_referer( \'nonce_wpse_54822\' );
    global $post, $wpdb;
    if( !isset( $_POST[\'the_attach_id\'] ) )
        wp_send_json_error( array( \'error\' => \'ID not set\' ) );

    $the_id = intval( $_POST[\'the_attach_id\'] );
    $do_query = $wpdb->query( $wpdb->prepare( 
        "UPDATE $wpdb->posts SET post_parent = 0 WHERE ID = \'%s\'", 
        $the_id 
    ));

    if( !$do_query ) {
        wp_send_json_error( array( \'error\' => \'Query error.\' ) ); // Refine this to show proper wpdb error
    }
    else {
        wp_send_json_success();
    }
}
这是排队的Javascript文件(script.js). 它发出Ajax调用,将执行MySql查询以取消附加,并从元框中删除缩略图。

jQuery(document).ready( function($) 
{       
    $(\'.dettach\').each( function(i,e)
    {
        var id = $(this).attr(\'id\').replace(/dettach-/, \'\');
        $(this).click( function(){
            $.post( 
                wp_ajax.url, 
                { 
                    action: \'wpse_54822_custom_query\', 
                    _ajax_nonce: wp_ajax.nonce, 
                    the_attach_id: id
                }, 
                function( response ){
                    if( !response.success )
                        console.log( response.data.error );
                    else
                        remove_image( id );
                }
            );
        });
     });                 

    function remove_image( id )
    {
        $(\'#container-\'+id).remove();
        return false;
    }
});  
Result:
unattach meta box

结束

相关推荐

使用$_SESSION和PRE_GET_POSTS的自定义发布类型搜索

我整天都在黑客攻击一个自定义的帖子类型搜索/过滤系统。到目前为止,我有:function kdev_property_query($query) { if(isset($_POST[\'rooms_n\'])) $_SESSION[\'rooms_n\'] = $_POST[\'rooms_n\']; if(isset($_POST[\'univer\'])) $_SESSION[\'univer\'] = $_POST[\'univer\']; if(isset($_P