最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Use CSS tag inside PHP code

programmeradmin1浏览0评论

I'm trying to show/hide two lines of text depending on whether a WooCommerce category is empty or has any products in it. Those lines are inserted in text blocks using Elementor and I've assigned a CSS #ID to each one, so I can control it.

Now, in a custom functions plugin I have, I want to add a function to control what phrase to hide using a css rule of the type: display:none;. The code I have now is:

function show_outlet_msg($content){
    $term = get_term( 40, 'product_cat' );
    $total_products=$term->count;
    if ($total_products>0){
        //Have a products 
    } else {
        //No products
    }
}

add_filter('astra_entry_content_before','show_outlet_msg');

Now I would like to apply that if there are products in Outlet, the text that there are no products in Outlet should be hidden:

#txt_outlet_zero{
display: none;
}

And in case there are no products, hide the one that says there are:

#txt_outlet_head{
display: none;
}

How can I apply these CSS rules from the php code?

I'm trying to show/hide two lines of text depending on whether a WooCommerce category is empty or has any products in it. Those lines are inserted in text blocks using Elementor and I've assigned a CSS #ID to each one, so I can control it.

Now, in a custom functions plugin I have, I want to add a function to control what phrase to hide using a css rule of the type: display:none;. The code I have now is:

function show_outlet_msg($content){
    $term = get_term( 40, 'product_cat' );
    $total_products=$term->count;
    if ($total_products>0){
        //Have a products 
    } else {
        //No products
    }
}

add_filter('astra_entry_content_before','show_outlet_msg');

Now I would like to apply that if there are products in Outlet, the text that there are no products in Outlet should be hidden:

#txt_outlet_zero{
display: none;
}

And in case there are no products, hide the one that says there are:

#txt_outlet_head{
display: none;
}

How can I apply these CSS rules from the php code?

Share Improve this question edited Feb 10, 2021 at 19:36 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 10, 2021 at 18:19 lk2_89lk2_89 1011 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

I'm not sure if you can add styles in filters. I tested the below code in wp_head action and worked perfectly:

function show_outlet_msg(){
    $term = get_term( 40, 'product_cat' );
    $total_products=$term->count;
    if ($total_products>0){
        $html = '<style> #txt_outlet_zero{ display: none; } </style>';
    } else {
        $html = '<style> #txt_outlet_head{ display: none; } </style>';
    }
    echo $html;
}

add_action('wp_head','show_outlet_msg');

You can hook to wp_enqueue_scripts and use wp_add_inline_style():

add_action( 'wp_enqueue_scripts', 'wpse383179_inline_style' );
function wpse383179_inline_style() {
    $term = get_term( 40, 'product_cat' );
    $total_products=$term->count;
    if ($total_products>0){
        $css = '#txt_outlet_zero { display: none; }';
    } else {
        $css = '#txt_outlet_head { display: none; }';
    }
    wp_add_inline_style( '{your-css-id}', $css );
}

The tricky part is determining what your-css-id should be. You can view the source of a given page and check the <link rel="stylesheet" ...> tags. The CSS ID for each of them (assuming they've been properly registered and enqueued for WordPress) will be in the id attribute, with -css appended to it. So, if you see, eg,

<link rel="stylesheet" id="my-styles-css" src="/path/to/stylesheet.css" />

... then the {your-css-id} bit in my code snippet should be my-styles.

However

That said, is there a reason to generate two different CSS IDs for this? There's never going to be a situation where the $total_products is 0 and greater than 0. There may be an easier way, eg, adding a single <div> and changing the content inside it based on the product count.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>