Support

Account

Home Forums General Issues ACF Groups and get_field

Solved

ACF Groups and get_field

  • Hey guys, I think I am going crazy so I am sorry if this is stupid or obvious. I need some help with the ACF group and using them.

    I set up a group for “page type” to be “Front Page” and the name is “hero” with the label “Hero”. I have added three fields to this group which are:

    – Label: Minor Title
    Name: minor_title
    – Label: Main Title
    Name: main_title
    – Label: Link Title
    Name: link_title

    Now when I click on the only page I have “Home – Front Page” the group comes up and I can enter the values. Update and go to the site. Nothing shows up. So I check the actual back-end (I am doing this on a local server using xampp).

    This is the code (This is inside front-page.php in Visual Studio Code):

    
    <?php get_header();?>
    
    <?php $hero = get_field('hero');?>
    
    <section class="bg-1 h-900x main-slider pos-relative">
        <div class="triangle-up pos-bottom"></div>
        <div class="container h-100">
            <div class="dplay-tbl">
                <div class="dplay-tbl-cell center-text color-white">
                    <h5><b><?php echo $hero["minor_title"];?></b></h5>
                    <h1 class="mt-30 mb-15"><?php echo $hero["main_title"];?></h1>
                </div><!-- dplay-tbl-cell -->
            </div><!-- dplay-tbl -->
        </div><!-- container -->
    </section>
    
    <?php get_footer();?>
    
    

    What am I doing wrong? What is missing? When I do this there is no text at all, no minor title or main title. If I replace the php code with regular text like “Hello” instead inside the code it works. But as soon as I put that php code it just shows nothing. I tried to inspect the code and it doesn’t show the php code when I inspect it at all, not even the
    “<?php $hero = get_field(‘hero’);?>”. I am not sure if it should or not but still. I am new to PHP and to WordPress so I would appreciate any help. I can provide more info if needed of course! 🙂

  • PHP is parsed on the server; what is sent back to the browser is plain text/HTML, so you can’t inspect PHP with your browser.

    Your code looks correct on first sight. What happens if you do <?php print_r($hero); ?> after the get_field line? Does anything come up?

  • Fair enough, but the header part works so I know PHP in itself works right?

    Nothing comes up or happens when I do the <?php print_r($hero); ?>, what is supposed to happen? I now also noticed when I inspect that it says an error to the far right (using Google Chrome):

    
    scripts.js?ver=1:46 Uncaught TypeError: $ is not a function
        at panelAccordian (scripts.js?ver=1:46)
        at scripts.js?ver=1:89
        at scripts.js?ver=1:138
    
    DevTools failed to load SourceMap: Could not load content for http://localhost/wpone/wp-content/themes/ThirdTheme/plugin-frameworks/maps/swiper.jquery.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
    DevTools failed to load SourceMap: Could not load content for http://localhost/wpone/wp-content/themes/ThirdTheme/plugin-frameworks/bootstrap.min.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
    DevTools failed to load SourceMap: Could not load content for http://localhost/wpone/wp-content/themes/ThirdTheme/plugin-frameworks/bootstrap.min.css.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
    
    

    Why did this happen? Is this related?

    This is my functions.php:

    
    <?php
    
        function load_stylesheets(){
            
            // Everything from the Luigis template
            wp_register_style("font", get_template_directory_uri() . "/fonts/beyond_the_mountains-webfont.css", array(), 1, "all");
            wp_enqueue_style("font");
            
            wp_register_style("bootstrap", get_template_directory_uri() . "/plugin-frameworks/bootstrap.min.css", array(), 1, "all");
            wp_enqueue_style("bootstrap");
    
            wp_register_style("swiper", get_template_directory_uri() . "/plugin-frameworks/swiper.css", array(), 1, "all");
            wp_enqueue_style("swiper");
            
            wp_register_style("ionicons", get_template_directory_uri() . "/fonts/ionicons.css", array(), 1, "all");
            wp_enqueue_style("ionicons");
    
            wp_register_style("styles", get_template_directory_uri() . "/common/styles.css", array(), 1, "all");
            wp_enqueue_style("styles");
    
            // This is a custom stylesheet of CSS that you can use to override anything of the above.
            wp_register_style("custom", get_template_directory_uri() . "/custom.css", array(), 1, "all");
            wp_enqueue_style("custom");
    
        }
    
        add_action("wp_enqueue_scripts", "load_stylesheets");
    
        function load_js() {
            
            wp_register_script("jquery", get_template_directory_uri() . "/plugin-frameworks/jquery-3.2.1.min.js", array(), 1, 1);
            wp_enqueue_script("jquery");
            
            wp_register_script("bootstrapjs", get_template_directory_uri() . "/plugin-frameworks/bootstrap.min.js", array(), 1, 1);
            wp_enqueue_script("bootstrapjs");
    
            wp_register_script("swiperjs", get_template_directory_uri() . "/plugin-frameworks/swiper.js", array(), 1, 1);
            wp_enqueue_script("swiperjs");
    
            wp_register_script("scriptsjs", get_template_directory_uri() . "/common/scripts.js", array(), 1, 1);
            wp_enqueue_script("scriptsjs");
    
            wp_register_script("customjs", get_template_directory_uri() . "/customjs.js", array(), 1, 1);
            wp_enqueue_script("customjs"); 
        }
    
        add_action("wp_enqueue_scripts", "load_js");
    

    Did I do something wrong here? I am very lost..

  • So update:

    There was a section that wasn’t working on the site and now it does. The errors that were listed above I fixed by changing:

    wp_register_script("jquery", get_template_directory_uri() . "/plugin-frameworks/jquery- 
     3.2.1.min.js", array(), 1, 1);
     wp_enqueue_script("jquery");
    

    To:

    
    wp_register_script("jQuery", get_template_directory_uri() . "/plugin-frameworks/jQuery-3.2.1.min.js", array(), 1, 1);
    wp_enqueue_script("jquery");
    

    Basically the same except the “jquery” needs to be “jQuery” with a capital Q. However the php part is still not showing anything. I can honestly say that I am not even sure the get_field(“hero”); is getting any results back at all. Not sure how to check it either.

  • Second Update:

    After I did this code: <?php $hero = get_field("hero");?>

    I put in this code as seen on: https://www.advancedcustomfields.com/resources/get_field/
    To check if there are any values at all. Again I am new to this, ACF and PHP so I am hoping I did this part correctly.

    if( $hero ) 
    {
    echo $hero;
    } 
    else 
    {
    echo "empty";
    }
    ;?>

    By doing this, at the top of the page, below the header and above the section (see first post), it showed a gray bar with the word “Empty”. Meaning no values are being returned? So what am I doing wrong?

  • Third update:

    So I have no idea what is wrong or how to fix it. But it is now obvious that the actual problem is that the <?php $hero = get_field("hero");?> doesn’t actually get any values at all.

    I did this for simplicity:

    <?php $txt = "Hello world!";?>

    And then added it to the code html part as such:

    <h5><b><?php echo $txt ;?></b></h5>

    And it does come up as “Hello World” so it does work. Meaning the issue is that for some reason ACF groups doesn’t work or at least isn’t somehow connected meaning it doesn’t retrieve any values at all.

  • Last Update: Now Works.

    I am uncertain as to why precisely or if I missed something important. However, for anyone reading this I installed the plugin “Classic Editor” and then redid the from group and now it works. I am not sure if it’s a requirement to have Classic Editor, maybe it is and I just haven’t read it in the guide or installation things etc. But it now works.

  • Classic editor should have nothing to do with it, ACF works with Gutenberg, too. And the JS errors have nothing to do with this as well.

    It’s hard to tell why it isn’t working (without classic editor) from the little snippet you provided. It would help if you showed us all of the template code and perhaps a screenshot of the field setup in the admin screen.

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

You must be logged in to reply to this topic.