使用Oxygen Builder构建的Wordpress网站。我的菜单旁边有一个线性图标(购物车)。当购物车中有一个项目(非空)时,我正在尝试更改该图标的颜色。对于我的一生,我无法使其正常运转。它会使站点崩溃,拧紧车身边缘等。
我在functions.php中尝试了以下内容的许多变体:
add_action('wp_footer', 'crit_cart') ;
function crit_cart() {
if(sizeof( WC()->cart->get_cart() ) > 0 ) {
jQuery('#fancy_icon-62-12').css('color','blue');
}
}
和
function crit_cart() {
if(WC()->cart->get_cart_contents_count() != 0){
echo "<script type=text/javascript> jQuery("#link-64-12").css('color','#47beff');</script>";
}
} add_filter('wp_headers','crit_cart');
我已经尝试了最后一个在jQuery之前有无回声的选项。
任何帮助将不胜感激。预先感谢!
您的想法正确,但是存在语法错误。
首先,您尝试从PHP运行jQuery。PHP在后端运行,而jQuery / JS在前端运行,因此您不能以这种方式运行它。
在第二个中,您做得正确-将脚本回显到前端以运行,但是第二个引号结束了回显,并且它将返回错误。试试看:
add_action('wp_footer', 'crit_cart') ;
function crit_cart() {
if(sizeof( WC()->cart->get_cart() ) > 0 ) : ?>
<script type='text/javascript'>
jQuery('#fancy_icon-62-12').css('color','blue');
</script>
<?php
endif;
}
在此示例中,您逃避了PHP,并将直接将脚本回显到DOM。由于它是一个脚本,因此它将在那时运行。
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句