0

Inside a php codeblock I have 3 buttons:

<button id='1' onclick="myFunction('string1')">1</button>
<button id='2' onclick="myFunction('string2')">2</button>
<button id='3' onclick="myFunction('string3')">3</button>

and I want to store the value passed by parameter to myFunction() and use them conditionally here (the values passed by parameter are static):

$args = array(
            'post_type' => 'product',
            'product_cat' => $cat, //this variable will hold the value when the user clicks a button.
            'posts_per_page' => 24
            );

My question is how to implement the myFunction() function, and where.

The whole thing is here, I know it is wrong, but I don't know how to make it work.

<?php
function setCat($cat) { return $cat; }
?>
<button id='online' onClick="setCat('<?php $cat = 'online-training'?>')">Online</button>
<button id='kits'  onClick="setCat('<?php $cat = 'kits'?>')">Kits</button>
<button id='ttt'  onClick="setCat('<?php $cat = 'trainer'?>')">TTT</button>



<ul class="products">
    <?php
        $args = array(
            'post_type' => 'product',
            'product_cat' => $cat,
            'posts_per_page' => 24
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
            echo '<li>' . get_the_title() . '</li>';
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>
</ul>
E M
  • 11
  • 7

0 Answers0