Site logo

Archived topic

Custom Post Type: Page hero element is not showing

27 replies · Started by webteam on February 16, 2022

Viewing posts 16–28 of 28

Can we try doing a var_dump of the post meta?

You can do it with this code:
var_dump( get_term_meta( get_queried_object_id(), 'category_image', bool $single = false ) );

If you run this, a bunch of string will appear on the page. It should show us the actual value stored on the category_image.

Once we see it, we should know what to do with it.

Let us know. :)

Hi Elvin,

Not sure if the code is wrong, it's only showing "string(4) "2251" for the category.

This is the code used in funtions.php (new ACF field is 'category_image_pts')

function get_acf_category_featured_image( $url ) {
		$term = get_queried_object();		
		$background_image = get_field('category_image_pts', $term);
		if ( $background_image ) {
			$url = $background_image;
			var_dump( get_term_meta( get_queried_object_id(), 'category_image_pts', true));		
    	}
    	return $url;		
}

add_action('wp', function(){
	if(is_archive()){
		add_filter( 'generate_after_header', 'get_acf_category_featured_image'  );
	}
});

That means its returning an Image ID instead of an Image Array or Image URL.

We can use this to get the url using this snippet:

$img_ID = get_term_meta( get_queried_object_id(), 'category_image_pts', true);
$img_src = wp_get_attachment_image_src($img_ID, 'full');

Where $img_src is the URL of the term meta image. With this, you can do $url = $img_src.

Can you do a var_dump($background_image); as well? just to know what ACF returns.

Sweet, that's right. var_dump($background_image); also returns the ID, and the var_dump($img_src) now returns the actual URL if category already has and without listing (but not the fallback image)


string(4) "2385" 
array(4) { [0]=> string(109) "https://professionaltrainingsolutionscomau.wpstaging.io/wp-content/uploads/2022/02/Rectangle-14pts-mining.jpg" [1]=> int(1180) [2]=> int(1180) [3]=> bool(false) }

Now that we have something we can use for the image, let's do the fallback image.

About the code:

Can you explain what's the goal for this code?

add_action('wp', function(){
	if(is_archive()){
		add_filter( 'generate_after_header', 'get_acf_category_featured_image'  );
	}
});

To understand if the goal matches what this code actually does.

As it is, it does nothing as you're using a filter for an action hook.

If you wish to apply the background to the container block, you'll need a different filter.

Or you can assign the PHP value with CSS.

That code bit was used from a different thread via the forum, it is just to see the output at least var_dump on the Archive template.
And yes, the background needs to be applied to the container block as other pages.
Can we go forward with the correct hook, please?

You'll need the generateblocks_background_image_url filter:

Example usage:

add_filter( 'generateblocks_background_image_url', function( $url, $settings ) {
    $field_img = get_term_meta( get_queried_object_id(), 'category_image_pts', true);

    //targets a specific container block 
    if ( 'unique-id' === $settings['uniqueId'] ) {

        return get_the_post_thumbnail_url();
    }

    return $url;
} );

To clarify further, is this result:
array(4) { [0]=> string(109) "https://professionaltrainingsolutionscomau.wpstaging.io/wp-content/uploads/2022/02/Rectangle-14pts-mining.jpg" [1]=> int(1180) [2]=> int(1180) [3]=> bool(false) } from doing a var_dump of $img_src?

If yes, to get the actual URL, you need to do $img_src[0].

Like this:

add_filter( 'generateblocks_background_image_url', function( $url, $settings ) {
    $img_ID = get_term_meta( get_queried_object_id(), 'category_image_pts', true);
    $img_src = wp_get_attachment_image_src($img_ID, 'full');

    /* targets a specific container block */
    if ( 'unique-id' === $settings['uniqueId'] && is_archive() ) {
        /* if field has value */
        if( $img_src[0] ){
            $url = $img_src[0];
        } 
    }
    return $url;
} );

unique-id is the number suffix you see on a container block when you inspect it. Example - https://share.getcloudapp.com/bLuxndBz

The fallback is assigned on the container block - https://share.getcloudapp.com/NQuxP7Nq

But we can assign it through the code as well.

Yes that's for var_dump of $img_src.

And the function gives the below error.
Uncaught ArgumentCountError: Too few arguments to function {closure}()

Strange, didn't get the error when I tested this.

Let's try specifying the $arg and priority and moving it to a callback function.

Try this:

add_filter( 'generateblocks_background_image_url', 'get_acf_category_featured_image', 20, 2 );

function get_acf_category_featured_image( $url, $settings ) {
    $img_ID = get_term_meta( get_queried_object_id(), 'category_image_pts', true);
    $img_src = wp_get_attachment_image_src($img_ID, 'full');

    /* targets a specific container block */
    if ( 'unique-id' === $settings['uniqueId'] && is_archive() ) {
        /* if field has value */
        if( $img_src[0] ){
            $url = $img_src[0];
        } 
    }
    return $url;
}

Reminder: While this workaround can work, it's unusual that an ACF field that's suppose to return URL returns an ID instead.

I remember ACF having a bug with fields that were originally text fields turned into image fields having this issue but I'm not sure if this still the case. I suggest contacting ACF about this as I believe having this fixed is the better approach instead of having workarounds.

Try this:

add_filter( 'generateblocks_background_image_url', 'get_acf_category_featured_image', 20, 2 );

function get_acf_category_featured_image( $url, $settings ) {
    $img_ID = get_term_meta( get_queried_object_id(), 'category_image_pts', true);
    $img_src = wp_get_attachment_image_src($img_ID, 'full');

    /* targets a specific container block */
    if ( 'unique-id' === $settings['uniqueId'] && is_archive() ) {
        /* if field has value */
        if( $img_src[0] ){
            $url = $img_src[0];
        } elseif ( !$img_src[0] && ! empty( $settings['gpUseFallbackImageBg'] ) && !have_posts() ) {
			if ( isset( $settings['bgImage']['id'] ) ) {
				$image_size = ! empty( $settings['bgImageSize'] ) ? $settings['bgImageSize'] : 'full';
				$image_src = wp_get_attachment_image_src( $settings['bgImage']['id'], $image_size );

				if ( is_array( $image_src ) ) {
					$url = $image_src[0];
				} else {
					$url = $settings['bgImage']['image']['url'];
				}
			} else {
				$url = $settings['bgImage']['image']['url'];
			}
		}
    }
    return $url;
}

This assumes the fallback toggle is active and there is an image assigned as fallback on the container block. :D

Hi Elvin,

Thank you so much for the following up and the support.

It is working now and we will follow up with the ACF team to see if there is a problem with the Image tag.

This is one of our test run projects with GP and so far it has been an awesome experience.
Thank you guys again :)

No problem. Glad to be of any help. :)

This archived topic is closed to new replies.