Home > database >  How do I enqueue a stylesheet in WordPress for custom taxonomy archives?
How do I enqueue a stylesheet in WordPress for custom taxonomy archives?

Time:05-28

How do I enqueue a stylesheet for custom taxonomy archives? I've created an ACF select field for project_category_css, and set field location to show if Taxonomy is equal to "Project Category". The select field values are:

  • /custom-css/white.css
  • /custom-css/black.css
  • /custom-css/blue.css

I've tried this, but it isn't working:

function taxonomy_style() 
{
    if (is_tax('project_category')) {
        $project_category_css = get_field('project_category_css');
        wp_enqueue_style('project_category_css', get_stylesheet_directory_uri(). $project_category_css);
    }
}
add_action('wp_enqueue_scripts', 'taxonomy_style', 99);

I've also tried this:

function taxonomy_style() 
{
    if (is_tax('project_category', array('Example 1', 'Example 2', 'Example 3', 'Example 4'))) {
        $project_category_css = get_field('project_category_css');
        wp_enqueue_style('project_category_css', get_stylesheet_directory_uri(). $project_category_css);
    }
}
add_action('wp_enqueue_scripts', 'taxonomy_style', 99);

CodePudding user response:

Your conditional for is_tax() is correct, and should be working.

If your ACF field is on the Taxonomy Term admin page, then you need to specify the Term's Object in the get_field - This is explained in the docs - Adding ACF to Tax

If that's the case, then this should work.

function taxonomy_style() {
    if ( is_tax( 'project_category' ) ) {
        $project_category_css = get_field( 'project_category_css', get_queried_object() );
        wp_enqueue_style( 'project_category_css', get_stylesheet_directory_uri() . $project_category_css );
    }
}
add_action( 'wp_enqueue_scripts', 'taxonomy_style', 99 );
  • Related