X

Como adicionar a miniatura em destaque às colunas de postagem do administrador

Snippets by IsItWP

Você está procurando uma maneira de adicionar a miniatura em destaque à listagem de posts no painel de administração do WordPress? Embora provavelmente exista um plug-in para isso, criamos um trecho de código rápido que você pode usar para adicionar a miniatura da imagem em destaque às colunas de postagem do administrador.

Instruções:

Tudo o que você precisa fazer é adicionar esse código ao arquivo functions.php do seu tema ou em um plugin específico do site:

add_filter('manage_posts_columns', 'posts_columns', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns', 5, 2);

function posts_columns($defaults){
    $defaults['riv_post_thumbs'] = __('Thumbs');
    return $defaults;
}

function posts_custom_columns($column_name, $id){
	if($column_name === 'riv_post_thumbs'){
        echo the_post_thumbnail( 'featured-thumbnail' );
    }
}

Observação: Se esta é a primeira vez que você adiciona trechos de código no WordPress, consulte nosso guia sobre como adicionar corretamente trechos de código no WordPress para não danificar acidentalmente seu site.

Se você gostou desse snippet de código, considere dar uma olhada em nossos outros artigos no site, como: 27 melhores temas do WordPress para blogs de viagem e como usar o Google Optimize no WordPress.

Comentários   Deixe uma resposta

  1. I want a 🔗 in the media library if I don’t have an image

  2. Is there an update on this post. I’ve added the code to my functions file where other custom functions are functioning fine and it doesn’t add anything for extra images within standard or any of the custom post types I’ve created. Any help greatly appreciated. I am running WordPress 5.2.1

  3. When I use this code it brings my products post from woocommerce to my custom post type list.

  4. do i add it to my wp-include/functions.php ? i am using wp4

    1. thanks got it ! love ya !

      1. Cool glad you got it working.

  5. Just what i needed, thanks a lot for great snippet, and the update on how to set thumbnails size just nailed it 🙂

  6. thank you for the snippet. its very helpful as i start to add featured images in all my post. If i like to make this custom column “sortable”, how do i go that? i know its strange to sort image, i just want to see which post do not have image.

  7. Thanks.
    Helped me a lot

  8. Sergey Trushin maio 7, 2013 em 9:59 pm

    Perfect code, thanks!

  9. hi kevin

    could you tell us how to reorder the e.g. 6 columns ???

    thumb – titel – date – user – cont – id
    to:
    id- titel – user – thumb – cont – date

    thanks you

  10. This is working just fine, however the new column is appearing on all custom post type edit screens as well. How would I go about just gettting this to appear on the edit post screens and not the edit CPT screens?

    1. Hi @5f9a29a8407e88a611b5bd004408018e:disqus I know I’m late but hoping this help other with same issue.

      Try this solution
      add_filter( ‘manage_edit-slider_columns’, ‘posts_columns’ ); // slider is the custom post-type you’ve created, feel free to update the post-type to which post-type you only want the custom column to display.

      Hope that helps

  11. Hi,

    I’d like to do that with a custom field value (‘facebook’) would that also be possible?

    Many thanks in advance!

    Cheers,
    Thomas.

    1. you could just get the custom field in the normal way eg:
      $custom_field = get_post_meta($post->ID, ‘facebook’, true);

      then echo the result to the page eg: echo $custom_field;

    2. @wpsnipp:disqus is right but before you can implement or call get_post_meta() function you need to add post_meta first 😉

  12. If post-thumbnails isn’t activated for the theme, only the 1st page/post is listed, with an error in the column.
    To add theme support (functions.php), this is added:
    if ( function_exists( ‘add_theme_support’ ) )add_theme_support( ‘post-thumbnails’ );

    What “IF” checks to see if the theme first supports post-thumbnails?

    1. I agree ill post an updated version of this snippet.

  13. hi, this might seem like a simple question, but where exactly do I place these snippets? functions.php?

    1. Hi onedeep99, yes you should add this snippet to the functions.php of your wordpress theme. I just updated the post to include that little detail as I forgot to mention it.

  14. Thank you for this great snippet. It is very useful to see if there is any thumbs in posts without opening them. Have just a few questions:

    1. Is it possible to get the same feature for the pages too?
    2. Is it possible to display all attached images in a post/page as thumbs instead of just one?
    3. Is it possible to define in which column the they appear in. I get them in the last column in the right of the screen but the default media library displays them in the first column at the left.

    Apreciate your concern 🙂

    1. Hi Zeb,
      1. yes you can ad this to pages as well just add these hooks. above the other hooks.

      add_filter(‘manage_pages_columns’, ‘posts_columns’, 5);
      add_action(‘manage_pages_custom_column’, ‘posts_custom_columns’, 5, 2);

      2. Yes you can reorder the columns Ill post some code asap.
      3. The problem with all attachments is that it may not have enough room for all attachments, Ill see if I can think of another way to display them. 

      1. Thanks alot kevin, 🙂
        If there is any way to specify a number in the snippet for the thumbs to be displayed so that,if needed, a user can change the number OR any other way you might think of would also be great.
        Thanks

        1. I’m not sure I understand can you elaborate a little?

        2. Sorry about not being clear.
          However, I was thinking about a maximum number of how many thumbs to be displayed. For instance, if there is space in the admin, a user can specify max 3 or 4 images to be displayed. Otherwise, “1” could mean only the first or the featured image, even if there is several attachments.
          Another way might be a kind of image rotator using any script.
          I hope I have been helpful 🙂

        3. Ahh ok I see what you mean, Ill look into this a little more could release this as a new snippet.

        4. Great Kevin, thanks 🙂

      2. Is there a way to do it for products?

  15. Thanks a lot for this snippet. Very useful!

    1. Cool glad you like the snippet, don’t forget to follow us on facebook or twitter we post new snippets daily.

  16. hi, I’ve added those snippet but there’s a little problem though. my featured thumbnail appeared in large size (original sizes I think). could you please show me how to resize thumbnail images in admin post columns if possible. thanks.

    1. Hi zarylnaxrie
      ‘featured-thumbnail’ would be a thumbnail size you created however if you replace the code on line nine above with the following.

      echo the_post_thumbnail( array(100,100) );

      Just change the size within the array, the follow page has more details on how post thumbnails function.

      http://codex.wordpress.org/Function_Reference/the_post_thumbnail

      1. Thank you soooooo much!!

  17. Tweets that mention Wordpress Add featured thumbnail to admin post columns – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com fevereiro 25, 2011 em 9:36 am

    […] This post was mentioned on Twitter by Y Combinator Newest!, newsery5 and wp_freak, WPSNIPP. WPSNIPP said: #wordpress Add featured thumbnail to admin post columns http://bit.ly/dHuROT #blog please RT 🙂 […]

Adicionar um comentário

Ficamos felizes por você ter optado por deixar um comentário. Lembre-se de que todos os comentários são moderados de acordo com nossa política de privacidade, e todos os links são nofollow. NÃO use palavras-chave no campo do nome. Vamos ter uma conversa pessoal e significativa.

WordPress Launch Checklist

A lista de verificação definitiva para o lançamento do WordPress

Compilamos todos os itens essenciais da lista de verificação para o lançamento de seu próximo site WordPress em um ebook prático.
Sim, envie-me o livro eletrônico gratuito grátis!