我的主要php文件free-shipping-badge.php
/** REGISTER STYLES **/
add_action( 'wp_enqueue_scripts', 'fsb_stylesheet' );
function fsb_stylesheet() {
wp_register_style( 'view-style', plugins_url('/view/fsb_badge_style.php', __FILE__) );
wp_enqueue_style( 'view-style' );
}
/** ADD FREE SHIPPING BADGE SUFFIX AFTER PRICE **/
add_filter( 'woocommerce_get_price_suffix', 'fsb_suffix', 99, 4 );
function fsb_suffix( $html, $product, $price, $qty ){
$fsb_price = (float) $product->get_price(); // Regular price
$fsb_limit_price = esc_attr( get_option('fsb_limit_price_option') ); // Limit price
$fsb_badge_text = esc_attr( get_option('fsb_badge_text_option') ); // Text to display on badge
$fsb_badge_color = esc_attr( get_option('fsb_badge_color_option') ); // Text to display on badge
if($fsb_price > $fsb_limit_price){
$html .='</br>'.'<fsb_badge class="fsb_badge_view">' .$fsb_badge_text.' '.'</fsb_badge>';
return $html;
}
}
我的样式文件:fsb_badge_style.php。背景未从php变量$ fsb_badge_color获取值,这是主要问题。
<?php
header('Content-type: text/css');
include('free-shipping-badge.php');
?>
.fsb_badge_view{
display: inline;
padding: .3em .6em .3em;
font-family: Arial;
font-size: 10.5px;
font-weight: bold;
line-height: 1.5;
color: #fff;
text-align: center;
white-space: middle;
vertical-align: baseline;
border-radius: .25em;
background-color: <?php echo $fsb_badge_color; ?>;
}
正如@Tony Djukic所说,您不能将PHP文件作为样式表排队,它不会处理PHP。虽然可能在标题中内联输出样式。一个例子:
function fsb_badge_style()
{
echo "<style type=\"text/css\">.fsb_badge_view{background-color: ".$fsb_badge_color.";}</style>";
}
add_action('wp_head', 'fsb_badge_style', 100);
这将直接在标题中写入样式。
<style type="text/css">.fsb_badge_view{background-color: #e7e7e7;}</style>
作为额外的好处,这也将对可能未启用JavaScript的用户起作用。