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' );
2
Answers
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:
I created my ACF select fields:
And they all share these dropdown values:
This code in the functions.php file works:
I would suggest you to change the dropdown value to
And then add the following code on functions.php file
However, if you still want to use the current dropdown value add the following code on the functions.php file to
And add the following code on functions.php so that we can get post_type on archive page