如何修改 WP 管理仪表板(后端)特定页面中的 CSS

我正在尝试删除 WordPress 管理面板的仪表板页面的填充和标题。仪表板经过重新设计"Welcome Dashboard for elementor" + Elementor。


我尝试过这个脚本:


var domainURL = window.location.origin;

var path = window.location.pathname;

if ((path == "/wp-admin/" || path == "/wp-admin" || path == "/wp-login.php") && domainURL+path)

{

    document.getElementsByClassName("h1").style.display = "none";

}

它不起作用。请问您有解决办法或想法来实现这一目标吗?


Smart猫小萌
浏览 110回答 3
3回答

米琪卡哇伊

您必须将 css 注入到 wordpress 标头中才能实际修改 wordpress css 管理控制台。在您的function.php文件中添加以下内容:<?php function theme_admin_css() {echo '<style>/* ... Your custom css goes here ... */</style>'; }add_action( 'admin_head', 'theme_admin_css' ); ?>现在,要轻松找到您想要定位和样式化的元素,您可以执行以下操作:在浏览器中:右键单击元素 > 检查。在源代码中找到您的元素:右键单击>复制>复制选择器现在您可以将选择器粘贴到样式标签之间并对其进行自定义。还有一件事,你应该使用该!important语句(例如background-color:red!important:)

POPMUISE

一般来说,这些<body>类包含特定于该页面的唯一类(例如页面名称),您可以将其添加为代码中的第一个选择器CSS。如果没有,您可以使用以下命令将CSS类添加到标记中<body>admin_body_class// Backendfunction filter_admin_body_class( $classes ) {      // Current url    $current_url = '//' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];        // Get last part from url. Expl: index.php    $last_part = basename( parse_url( $current_url, PHP_URL_PATH ) );        if ( $last_part == 'index.php' ) {        // String        $classes .= 'my-custom-backend-class';    }        return $classes;}add_filter( 'admin_body_class', 'filter_admin_body_class', 10, 1 );附加:对于前端页面,您可以使用body_class注意: WooCommerce 和 WordPress 的条件标签可在模板文件中使用,以根据页面匹配的条件更改显示的内容。// Frontendfunction filter_body_class( $classes ) {    // Returns true on the cart page.    if ( is_cart() ) {        // Array        $classes[] = 'my-custom-frontend-class';    }        return $classes;}add_filter( 'body_class', 'filter_body_class', 10, 1 );

慕的地6264312

我遇到了类似的问题,所以我偶然发现了这个,但最终我能够从 WP admin 中的模板加载 css 文件。因此不需要注入<style>元素,而是可以这样做:function enqueue_admin_stylesheet(){&nbsp; &nbsp; if ('a-specific-page' == get_current_screen()->base) {&nbsp; &nbsp; &nbsp; &nbsp; wp_enqueue_style(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'your-admin-css',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sprintf('%s/assets/css/admin-style.css', get_template_directory_uri())&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }}add_action('admin_enqueue_scripts', 'enqueue_admin_stylesheet');
打开App,查看更多内容
随时随地看视频慕课网APP