StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
794 stars 62 forks source link

ACF setLocation('page_template') Not Working #182

Closed KhawarMehfooz closed 11 months ago

KhawarMehfooz commented 11 months ago

Issue Description

The setLocation('page_template', '==', 'template_name') function in ACF does not seem to be working as expected. I have ensured that the template name is correct and matches exactly, including case sensitivity. However, the ACF fields associated with the specified page template are not displaying.

Code Snippet

$analytics_fields = new FieldsBuilder('analytics_fields');
$analytics_fields
    ->addText('google_analytics_code',[
        'label'=>__('Google Analytics Code','iqs')
    ])
    ->addText('google_tag_manager_code',[
        'label'=>__('Google Tag Manager Code','iqs')
    ])
    ->addTextarea('extra_head_code',[
        'label'=>__('Extra HTML or JS Code for Head','iqs')
    ])
    ->addTextarea('extra_footer_code',[
        'label'=>__('Extra Code for Footer','iqs')
    ]);
$analytics_fields
    ->setLocation('page_template','==','google-analytics');

Video Preview

https://github.com/StoutLogic/acf-builder/assets/80300513/1b68fd59-eb42-4aba-803d-8839afc35ef4

Additional Details:

stevep commented 11 months ago

Usually the value of the page template is the filename.

If you go to the ACF UI and try to build the field group, set the location to Page Template, inspect the dropdown of page template values and see what ACF is using. Then just mimic that in your code. It might be something like google-analytics.php. But checking what ACF itself would use is the best way to find out.

Here is an example on one of my projects. In my case I'd do:

->setLocation('page_template','==','views/template-custom.blade.php');
Screenshot 2023-11-15 at 9 29 38 AM
KhawarMehfooz commented 11 months ago

Thank you. It Worked.