如果項目計數大於4,WordPress將顯示短代碼
問題描述:
I am using a plugin (developed by someone else) which is outputting a shortcode [wof_wheel id=\"1111\"]
. I am using this shortcode on a page.
I am trying to apply conditional logic to the shortcode to only display/run the shortcode IF the number of items in the cart is greater than 4.
I know how to get and then check the cart item count with WC()->cart->get_cart_contents_count()
but not sure if its possible to implement the shortcode display/run logic.
1 2 3 4 5 6 7 8 9 10 11 12 |
function do_shortcode() { $items_count = WC()->cart->get_cart_contents_count(); if ($items_count > 4) { DISPLAY/RUN SHORTCODE } else if ($items_count < 4) { DO NOT DISPLAY/RUN SHORTCODE } } |
Is this type of conditional logic possible with shortcodes?
網友觀點:
What @LoicTheAztec is one good solution but if you want another solution you can use the following:
WordPress have built in function which allow you to remove the default callback function for your shortcode and replaced it with your custom one.
In this case we are going to check if the cart content count is more than 4 then remove the default callback and replace it with ours .
i will consider for example that the page id which you have is 49 here which you should change when using this code to match the page which contain the shortcode.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//Our Check function checkShortCode() { $page = get_post(49); if (WC()->cart) { $items_count = WC()->cart->get_cart_contents_count(); if ($items_count == 4) { //Remove the Default Hook function for this shortcode remove_shortcode(\'wof_wheel\'); //Add custom callback for that short to display whatever message you want add_shortcode(\'wof_wheel\', \'myCustomCallBack\'); } } } add_action(\'wp_loaded\', \'checkShortCode\'); |
now we need to add our custom callback to display whatever message you want:
1 2 3 4 5 |
function myCustomCallBack() { echo \'my shortcode is running\'; } |
the code above is tested and working 100%
###
You can add conditional shortcode by using below code :
1 2 3 4 5 6 7 8 |
$cartcount = WC()->cart->get_cart_contents_count(); if ($cartcount > 4) { echo do_shortcode( \'[wof_wheel id=\\\"1111\\\"]\' ); }else{ //item count is less than 4 } |
Note : In the above question you re-declare the predefined function which is incorrect , do_shortcode()
is predefined function for echo the shortcode in template file.
For more help see this link : Click Here
###
You can embed a shortcode in a custom shortcode with the needed conditional logic:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
add_shortcode( \'my_wheel\', \'custom_conditional_wof_wheel\' ); function custom_conditional_wof_wheel( $atts ){ $atts = shortcode_atts( array( \'id\' => \'\', \'count\' => \'4\', // 4 cart items by default ), $atts, \'my_wheel\' ); // If there is more than 4 items count in cart the shortcode [wof_wheel] is executed if( WC()->cart->get_cart_contents_count() > $atts[\'count\'] ){ $id = $atts[\'id\']; return do_shortcode( \\\"[wof_wheel id=\'$id\']\\\" ); } // Else it display nothing return \'\'; } |
Code goes in function.php file of your active child theme (active theme). tested and works.
(see at the end how I have tested that).
USAGE:
1) For more than 4 items in cart (4 items is set by default in the shortcode):
12 [my_wheel id=\\\"1111\\\"]2) For more than 6 items in cart for example:
12 [my_wheel id=\\\"1111\\\" count=\'6\']
How this has been tested.
As I can\'t test this shortcode that comes from a specific third party plugin, I have created a [my_wheel]
shortcode that will output the ID provided in the shortcode argument id
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
add_shortcode( \'my_wheel\', \'custom_conditional_wof_wheel\' ); function custom_conditional_wof_wheel( $atts ){ $atts = shortcode_atts( array( \'id\' => \'\', \'count\' => 4, ), $atts, \'my_wheel\' ); if( WC()->cart->get_cart_contents_count() > $atts[\'count\'] ){ $id = $atts[\'id\']; return do_shortcode( \\\"[wof_wheel id=\'$id\']\\\" ); } return \'\'; } |
Code goes in function.php file of your active child theme (active theme).
Then I have added [my_wheel id=\\\"1111\\\"]
shortcode in the WordPress text editor of a page and I get this display when the cart items count is 5 or more:
So it works.
1. 帶 [親測] 說明源碼已經被站長親測過!
2. 下載後的源碼請在24小時內刪除,僅供學慣用途!
3. 分享目的僅供大家學習和交流,請不要用於商業用途!
4. 本站資源售價只是贊助,收取費用僅維持本站的日常運營所需!
5. 本站所有資源來源於站長上傳和網路,如有侵權請郵件聯繫站長!
6. 沒帶 [親測] 代表站長時間緊促,站長會保持每天更新 [親測] 源碼 !
7. 盜版ripro用戶購買ripro美化無擔保,若設置不成功/不生效我們不支持退款!
8. 本站提供的源碼、模板、插件等等其他資源,都不包含技術服務請大家諒解!
9. 如果你也有好源碼或者教程,可以到審核區發布,分享有金幣獎勵和額外收入!
10.如果您購買了某個產品,而我們還沒來得及更新,請聯繫站長或留言催更,謝謝理解 !
GG資源網 » 如果項目計數大於4,WordPress將顯示短代碼