Home > Net >  PHP Help Using ACF Pro to Load/Enqueue CSS Stylesheet on WordPress Archives, Pages, and Posts
PHP Help Using ACF Pro to Load/Enqueue CSS Stylesheet on WordPress Archives, Pages, and Posts

Time:05-24

I have no experience with PHP. I am using ACF select fields to give my client the option of choosing a stylesheet at the individual page, post, and archive level. Just for reference, the fields are singular_css, portfolio_css, and careers_css, depending on the content type, and share these dropdown values:

/wp-content/themes/hello-theme-child-master/custom-css/white.css : White
/wp-content/themes/hello-theme-child-master/custom-css/black.css : Black
/wp-content/themes/hello-theme-child-master/custom-css/blue.css : Blue
/wp-content/themes/hello-theme-child-master/custom-css/tan.css : Tan
/wp-content/themes/hello-theme-child-master/custom-css/gray.css : Gray

This script in the functions.php seems to work as expected to control the stylesheet that gets loaded on singular Posts/Pages, but doesn't allow them to choose a stylesheet for custom post type archives:

/** Insert Dynamic Stylesheet Into <Head> using ACF Field **/
add_action( 'wp_head', 'add_head_script' );
function add_head_script() { ?>

<?php 
$singular_css = get_field('singular_css');
if( $singular_css ): ?>
    <link rel="stylesheet" href="<?php echo esc_url( $singular_css ); ?>">
<?php endif; ?>

<?php }

Since I wasn't able to control the stylesheet on custom post archives the same way, I've created options pages for them:

/** Creates ACF Options Pages **/
if( function_exists('acf_add_options_page') ) {
    
    acf_add_options_sub_page(array(
        'page_title'    => 'Portfolio Style',
        'menu_title'    => 'Portfolio Style',
        'parent_slug'   => 'edit.php?post_type=portfolio',
        'capability'    => 'manage_options'
    ));
    
    acf_add_options_sub_page(array(
        'page_title'    => 'Careers Style',
        'menu_title'    => 'Careers Style',
        'parent_slug'   => 'edit.php?post_type=careers',
        'capability'    => 'manage_options'
    ));
    
}

And tried to enqueue the stylesheets instead, but something isn't working:

/** Enqueue Dynamic Stylesheet using ACF Field **/
function dynamic_style() {
    $singular_css = get_field('singular_css');
    $portfolio_css = get_field('portfolio_css', 'option');
    $careers_css = get_field('careers_css', 'option');

    if (is_singular()) {
    wp_enqueue_style( 'singular_css', get_stylesheet_directory_uri(). $singular_css );
    }
    elseif (is_post_type_archive('portfolio')) {
    wp_enqueue_style( 'portfolio_css', get_stylesheet_directory_uri(). $portfolio_css );
    }
    elseif (is_post_type_archive('careers')) {
    wp_enqueue_style( 'careers_css', get_stylesheet_directory_uri(). $careers_css );
    }
}
add_action( 'wp_enqueue_scripts', 'dynamic_style' );

I also tried writing it this way, but it still doesn't work:

/** Enqueue Dynamic Stylesheet using ACF Field **/
function singular_style() {
    $singular_css = get_field('singular_css');
    if (is_singular()) {
    wp_enqueue_style( 'singular_css', get_stylesheet_directory_uri(). $singular_css );
    }
}
add_action( 'wp_enqueue_scripts', 'singular_style' );
    
function portfolio_style() {
    $portfolio_css = get_field('portfolio_css', 'option');
    if (is_post_type_archive('portfolio')) {
    wp_enqueue_style( 'portfolio_css', get_stylesheet_directory_uri(). $portfolio_css );
    }
}
add_action( 'wp_enqueue_scripts', 'portfolio_style' );  
    
function careers_style() {
    $careers_css = get_field('careers_css', 'option');  
    if (is_post_type_archive('careers')) {
    wp_enqueue_style( 'careers_css', get_stylesheet_directory_uri(). $careers_css );
    }
}
add_action( 'wp_enqueue_scripts', 'careers_style' );

CodePudding user response:

I would suggest you to change the dropdown value to

  /custom-css/white.css : White
    /custom-css/black.css : Black
    /custom-css/blue.css : Blue
    /custom-css/tan.css : Tan
    /custom-css/gray.css : Gray

And then add the following code on functions.php file

 function dynamic_style()
    {
        if (is_singular()) {
            global $post;
            $cog_stylesheet = get_field('cog_background_color', $post->ID);
            wp_enqueue_style('singular_css', get_stylesheet_directory_uri() . $cog_stylesheet);
        }else {

    global $post;
    $post_type = get_current_post_types($post);

    if ($post_type == 'portfolio') {
        $portfolio_css = get_field('portfolio_css', 'option');
        wp_enqueue_style('portfolio_css', get_stylesheet_directory_uri() . $portfolio_css);
    } elseif ($post_type == 'careers') {
        $careers_css = get_field('careers_css', 'option');
        wp_enqueue_style('careers_css', get_stylesheet_directory_uri() . $careers_css);
    } 
}
    }
    add_action('wp_enqueue_scripts', 'dynamic_style');

However, if you still want to use the current dropdown value add the following code on the functions.php file to

function dynamic_style()
{
    if (is_singular()) {
        global $post;
        $cog_stylesheet = get_field('cog_background_color', $post->ID);
        wp_enqueue_style('singular_css', home_url() . $cog_stylesheet);
    } else {

    global $post;
    $post_type = get_current_post_types($post);

    if ($post_type == 'portfolio') {
        $portfolio_css = get_field('portfolio_css', 'option');
        wp_enqueue_style('portfolio_css', home_url() . $portfolio_css);
    } elseif ($post_type == 'careers') {
        $careers_css = get_field('careers_css', 'option');
        wp_enqueue_style('careers_css', home_url() . $careers_css);
    } 
}
}
add_action('wp_enqueue_scripts', 'dynamic_style');

And add the following code on functions.php so that we can get post_type on archive page

function get_current_post_types($object = null)
{

    // if a numeric value passed, assume it is a post ID
    if (($object) && (is_numeric($object))) {
        $object = get_post($object);
    }
    // if an object is passed, assume to be a post object
    if (($object) && (is_object($object))) {
        return get_post_type($object);
    }

    // standard single post type checks
    if (is_404()) {
        return '';
    }
    // update: removed this check, handled by is_singular
    // if (is_single()) {return 'post';}
    if (is_page()) {
        return 'page';
    }
    if (is_attachment()) {
        return 'attachment';
    }
    if (is_singular()) {
        return get_post_type();
    }

    // if a custom query object was not passed, use $wp_query global
    if ((!$object) || (!is_object($object))) {
        global $wp_query;
        $object = $wp_query;
    }
    if (!is_object($object)) {
        return '';
    } // should not fail

    // if the post_type query var has been explicitly set
    // (or implicitly set on the cpt via a has_archive redirect)
    // ie. this is true for is_post_type_archive at least
    // $vqueriedposttype = get_query_var('post_type'); // $wp_query only
    if (property_exists($object, 'query_vars')) {
        $posttype = $object->query_vars['post_type'];
        if ($posttype) {
            return $posttype;
        }
    }

CodePudding user response:

Thank you Bijay! You've been a huge help. Just to recap for others, here are the steps I took:

I registered these options pages:

/** Creates ACF Options Pages **/
if( function_exists('acf_add_options_page') ) {
    
    acf_add_options_sub_page(array(
        'page_title'    => 'Posts Style',
        'menu_title'    => 'Posts Stylesheet',
        'parent_slug'   => 'edit.php',
        'capability'    => 'manage_options'
    ));
    
    acf_add_options_sub_page(array(
        'page_title'    => 'Portfolio Style',
        'menu_title'    => 'Portfolio Stylesheet',
        'parent_slug'   => 'edit.php?post_type=portfolio',
        'capability'    => 'manage_options'
    ));
    
    acf_add_options_sub_page(array(
        'page_title'    => 'Careers Style',
        'menu_title'    => 'Careers Stylesheet',
        'parent_slug'   => 'edit.php?post_type=careers',
        'capability'    => 'manage_options'
    ));
    
}

I created my ACF fields:

  • singular_css
  • articles_css
  • portfolio_css
  • careers_css

And they all share these dropdown values:

  • /custom-css/white.css : White
  • /custom-css/black.css : Black
  • /custom-css/blue.css : Blue
  • /custom-css/tan.css : Tan
  • /custom-css/gray.css : Gray

This code in the functions.php file works:

/** Enqueue Dynamic Stylesheet using ACF Field **/
function dynamic_style()
{
    if (is_singular()) {
        global $post;
        $singular_css = get_field('singular_css', $post->ID);
        wp_enqueue_style('singular_css', get_stylesheet_directory_uri(). $singular_css);
    } elseif (is_home()) {
        $articles_css = get_field('articles_css', 'option');
        wp_enqueue_style('articles_css', get_stylesheet_directory_uri(). $articles_css);
    } elseif (is_post_type_archive('portfolio')) {
        $portfolio_css = get_field('portfolio_css', 'option');
        wp_enqueue_style('portfolio_css', get_stylesheet_directory_uri(). $portfolio_css);
    } elseif (is_post_type_archive('careers')) {
        $careers_css = get_field('careers_css', 'option');
        wp_enqueue_style('careers_css', get_stylesheet_directory_uri(). $careers_css);
    } elseif (is_404()) {
        wp_enqueue_style('white_css', get_stylesheet_directory_uri(). '/custom-css/white.css');
    }
}
add_action('wp_enqueue_scripts', 'dynamic_style', 99);
  • Related