dougou3871 2018-08-04 08:06
浏览 83

如何在WordPress中为特定元素或函数执行CSS和JS

I have a WordPress WooCommerce website

I want to execute bootstrap CSS and js for the specific function in my WooCommerce checkout page. My Code is

if( current_user_can('administrator')) {
function display_customers_list() {
// query array
$args = array(
    'role' => 'outlet'
);
$customers = get_users($args);
if( empty($customers) )
return;

echo'<br>';
echo'<h3>Display Customer List</h3>';
echo '<form action="" method="POST">';
echo'<select class="selectpicker" data-show-subtext="true" data-live-search="true" name="select_name[]" title="Select Customer">';
foreach( $customers as $customer ){
    echo '<option value="'.$customer->data->ID.'">'.$customer->data->display_name.'</option>';
}
echo '</select>';
echo '<input type="submit" value="Select Customer">';
echo '</form>'; 


echo'<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">';
echo'<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css" />';
echo'<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>';
echo'<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>';
echo'<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>';

}   
add_filter( 'woocommerce_before_checkout_form', 'display_customers_list' );
}

The problem is the bootstrap CSS and js also affect the other form fields in my WooCommerce checkout page.

I want to only use in my select box of the function. How can I load this css and js only for my select box in the function and it did not affect my other WooCommerce checkout form fields.

Please help me in this regard.

  • 写回答

1条回答 默认 最新

  • duanao4503 2018-08-04 23:18
    关注

    You can’t use bootstrap css or js only for select box or other html element, unless you have specific css and js for that specific element. You can use functions like wp_enque_script() and wp_enque_style to display on that certain page with is_page() condition. But it will definitely affect whole page, not only specific element.

    I’d load the styling you need, then inspect element with new style and copy all styling you see in inspector to new stylesheet, remove bootstrap files and enque new style. Then try adding jquery - basically same way.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c