Wordpress 如何在管理员中显示附在帖子上的图像?
朋友们好 希望这里有人能帮我!我在互联网上到处找都找不到运气:( 我想做的是以某种方式在WP管理员中显示附加到帖子的图像。例如,与帖子特色图像显示的方式相同。我发现的唯一一件事是如何在管理员列中显示帖子的附件数量,但我想看到的是在帖子管理页面中每个帖子内所有附加图像的缩略图1.我想这会很有帮助,因为现在我甚至不知道哪个帖子有附件 我希望不使用插件就能完成这项工作 我已经在网上找了好几天没有运气了。任何帮助都将不胜感激Wordpress 如何在管理员中显示附在帖子上的图像?,wordpress,Wordpress,朋友们好 希望这里有人能帮我!我在互联网上到处找都找不到运气:( 我想做的是以某种方式在WP管理员中显示附加到帖子的图像。例如,与帖子特色图像显示的方式相同。我发现的唯一一件事是如何在管理员列中显示帖子的附件数量,但我想看到的是在帖子管理页面中每个帖子内所有附加图像的缩略图1.我想这会很有帮助,因为现在我甚至不知道哪个帖子有附件 我希望不使用插件就能完成这项工作 我已经在网上找了好几天没有运气了。任何帮助都将不胜感激 非常感谢高级版!解决方案是创建一个元框,然后将缩略图放在其中。这需要函数和。我
非常感谢高级版!解决方案是创建一个元框,然后将缩略图放在其中。这需要函数和。我们还需要找到某篇文章的所有附加图像,我们将使用答案来完成 将所有这些放在一起,假设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_操作('add_meta_box',函数(){
添加元框('att_thumb_display','attached images',函数($post){
$args=数组(
“post_类型”=>“附件”,
“post_mime_type”=>“image”,
“post\u parent”=>$post->ID
);
回声“”;
foreach(获取文章($args)作为$image){
回显“- ID”)“/>
”;
}
回声“
”;
}"职位";;
});
我已将add_meta_box
的参数设置为“post”,该参数指示此meta框在哪种帖子类型上可用。如果您希望它在页面上可用,则必须将其设置为pages。或者如果您希望它在自定义帖子类型上可用,则还必须相应地修改它
我希望这能奏效。我还没有试过。试试这个:
/* === 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 suggestion:
(此处已修改以工作)
add_操作('add_meta_box','o99_add_attach_thumbs_meta_b');
函数o99\添加\附加\拇指\元\ b(){
添加元框('att_thumb_display'、'Attached images'、'o99_render_attache_meta_b'、'post');
}
函数o99_render_attach_meta_b($post){
$output='';
$args=数组(
“post_类型”=>“附件”,
“post_mime_type”=>“image”,
“post\u parent”=>$post->ID
);
//
//如果需要有序列表,请取消注释
//
//$output.='';
$images=get_posts($args);
foreach($images作为$image){
//$output.='- ';
$output.='ID.)“/>”;
//$output.='
';
}
//$output.='
';
echo$输出;
}
您好,Calle,首先,非常感谢您的评论。我尝试了上面的代码,但什么也没发生。我在我的管理面板中没有看到任何更改。我对PHP不太在行,但看看代码,我认为这太难了,因为页面代码中的图像太难显示,访问者看不到(在前端)。我想看看后管理面板的缩略图。非常感谢。我在这方面是个新手,所以我可能会错过一些东西。我在functions.php中添加了您的代码,谢谢!您好,Calle,我发现这非常接近您的解决方案:这实际上显示了附件!!但是在页面的顶部。我相信这只是缺少了将其包裹在元框中的部分。我怎样才能做到呢?非常感谢。添加:@Obmerk kronen在查看代码时,我注意到我漏掉了一个分号。我很惊讶你没有收到错误信息。但是,是的,您将这段代码放在functions.php中(自从我添加了分号后,请再次复制它)。它将在后端面板的文本编辑器下方创建一个框,仅用于博客文章。好的,很高兴它为您工作。除了放弃使用匿名函数,我没有看到任何修改。因此,如果这就是所需的全部,那么您可能没有PHP>=5.3。这可能就是问题所在。嗨,奥伯默克·克罗宁,我真的很感谢你的帮助。我尝试了你的解决方案,这只显示了admin列中的第一个附件。如果可能的话,我想看看Post View管理部分中所有附件的缩略图。非常感谢你!然后就像我说的-使用@calle solution你是我的英雄!!!这正是我要找的!它工作得非常完美!!!非常感谢Obmerk Kronen和@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;
}