Search
 
SCRIPT & CODE EXAMPLE
 

PHP

WooCommerce shop loop random array function not same values after each other

function random_result() {
    // Input
    $input = array( '<img src="/wp-content/uploads/2022/01/banner-001.svg">', '<img src="/wp-content/uploads/2022/01/banner-002.svg">', '<img src="/wp-content/uploads/2022/01/banner-003.svg">' );

    // NOT isset (result) OR NOT isset (counter)
    if ( ! isset( $GLOBALS['result'] ) || ! isset( $GLOBALS['counter'] ) ) {
        // Shuffle an array
        shuffle( $input );

        // Globals
        $GLOBALS['result'] = $input;
        $GLOBALS['counter'] = 0;
    } else {
        // Plus 1
        $GLOBALS['counter'] += 1;
    }

    // Store
    $counter = $GLOBALS['counter'];

    // Limit is reached, reset
    if ( $counter == ( count( $input ) - 1 ) ) {
        unset( $GLOBALS['counter'] );
    }

    return $GLOBALS['result'][$counter];
}

function action_woocommerce_shop_loop() {
    // Variables
    global $wp_query;
    
    // Add content every X product
    if ( (  $wp_query->current_post % 7 ) ==  0 && $wp_query->current_post !=  0 ) {
        // Call function, display result
        echo random_result();
    }
}
add_action( 'woocommerce_shop_loop', 'action_woocommerce_shop_loop' );
Comment

PREVIOUS NEXT
Code Example
Php :: php mail merge docx document 
Php :: php javascript dynamic form 
Php :: Custom search form 
Php :: permission for multisite in wp-config.php file 
Php :: Comment définir un délimiteur de fil d’Ariane personnalisé dans WooCommerce 
Php :: yii2 change transport 
Php :: search line phpstorm mac 
Php :: ezSql PDO Connection 
Php :: php normalize whitespace characters 
Php :: PHP Dependency Resolver 
Php :: how to set default php version in ubuntu 
Php :: slim disable display error details 
Php :: Include Custom Post Types Categories to the main query 
Php :: membership_registration: city or town 
Php :: file_get_contents max_execution_time 
Php :: ob_start store variable in php 
Php :: json encode unset array 
Php :: inject multiple logger symfony 
Php :: acho in php 
Php :: repalce 0 in phone with 234 
Php :: file handling x+ in php example 
Php :: The app function returns the service container instancel 
Php :: integracao de webservice no php usando soap 
Php :: wcfm filter vendor dashboard widgets 
Php :: blocking youtube adds in php 
Php :: php mysqli connect to two databases pdo 
Php :: how to add user profile image in my account page in woocommerce 
Php :: How to remove repetitive values from foreach loop in php laravel 
Php :: TypeError: Argument 1 passed to DrupalCoreEntityEntityViewBuilder::view() must implement interface 
Php :: storefront header cart 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =