Support

Account

Home Forums Front-end Issues custom field applied to "page" not seen in archive-page

Solving

custom field applied to "page" not seen in archive-page

  • Hello,
    I’ve this gallery field applied to all pages (type of publication > is equal to > page).
    The goal is to show a random full screen background image on each page of the site.
    I’ve well implemented it in my header.php file and it works well except for this e-shop page. I think the problem comes from Woocommerce who switch the page template to use with an archive-page template instead.
    So to be clear, I’ve created a page called e-shop, and I told Woocommerce this page is the shop page. So now, my original e-shop page doesn’t seems to be read and the custom fields neither. I see that in place of using the page.php template, it’s the archive-product.php that’s used instead.
    I don’t understand why the custom field are not linked to this page?

  • Ok I found a workaround, it’s well an archive-page that’s called so I added a condition where I call my custom field,

    if(is_archive(MY-ARCHIVE-ID)){
        $mygallery=get_field('MY_GALLERY_FIELD',MY-PAGE-ID);
        ...
    };
  • @trouille2 I’m having this issue too, but I can’t get your fix to work. The field I want to call is being used a bit like a featured image, and it lives in the header file. I’ve made a new header called ‘header-shop.php’ and I’m calling this on my Woocommerce shop page. However it’s not pulling in the correct field.

    In your example above, how did you get the ‘MY-ARCHIVE-ID’? I know the page id, but not the archive id.

    Thanks

  • Outside of “The Loop” which is where the header is, you need to tell ACF what $post_id you want to use get_field('field_name', $page_id); $page_id can take many forms form an actual post id to a user to a term. This is covered on https://www.advancedcustomfields.com/resources/get_field/ in the section Get a value from different objects.

    How you get the post ID for the current page depends on what you’re trying to get. For example an archive page is generally associated with a term

    
    $post_id = 0;
    $queried_object = get_queried_object();
    if (isset($queried_ojbect->term_id)) {
      $post_id = 'term_'.$term_id;
    }
    

    and this will only work if you have a custom field associated with the term in question.

    For most “archive” pages, there isn’t anywhere to put a custom field. Archive pages outside of terms do not have an admin page, so there isn’t any way to add a field to a specific archive. In this case the best option is an options page that is associated with a specific post type.

  • @plinth Hi, This is what I have in my code today :

    $bgimg = get_field('bg_img');
    		if(is_woocommerce()){
    			$bgimg = get_field('bg_img', 75);
    		}

    So as time passed, I guess the is_woocommerce() replace the is_archive(MY-ARCHIVE-ID) and cehck if I’m on a woocommerce page.
    Tell me if it solve your issue.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘custom field applied to "page" not seen in archive-page’ is closed to new replies.