如何在登录用户看到替代图像的情况下,仅向登录用户显示产品图像?

时间:2017-11-28 15:03:06

标签: wordpress if-statement plugins woocommerce conditional

我想修改我的WordPress / WooCommerce网站,以便我的产品图片具备以下条件:

登录网站访问者:

我想登录访问者,以便能够看到所有的WooCommerce产品图片。

退出网站访问者:

我想退出访问者,看一个替代图片。因此,隐藏产品图像。对于每个产品,此替代图像都是相同的。

1 个答案:

答案 0 :(得分:0)

首先,您需要为WordPress / WooCommerce网站创建Child Theme

创建后,只需将以下代码插入您的子主题中的<?php // Remove Default WooCommerce Product Images/Placeholders. remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 ); // Create conditional logic function. function logged_in_images(){ // If site visitor is Logged in, display Default WooCommerce Product Images/Placeholder. if ( is_user_logged_in() ) { echo woocommerce_show_product_images(); } // If site visitor is not logged in, display the following Image instead. else { echo '<img src=http://vaporcenter.be/theme/Wholesale/img/placeholders/default.jpg?1504084579>'; }; } // We are inserting ('Hooking' into) the above function into where the Default WooCommerce Product Image/Placeholder was, before we removed it. add_action( 'woocommerce_before_single_product_summary', 'logged_in_images', 20 ); ?> 文件中:

wp-config.php

请务必将http://vaporcenter.be/theme/Wholesale/img/placeholders/default.jpg?1504084579替换为所需的图像。

别忘了,您可以从代码中删除评论(// ...)。他们在那里,只是帮助解释正在发生的事情。

我已经测试了上面的代码,它确实有效。

祝你好运!

如果您收到错误

  1. 第一步:使用FTP客户端(如Filezilla)进入根文件夹(安装WordPress的位置)。
  2. 查找名为define('WP_DEBUG', false);的文件并将其拖至您的计算机。
  3. 使用记事本/记事本++打开文件,查找专栏wp-config.php并更改&#39; false&#39;到了&#39; true&#39;。
  4. wp-config.php文件从您获取的位置拖回。
  5. 刷新您遇到问题的页面。然后,您应该会看到一条错误消息,这将有助于解决如何解决任何问题。
  6. 解决问题后,您的网站又恢复正常工作状态,请返回def getPlayers(): print("Hello and Welcome to the Crown and Anchor Game") num = int(input('Please enter the number of people playing today: ')) # takes the number of people who are playing from the user scoreInit = [] # creating an empty list for the players inital score of 10 for i in range(num): # for loop to append the inital score of 10 to the empty list scoerInit for the amount of players input scoreInit += i * [10] return scoreInit # returns the list of inital scores for the amount of players playing def collectBets(balance): bets = {} index = 0 for i in balance: index += 1 print('Player %d, what would you like to do this round?' % (index)) print('1: Bet on a symbol') print('2: Skip this round') userOpt = int(input('Please enter 1 or 2 depending on your choice: ')) # assigning what the user inputs as the variable 'usesrOpt' if userOpt == 1: # if user decides to bet: betTimes = int(input('How many times would you like to bet this round?: ')) for a in range(betTimes): betAmount = int(input('Enter the amount you would like to bet this round: $1, $2, $5, or $10: ')) symbol = int(input('Enter the number corresponding to the symbol you would like to bet on\n' # asking user what symbol they want to bet on - assigning it to a variable '1: Crown\n' '2: Anchor\n' '3: Heart\n' '4: Diamond\n' '5: Club\n' '6: Spade\n' )) bets.update({symbol:betAmount}) print(bets) def main(): balance1 = getPlayers() collectBets(balance1) main() 并更改“真实”字样。回到&#39; false&#39;。