dtt5830659 2016-03-04 18:12
浏览 100
已采纳

在WordPress中使用PHP更改CSS样式

I'm somewhat new to PHP and WordPress and I'm trying style a menu plugin (their support is busy) based on what page the user is on.

What I want is for the background and other styles to change if it isn't on the front page.

I've tested the CSS in inspect element so the selectors are correct, but I don't really know how to implement this in a clean and effective way.

The messy way would be to create another two menus (because top_navigation and main_menu are separate so 4 total) style those in their plugin settings and replace the two menus on the front page with two other ones when not on the front page using php. So if I need to make a change the menu's items I'd have to do it for two or four times which to me is stupid.


So far I've come up with creating a PHP variant of the style sheet and rewriting the URL to redirect from .css to .php however I don't really have a firm grasp on how I would do this from the root directory's .htaccess as the location of the two style sheets are far from it.

Location of style sheets:/wp-content/plugins/hmenu/_frontend_files/_menu_5/_css
Name of style sheets: hero_menu_styles.css and hero_menu_styles.php
PHP Style sheet:

<?php 
header('Content-type: text/css; charset: UTF-8'); 
?>

/*  Normal CSS Here */

<?php 
if(!is_front_page())
{
echo "#hmenu_load_5 .hmenu_main_holder {background-color: #67b7e1;}"
}
?>

.htaccess:
I am almost 100% sure there is something wrong with this.
I have no idea what though.

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase "/wp-content/plugins/hmenu/_frontend_files/_menu_5/_css/"
RewriteRule ^hero\_menu\_styles\.css$ hero\_menu\_styles\.php [L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

So I have a bunch of questions.

  1. Does echo work the way that I'm currently writing it assuming that it is in a stylesheet.php rather than a stylesheet.css?
  2. What if I were to reference an additional stylesheet.php on all my pages and just put it in there rather than rewriting my URLs? Is there a way to make sure it would override the other stylesheets?
  3. Can I have multiple .htaccess files
    (for example in /wp-content/plugins/hmenu/_frontend_files/_menu_5/_css)
    so I don't have to bother with long RewriteBase headaches?
  4. Am I correct in that I cannot just stick this code+css into my WordPress theme's header.php or functions.php?
  5. Do I need to require('/wp-load.php'); or load some WordPress functions (no idea what other than wp-load though) on my .php stylesheet if I wanted to use is_front_page()?
  • 写回答

2条回答 默认 最新

  • douxian1939 2016-03-04 18:47
    关注

    If you need to style a menu based solely on whether you are on the homepage or not. Then you can simply use CSS selectors based on what you need to change.

    Target home page menu style:

    body.home .menu-class {background: #333;}

    Target menu on a page:

    body.page .menu-class {background: #FFF;}

    You can find more scenarios and their classes here: https://codex.wordpress.org/Function_Reference/body_class

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵