Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Header Element – Custom Taxonomy Featured image

  • Greetings everyone,
    I’m strugling to find a way to create a header element that displays the featured image of custom taxonomy I have created.

    I’ve made a metabox inside the custom taxonomy in order to add a featured image. Although in the header element I’ve created for that taxonomy no matter if I set the background to featured image I get nothing.

    Is there any kind of modification in order to make this?

    **Below is the code I’ve made in my functions.php to create the field in the backend of the taxonomy admin page in order to load and save the image

    // Add a custom field to the taxonomy edit form
    function industry_add_featured_image_field($term) {
        $image_id = get_term_meta($term->term_id, 'featured_image', true);
        ?>
        <tr class="form-field term-group-wrap">
            <th scope="row">
                <label for="featured_image"><?php _e('Featured Image', 'generatepress_child'); ?></label>
            </th>
            <td>
                <input type="hidden" id="featured_image" name="featured_image" value="<?php echo esc_attr($image_id); ?>">
                <div id="featured_image_preview">
                    <?php if ($image_id) : ?>
                        <?php echo wp_get_attachment_image($image_id, 'thumbnail'); ?>
                    <?php endif; ?>
                </div>
                <button type="button" class="button" id="upload_featured_image_button"><?php _e('Upload/Add image', 'generatepress_child'); ?></button>
                <button type="button" class="button" id="remove_featured_image_button"><?php _e('Remove image', 'generatepress_child'); ?></button>
            </td>
        </tr>
        <script>
            jQuery(document).ready(function($) {
                var frame;
                $('#upload_featured_image_button').on('click', function(event) {
                    event.preventDefault();
                    if (frame) {
                        frame.open();
                        return;
                    }
                    frame = wp.media({
                        title: 'Select or Upload Media',
                        button: {
                            text: 'Use this media'
                        },
                        multiple: false
                    });
                    frame.on('select', function() {
                        var attachment = frame.state().get('selection').first().toJSON();
                        $('#featured_image').val(attachment.id);
                        $('#featured_image_preview').html('<img src="' + attachment.sizes.thumbnail.url + '">');
                    });
                    frame.open();
                });
                $('#remove_featured_image_button').on('click', function(event) {
                    event.preventDefault();
                    $('#featured_image').val('');
                    $('#featured_image_preview').html('');
                });
            });
        </script>
        <?php
    }
    add_action('industry_edit_form_fields', 'industry_add_featured_image_field');
    
    // Save the custom field value
    function industry_save_featured_image($term_id) {
        if (isset($_POST['featured_image'])) {
            update_term_meta($term_id, 'featured_image', absint($_POST['featured_image']));
        }
    }
    add_action('edited_industry', 'industry_save_featured_image');
  • Nevermind.

    Issue solved.

    I will attach the code that worked for me in case someone else will need to do the same.

    // Add a custom field to the taxonomy edit form
    function industry_add_featured_image_field($term) {
        $image_id = get_term_meta($term->term_id, 'featured_image', true);
        ?>
        <tr class="form-field term-group-wrap">
            <th scope="row">
                <label for="featured_image"><?php _e('Featured Image', 'generatepress_child'); ?></label>
            </th>
            <td>
                <input type="hidden" id="featured_image" name="featured_image" value="<?php echo esc_attr($image_id); ?>">
                <div id="featured_image_preview">
                    <?php if ($image_id) : ?>
                        <?php echo wp_get_attachment_image($image_id, 'thumbnail'); ?>
                    <?php endif; ?>
                </div>
                <button type="button" class="button" id="upload_featured_image_button"><?php _e('Upload/Add image', 'generatepress_child'); ?></button>
                <button type="button" class="button" id="remove_featured_image_button"><?php _e('Remove image', 'generatepress_child'); ?></button>
            </td>
        </tr>
        <script>
            jQuery(document).ready(function($) {
                var frame;
                $('#upload_featured_image_button').on('click', function(event) {
                    event.preventDefault();
                    if (frame) {
                        frame.open();
                        return;
                    }
                    frame = wp.media({
                        title: 'Select or Upload Media',
                        button: {
                            text: 'Use this media'
                        },
                        multiple: false
                    });
                    frame.on('select', function() {
                        var attachment = frame.state().get('selection').first().toJSON();
                        $('#featured_image').val(attachment.id);
                        $('#featured_image_preview').html('<img src="' + attachment.sizes.thumbnail.url + '">');
                    });
                    frame.open();
                });
                $('#remove_featured_image_button').on('click', function(event) {
                    event.preventDefault();
                    $('#featured_image').val('');
                    $('#featured_image_preview').html('');
                });
            });
        </script>
        <?php
    }
    add_action('industry_edit_form_fields', 'industry_add_featured_image_field');
    
    // Save the custom field value
    function save_industry_featured_image($term_id) {
        if (isset($_POST['featured_image'])) {
            update_term_meta($term_id, 'featured_image', sanitize_text_field($_POST['featured_image']));
        }
    }
    add_action('edited_industry', 'save_industry_featured_image', 10, 2);
    add_action('create_industry', 'save_industry_featured_image', 10, 2);
    
    // Apply the custom taxonomy's featured image as the header background
    function generatepress_custom_taxonomy_header_background() {
        if (is_tax('industry')) {
            $term = get_queried_object();
            error_log('Function called');
            error_log('Term ID: ' . $term->term_id);
            
            $image_id = get_term_meta($term->term_id, 'featured_image', true);
            error_log('Image ID: ' . $image_id);
            
            if ($image_id) {
                $image_url = wp_get_attachment_url($image_id);
                error_log('Image URL: ' . $image_url);
                
                if ($image_url) {
                    ?>
                    <style>
                        .page-hero {
                            background-image: url('<?php echo esc_url($image_url); ?>');
                            background-size: cover;
                            background-position: center;
                        }
                    </style>
                    <?php
                }
            }
        }
    }
    add_action('wp_head', 'generatepress_custom_taxonomy_header_background');
  • Glad to hear you got it working

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.