admin管理员组

文章数量:1129796

My home page uses ACF ("settings home") fields. These fields position the items in the footer. The problem is that they don't display on other sites. How to display fields from the home page on all subpages? So that you do not have to enter the same data on each subpage

To display in footer.php I'm using:

<?php the_field('contact_form_title'); ?>

My home page uses ACF ("settings home") fields. These fields position the items in the footer. The problem is that they don't display on other sites. How to display fields from the home page on all subpages? So that you do not have to enter the same data on each subpage

To display in footer.php I'm using:

<?php the_field('contact_form_title'); ?>
Share Improve this question asked Feb 4, 2021 at 15:24 SimonSimon 235 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

ACF's the_field() and get_field() accept several parameters, one of which is a post ID. So, you just need to get the ID of the page the field is on, like this:

<?php
$frontpage_id = get_option( 'page_on_front' );
the_field('contact_form_title', $frontpage_id); 

// For pages other than home
$page = get_page_by_path('some-page-slug');
the_field('contact_form_title', $page->ID);
?>

If you have the Pro version of the plugin, you might be better to use an ACF Options Page.

More info: https://www.advancedcustomfields.com/resources/options-page/

You'll need to enable it in your theme by adding something like this to your functions.php file.

if( function_exists('acf_add_options_page') ) {
    
    acf_add_options_page(array(
        'page_title'    => 'Theme General Settings',
        'menu_title'    => 'Theme Settings',
        'menu_slug'     => 'theme-general-settings',
        'capability'    => 'edit_posts',
        'redirect'      => false
    ));
}

Then you can pop this into your footer;

<?php the_field( 'field_name', 'option' ); ?>

However you should be weary of the the fact that this will be another database query, so if you're worried about speed then it might be better to hard code the content into footer.php

At this moment in time, Options pages are only in the Pro version of the plugin I believe.

本文标签: phpHow to display acf field values from home page on all pages