Site logo

Archived topic

Advance Custom Field from category for my category archive header background?

16 replies · Started by Melissa on April 29, 2019

Viewing posts 1–15 of 17

Hi,

I've been using the code below to take a pic from a custom field on a post and use it as the hero image. (Works great! Thanks!) I also have this custom field for each category. Is there a way to set up a query for the category archive page to use that image?

add_filter( 'generate_page_hero_background_image_url', function( $url ) {
    $background_image = get_post_meta( get_the_ID(), 'hero_url', true );

    if ( $background_image ) {
        $url = $background_image;
    }

    return $url;
} );

Hi there,

So you have a custom field in your categories?

According this this: https://www.advancedcustomfields.com/resources/taxonomy/

We should be able to do this:

add_filter( 'generate_page_hero_background_image_url', function( $url ) {
    $background_image = get_field( 'taxonomy_field_name' );

    if ( $background_image ) {
        $url = $background_image;
    }

    return $url;
} );

Let me know :)

But that page is talking about a taxonomy field. I just have a url field (to the image) on each category. This is the same thing I did on the single posts, and that worked.

(Oddly the Basic URL field isn't one of the ones in that documentation.)

Where is the URL field exactly? Is it on the page when you're editing the category? Did you add the URL field using ACF?

I added it with ACF to the backend page where you edit the category.

So that should work, although I'm not super familiar with ACF, so I could be wrong.

Did you replace taxonomy_field_name in my code with the name you gave the field?

If so, it might be worth checking with their support on how to get a field value from a category.

Okay, I'll head over and ask them about it.

If we know how to get the value, we can plug it into the filter.

Let me know :)

I've got it working now! Thanks!

Awesome! No problem :)

This is exactly what I need to do and I can't get it to work.

Could you please share what worked?

I use ACF, but unfortunately I am not a programmer, so the ACF instructions are like another language to me.

I have setup the ACF category image (shows on backend when editing categories and tags for my custom post type). My custome field name is: category_featured_image

Just can't get it to show in Generatepress header.

Thanks in advance.
Sam

Hi Sam,

Can you open a new topic for your question?

Thanks. :D

This archived topic is closed to new replies.