3

Hello stackoverflow friends!

Hopefully somebody here can help me with this! I've looked all over the internet with no luck :(

What I want to do is somehow show the images attached to a post inside the WP admin. For example, the same way the post Featured Image shows. The only thing I found was how to display the number of attachments of a post in the admin columns but what I would like to see is thumbnails of all attached images inside each post in the post admin page. I think this will be very helpful since right now I cannot even tell which post has attachements.

I will like to accomplish this without using a plugin.

I have looked for days online with no luck. Any help will be greatly appreciated.

Thank you very much in advanced!

4

2 回答 2

5

解决方案是创建一个元框,然后将缩略图放在那里。这需要add_meta_box()函数和wp_get_thumb_attachment_url()。我们还需要找到某个帖子的所有附加图片,我们将在这里使用答案。

将所有这些放在一起,并假设 PHP 版本 >= 5.3 以便我们可以使用匿名函数,它看起来像这样:

add_action( 'add_meta_boxes', function() {
add_meta_box( 'att_thumb_display', 'Attachmed images', function( $post ) {
    $args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'post_parent' => $post->ID
    );

    echo '<ul>';
    foreach( get_posts( $args ) as $image) {
        echo '<li><img src="' . wp_get_attachment_thumb_url( $image->ID ) . '" /></li>';
    }
    echo '</ul>';
}, 'post' );
});

add_meta_box我设置为“post”的参数表示此元框将在哪种帖子类型上可用。如果您希望它在页面上可用,则必须将其设置为页面。或者,如果您希望它可用于自定义帖子类型,您还必须相应地对其进行修改。

我希望这行得通。我没试过。

于 2013-01-04T17:46:28.710 回答
2

尝试这个 :

   /* === Add Thumbnails to Posts/Pages List === */
if ( !function_exists('o99_add_thumbs_column_2_list') && function_exists('add_theme_support') ) {

    //  // set your post types , here it is post and page...
    add_theme_support('post-thumbnails', array( 'post', 'page' ) );

    function o99_add_thumbs_column_2_list($cols) {

        $cols['thumbnail'] = __('Thumbnail');

        return $cols;
    }

    function o99_add_thumbs_2_column($column_name, $post_id) {

            $w = (int) 60;
            $h = (int) 60;

            if ( 'thumbnail' == $column_name ) {
                // back comp x WP 2.9
                $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
                // from gal
                $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
                if ($thumbnail_id)
                    $thumb = wp_get_attachment_image( $thumbnail_id, array($w, $h), true );
                elseif ($attachments) {
                    foreach ( $attachments as $attachment_id => $attachment ) {
                        $thumb = wp_get_attachment_image( $attachment_id, array($w, $h), true );
                    }
                }
                    if ( isset($thumb) && $thumb ) {
                        echo $thumb;
                    } else {
                        echo __('None');
                    }
            }
    }

    // for posts
    add_filter( 'manage_posts_columns', 'o99_add_thumbs_column_2_list' );
    add_action( 'manage_posts_custom_column', 'o99_add_thumbs_2_column', 10, 2 );

    // for pages
    add_filter( 'manage_pages_columns', 'o99_add_thumbs_column_2_list' );
    add_action( 'manage_pages_custom_column', 'o99_add_thumbs_2_column', 10, 2 );
}

它会在管理帖子/页面列表中向您显示特色图像作为预览,如果您想在帖子本身中显示它 - 使用@Calle 建议:(在此处修改以工作)

add_action( 'add_meta_boxes', 'o99_add_attach_thumbs_meta_b' );

function o99_add_attach_thumbs_meta_b (){

add_meta_box ('att_thumb_display', 'Attached images','o99_render_attach_meta_b','post');

}

function o99_render_attach_meta_b( $post ) {
$output = '';
$args = array(
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'post_parent' => $post->ID
    );
    //
    // uncomment if you want ordered list
    //
    // $output .= '<ul>';
     $images = get_posts( $args );
    foreach(  $images as $image) {
    //$output .= '<li>';
        $output .= '<img src="' . wp_get_attachment_thumb_url( $image->ID ) . '" />';
        //$output .= '</li>';
    }
   // $output .= '</ul>';
  echo $output;
}
于 2013-01-04T19:42:12.160 回答