Support

Account

Home Forums Backend Issues (wp-admin) Loading ACF Field Output on Page Editor

Solving

Loading ACF Field Output on Page Editor

  • I have an ACF field that adds custom CSS to the page associated with that field. I recently added the output of the field to the Editor page for a better editing experience that looked closer to the page output. It works, but I’m getting these errors:

    • Notice: Undefined variable: post in /var/www/billschatbot.com/wp-content/themes/apache/functions.php on line 86
    • Notice: Trying to get property ‘ID’ of non-object in /var/www/billschatbot.com/wp-content/themes/apache/functions.php on line 86

    Here’s the code on line 86 in functions.php:

    $post_css = get_field(‘custom_css’, $post->ID); if ( !empty($post_css) ): echo ‘<style type=”text/css”>’ . $post_css . ‘</style>’; endif;

  • $post is not defined when function.php is loaded.

    $post is not defined until your template file is loaded and usually inside “The Loop”

    Is your above code part of some function that is called?

  • Hey John,

    That makes sense, but somehow it does load the ACF field in the admin area correctly. This is the full equation:

        // Load admin styling
        function apache_admin_style() {
            wp_enqueue_style('admin-css', get_template_directory_uri() . '/css/admin.css');
            wp_enqueue_script( 'admin-js', get_template_directory_uri() . '/js/admin.js', array( 'jquery' ), null, true );
            include get_template_directory() . '/inc/c-styling.php';
            $post_css = get_field('custom_css', $post->ID); if ( $post_css ) { echo '<style type="text/css">' . $post_css . '</style>'; };
        }
    
        add_action('admin_enqueue_scripts', 'apache_admin_style');
  • I don’t think that $post will be defined at this point, so you may need to figure it out, see my comments

    
    // Load admin styling
        function apache_admin_style() {
            wp_enqueue_style('admin-css', get_template_directory_uri() . '/css/admin.css');
            wp_enqueue_script( 'admin-js', get_template_directory_uri() . '/js/admin.js', array( 'jquery' ), null, true );
            include get_template_directory() . '/inc/c-styling.php';
    
    global $post;
    // this may still not be set, you may need to get the post ID from the URL ($_GET)
    // or you may be able to use get_queried_object()
    
            $post_css = get_field('custom_css', $post->ID); if ( $post_css ) { echo '<style type="text/css">' . $post_css . '</style>'; };
        }
    
        add_action('admin_enqueue_scripts', 'apache_admin_style');
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.