Thumbnails of the Categories

Asked

Viewed 51 times

0

what would be the best way to catch the thumbnails of the categories ?

function get_listaCatPort($atts) {
 echo '<ul class="section section-filters">';
                                $atts = array(
                                        'taxonomy'              => 'portfolio-types',
                                        'orderby'               => 'name',
                                        'order'                 => 'ASC',
                                        'show_count'            => 0,
                                        'hierarchical'          => 1,
                                        'hide_empty'            => 1,
                                        'title_li'              => '',
                                        'depth'                 => 1,
                                        'walker'                => new New_Walker_Category(),
                                        'current_category'      => get_queried_object()->term_id,
                                        );
                                wp_list_categories($atts);
                echo '</ul>';
}
add_shortcode('listaCatPortfolio', 'get_listaCatPort');

1 answer

1


Talk partner, all beauty?

Dude, I can do what you want with the plugin Advanced Custom Fields.

Basically, I create a custom image field and then define its location, during the configuration of it, to appear on the edit page of the group of "taxonomy" that I want.

Having done that, I go to the coding part of the template, in order to display the category Thumb.

So I do:

<div>
    <?php
        $tags = get_terms('MINHA_TAXONOMY');
        $post_tags = wp_get_post_terms($post->ID,'MINHA_TAXONOMY',array('fields' => 'slugs'));
    ?>
    <?php foreach( $tags as $tag ): ?>

    <?php
        $terms = get_the_terms( $post->ID , 'MINHA_TAXONOMY');
        $variable = 'categoria_'. $terms;
        $attachment_id = get_field('NOME_DO_MEU_CAMPO_IMAGEM', $variable);
        $size = "full"; // (thumbnail, medium, large, full or custom size)
        $thumb = wp_get_attachment_image_src( $attachment_id, $size );
        $ID = $tag->term_id;
        $term_id = $ID;
        $image = get_field('NOME_DO_MEU_CAMPO_IMAGEM', 'category_'. $term_id); 
    ?>

    <div>
        <a href="#" class="block <?php echo $tag->slug; ?>">       
        <?php if ( $image ) : ?>
        <img src="<?php echo $image['url']; ?>">
        <?php else : ?>
        <img src="<?php bloginfo('template_directory'); ?>/img/no-picture.jpg">
        <?php endif; ?>   
        <span><?php echo $tag->name ?></span>
        </a>
    </div>
    <?php endforeach; ?> 
</div>

Then, in the settings, you will have to edit in "MINHA_TAXONOMY" and "NOME_DO_MEU_CAMPO_IMAGEM", according to your data.

If you already mess with the plugin Advanced Custom Fields, will understand a little of what I put up. Otherwise, recommend you take a studied in his documentation. The plugin is genius!

I hope it helps you!

Hug.

  • Ummmm guy really, had used this plugin in the past in another situation, it really makes sense what you said, I’ll try here. Thanks for the tip there brother!!!

  • Beauty! In my case there, I display in the theme a listing of the categories I have.

  • Brício, don’t just see if I’m on the right track, please: 1º - create a field of the Type Image 2ª - Localization, create the Rules that would be like "Taxonomy = Category of Porfolios" ?

  • He gave right here brother, Thanks a lot for the tips. ;)

  • Show man, that good! Note that you can still discard some variables if you want to make the code cleaner.

  • yes, I saw this very, very good. This plugin of infinite possibilities, I’m already using for another idea here hehehe.

  • face, I noticed something. When I use: $image = get_field('capa_cat', 'category_' .$tag->term_id); works beautiful, but if I click edit page, it gets loading until lock. Did I forget something, or doing wrong ?

  • Edit page in your wordpress admin?

  • This, like on the page or post that I add this shortcode that I created, if I try to edit it it keeps loading until it gives error 502 or 505, there I tested. So, then I go there in my functions.php file and I comment on this line of $image in my shortcode, then it doesn’t lock anymore, but I don’t list my kkk images

  • Right! Dude, this code that I passed you you add in the theme itself, in the exact location you want to display, nothing should be inserted in functions.php

  • ah ok I’ll do it then, Thank you!!!

  • Brother, just one more question. I’m trying not to show some categories, but I think I’m using the wrong 'category__not_in' => array( '12', '13') .

  • Try using exclude=> '12', '13' .... or else 'exclude' => array( '12', '13'). I can’t remember if it’s one or the other.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.