Support

Account

Home Forums General Issues Featured image for custom taxonomy

Solving

Featured image for custom taxonomy

  • Hello !

    I’ve created a custom taxonomy and created a custom image field for.
    I would use it as a featured image (for a gridbuiler).
    Any idea how to do it ?

    Thanks !

  • That would dependent on what you are building with and would best be a question for the developers of that solution.

    Terms in WP do not have “Featured Images”

  • Hello,
    Thanks for your answer.
    I do build on Woocommerce and it seems to me that the product category, witch is a wp taxonomy, has the featured image.
    Anyway I’ve ask the gridbuilder support.
    To be continued…

  • That is an image added by WC with code that shows them.

    I wouldn’t know how to show a custom field for a term in whatever you are using to build with, you have not even relieved what it is you’re using.

    As I said, how you show custom fields for a term would be determined by the tool you are using to build the page.

    The only thing I can point you to is PHP code which is not what I thin you’re looking for.

  • Hi,

    Sorry for that misunderstanding, I’m working on woocommerce and the plugin “WP Gridbuilder”.
    And a php code is exactly what I’m looking for !

    So far I’ve done this :

    function save_acf_image_as_featured_in_taxonomy($term_id) {
    // Assurez-vous de remplacer ‘nom_du_champ_image’ par le slug de votre champ ACF
    $image_id = get_field(‘_thumbnail_id_marque’, ‘taxonomy_’ . $term_id);

    if ($image_id) {
    // Enregistrez l’ID de l’image dans les métadonnées de la taxonomie
    update_term_meta($term_id, ‘featured_image_id’, $image_id);
    }
    }
    add_action(‘edited_ma_taxonomie’, ‘save_acf_image_as_featured_in_taxonomy’);
    add_action(‘create_ma_taxonomie’, ‘save_acf_image_as_featured_in_taxonomy’);

    function set_custom_card_thumbnail_for_taxonomy($object) {
    // Obtenez les paramètres du grid courant.
    $grid = wpgb_get_grid_settings();

    // Si cela ne correspond pas à l’ID du grid 6.
    if (6 !== $grid->id) {
    return $object;
    }

    // Si l’objet est une taxonomie (ajustez ‘ma_taxonomie’ pour votre taxonomie).
    if (is_a($object, ‘WP_Term’) && ‘marque’ === $object->taxonomy) {
    // Obtenez l’ID de l’image ACF pour la taxonomie.
    $image_id = get_field(‘_thumbnail_id_marque’, ‘taxonomy_’ . $object->term_id);

    // Si un ID d’image est trouvé, affectez-le.
    if (!empty($image_id)) {
    $object->post_thumbnail = $image_id;
    }
    }

    return $object;
    }
    add_filter(‘wp_grid_builder/grid/the_object’, ‘set_custom_card_thumbnail_for_taxonomy’);

    But it’s not working…
    I was poiting out the product category miniatures of woocomerce because, without any dev, it just works fine with what I’m willing to do.

    Thanks.

  • This document covers getting values from a term https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    You are doing that incorrectly

    
    $image_id = get_field(‘_thumbnail_id_marque’, ‘term_’ . $term_id);
    

    you can also call this function with the term object instead of "term_{$term_id}"

    This document covers using an image field https://www.advancedcustomfields.com/resources/image/

    How you use it depends on what you have set as a return value for the image field. But you can always get only the ID value regardless of the return setting by setting the 3rd parameter to false

    
    $image_id = get_field(‘_thumbnail_id_marque’, ‘term_’ . $term_id, false);
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.