在 WooCommerce 商店存档循环中显示随机自定义内容
Display random custom content in WooCommerce shop archive loop
我创建了以下代码以在商店存档循环中显示自定义内容。这是一种很好的方式,但我在最后一步遇到了问题。我想让它更有活力。因此我希望输出始终是随机计算的而不是硬编码的。
在这个例子中,我有 3 个输入变量,目前我只能在循环行中输出相同的图像。
在每 3 个产品之后应显示自定义图像内容,但并不总是相同。我现在尝试让它充满活力。意思是,对于每个输出 select 一个随机输入。我该怎么做?
// Adding custom content block to shop loop row
add_action( 'woocommerce_shop_loop', 'add_custom_content_to_shop_loop_row' );
function add_custom_content_to_shop_loop_row() {
// Variables
global $wp_query;
// Custom input options
$input1 = '<img src="/wp-content/uploads/2022/01/banner-test-01.svg">' ;
$input2 = '<img src="/wp-content/uploads/2022/01/banner-test-02.svg">' ;
$input3 = '<img src="/wp-content/uploads/2022/01/banner-test-03.svg">' ;
// Column count
$columns = esc_attr( wc_get_loop_prop( 'columns' ) );
// Add content every X product
if ( ( $wp_query->current_post % 3 ) == 0 && $wp_query->current_post != 0 ) {
// output random input
echo $input1 ;
}
}
我知道,我必须为此使用数组。但是我尝试了所有来自这里 Get random item from array 的信息,但它不适用于 html 回显输出。
$input = array("$input1 = '<img src="/wp-content/uploads/2022/01/banner-test-01.svg">'", "$input2 = '<img src="/wp-content/uploads/2022/01/banner-test-02.svg">'", "$input3 = '<img src="/wp-content/uploads/2022/01/banner-test-03.svg">'");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
您的代码中存在一些错误,此处修复
$input = array("<img src='/wp-content/uploads/2022/01/banner-test-01.svg'>", "
<img src='/wp-content/uploads/2022/01/banner-test-02.svg'>", "<img src='/wp-
content/uploads/2022/01/banner-test-03.svg'>");
$rand_keys = array_rand($input, 1);
echo $input[$rand_keys] . "\n";
我创建了以下代码以在商店存档循环中显示自定义内容。这是一种很好的方式,但我在最后一步遇到了问题。我想让它更有活力。因此我希望输出始终是随机计算的而不是硬编码的。
在这个例子中,我有 3 个输入变量,目前我只能在循环行中输出相同的图像。
在每 3 个产品之后应显示自定义图像内容,但并不总是相同。我现在尝试让它充满活力。意思是,对于每个输出 select 一个随机输入。我该怎么做?
// Adding custom content block to shop loop row
add_action( 'woocommerce_shop_loop', 'add_custom_content_to_shop_loop_row' );
function add_custom_content_to_shop_loop_row() {
// Variables
global $wp_query;
// Custom input options
$input1 = '<img src="/wp-content/uploads/2022/01/banner-test-01.svg">' ;
$input2 = '<img src="/wp-content/uploads/2022/01/banner-test-02.svg">' ;
$input3 = '<img src="/wp-content/uploads/2022/01/banner-test-03.svg">' ;
// Column count
$columns = esc_attr( wc_get_loop_prop( 'columns' ) );
// Add content every X product
if ( ( $wp_query->current_post % 3 ) == 0 && $wp_query->current_post != 0 ) {
// output random input
echo $input1 ;
}
}
我知道,我必须为此使用数组。但是我尝试了所有来自这里 Get random item from array 的信息,但它不适用于 html 回显输出。
$input = array("$input1 = '<img src="/wp-content/uploads/2022/01/banner-test-01.svg">'", "$input2 = '<img src="/wp-content/uploads/2022/01/banner-test-02.svg">'", "$input3 = '<img src="/wp-content/uploads/2022/01/banner-test-03.svg">'");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
您的代码中存在一些错误,此处修复
$input = array("<img src='/wp-content/uploads/2022/01/banner-test-01.svg'>", "
<img src='/wp-content/uploads/2022/01/banner-test-02.svg'>", "<img src='/wp-
content/uploads/2022/01/banner-test-03.svg'>");
$rand_keys = array_rand($input, 1);
echo $input[$rand_keys] . "\n";