3

Is there anyway to load a custom style in specific taxonomy pages? for example these two pages:

wp-admin/edit-tags.php?taxonomy=news-category&post_type=news
wp-admin/term.php?taxonomy=news-category&..

I added a style to admin but other pager got edited too, how can we load the style when in these two pages of custom post type taxonomy?

5

You can do it like this:

add_action ('admin_enqueue_scripts', 'wpse_style_tax') ;

function
wpse_style_tax ()
{
    // these 3 globals are set during execution of {edit-tags,term}.php
    global $pagenow, $typenow, $taxnow ;

    if (!in_array ($pagenow, array ('edit-tags.php', 'term.php')) {
        return ;
        }
    if ('news' != $typenow) {
        return ;
        }
    if ('news-category' != $taxnow) {
        return ;
        }

    wp_enqueue_style ('wpse_my_handle', 'path_to_css_file', ...) ;

    return ;
}
2

I know there's already an accepted answer, but here's another way to accomplish the same thing using hooks.

//* Make sure we're on the load edit tags admin page
add_action( 'load-edit-tags.php', 'wpse_262299_edit_tags' );
add_action( 'load-term.php', 'wpse_262299_edit_tags' );

function wpse_262299_edit_tags() {

  //* Return early if not the news post type
  if( 'news' !== get_current_screen()->post_type ) {
    return;
  }

  $taxonomies = [ 'news-category', 'other-taxonomy' ];
  //* Add actions to $taxonomy_pre_add_form and $taxonomy_pre_edit_form
  array_filter( $taxonomies, function( $taxonomy ) {
    add_action( "{$taxonomy}_pre_add_form",  'wpse_262299_enqueue_style' );
    add_action( "{$taxonomy}_pre_edit_form", 'wpse_262299_enqueue_style' );
  });
}

function wpse_262299_enqueue_style( $taxonomy ) {
  //* All the logic has already been done, do enqueue the style
  wp_enqueue_style( 'wpse-262299', plugins_url( 'style.css', __FILE__ ) );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.