Search code examples
phpwordpresswoocommerceshortcodeproduct

Create a shortcode displaying simple products names list in Woocommerce


Does anyone have a function to create a shortcode to list all products names? Don't need images, links, descriptions, sorting — just a simple template like.

<ul>
<li>ProductName1</li>
<li>ProductName2</li>
...
<li>ProductNameN</li>
</ul>

Solution

  • Try this simple shortcode that will output a product list based on a WP_Query:

    function get_custom_product_list() {
    
        // The WP_Query
        $query = new WP_Query( array(
            'posts_per_page' => -1,
            'post_type' => 'product',
            'post_status' => 'publish',
            'hide_empty' => 0,
            'orderby' => 'title',
        ) );
    
        $output = '<ul>';
    
        while ( $query->have_posts() ) : $query->the_post();
            $output .= '<li>' . $query->post->post_title . '</li>';
        endwhile;
        wp_reset_postdata();
    
        return $output.'</ul>'; 
    } 
    add_shortcode( 'product_list', 'get_custom_product_list' );
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    Usage: [product_list]