• Resolved steve49589

    (@steve49589)


    Wondering if there is a way I could display a list of all the products in a specific category via shortcode?

    The shortcodes I have found seem to display too much info. JUST want the title, kind of like a bulleted list would be perfect, that links to the product page.

    I need to display all of the products in a specified category.

    Use Case: Going to display a list of classes. There are five different classes, and I’m going to create a category for each of the classes. The classes are taught a few times per year. I have a regular page describing the classes, and I want to “list” the next classes coming up.

    So this bullet list of classes will be a maximum of three or four “products.”

    Thank you.

    https://wordpress.org/plugins/woocommerce/

Viewing 15 replies - 1 through 15 (of 19 total)
  • hommealone

    (@hommealone)

    Hi steve49589,

    Just wondering if you ever came up with a solution for this? I’m looking for the same thing…

    Thread Starter steve49589

    (@steve49589)

    Nope … I’m having to update them manually so far. Never heard back from WooCommerce in this thread as you can see.

    hommealone

    (@hommealone)

    Too bad. 🙁

    If I find anything, I’ll let you know. I’m considering outputting a product loop there using a shortcode, and hiding everything but the titles (w/ permalinks) with CSS. A lot of wasted overhead, but I really don’t want to have to manually maintain the list…

    WooCommerce shortcodes:
    https://docs.woothemes.com/document/woocommerce-shortcodes/

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Our shortcodes use the grids. If you need custom output you could make your own, combining

    https://codex.wordpress.org/Template_Tags/get_posts
    https://codex.wordpress.org/Function_Reference/add_shortcode

    Thread Starter steve49589

    (@steve49589)

    Mike,
    Yes, thank you for the information. I figured it would be possible to do something custom, but those links provide information and coding that is beyond my skill set.

    I understand this is beyond the scope of free support, but maybe someone will see this thread and provide detailed instructions on how to do this.

    Thank you.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Mike,

    Thanks for pointing me in the right direction. Let me know what you think of this. It’s working well for me. It might make a nice addition to WooCommerce built in shortcodes?

    Steve,

    Try this and see if it works for you. You can paste this into your functions.php file. Shortcode usage and parameter options are shown in the comments of the code.

    With this in your functions.php file, you could just insert the shortcode wherever you want to output the list.

    Basic usage would be something like:

    [my_product_titles number_of_posts="10" category="tattoo-supplies" orderby="date"]

    All parameters are optional.

    Paste the following into your functions.php file:

    /*
    *
    * Shortcode to add a list of hyperlinked products from a category, listed by title only.
    * Outputs an unordered list.
    * Used wherever you can use a shortcode, such as pages or posts.
    * See: https://wordpress.org/support/topic/display-list-of-products-title-only-in-a-category-via-shortcode
    *
    * Format is: [my_product_titles]
    *
    * Parameter options are: [my_product_titles number_of_posts="-1" category="tattoo-supplies" orderby="date"]
    *
    * number_of_posts defaults to -1 (show all products); value is an integer. Ex: [my_product_titles number_of_posts="10"]
    * category defaults to '' (empty value; shows all categories); value is a category slug. Ex: [my_product_titles category="tattoo-supplies"]
    * orderby defaults to 'title' (alphabetical); Ex: [my_product_titles category="tattoo-supplies" orderby="date"];
    *   For orderby options see: https://codex.wordpress.org/Template_Tags/get_posts
    *
    **/
    
    function my_woocommerce_titles_list($atts) {
    	extract(shortcode_atts(array(
    		'number_of_posts'	=> -1,
    		'category'		=> '',
    		'orderby' 		=> 'title'
    		), $atts));
    
    	$return_string = '<ul class="product-titles-list">'.PHP_EOL;
    
    	global $post;
    	$args = array(
    		'posts_per_page'		=> $number_of_posts,
    		'offset'           		=> 0,
    		'post_status'      		=> 'publish',
    		'post_type'      		=> 'product',
    		'product_cat' 			=> $category,
    		'order'				=> 'ASC',
    		'orderby' 			=> $orderby
    	);
    	$postslist = get_posts( $args );
    	foreach ( $postslist as $post ) :
    		setup_postdata( $post );
    		$my_permalink = get_the_permalink();
    		$my_title = get_the_title();
    		$return_string .= '<li><a href="' . $my_permalink . '">' . $my_title . '</a></li>'.PHP_EOL;
    
    	endforeach;
    	wp_reset_postdata();
    
    	$return_string .= '</ul>'.PHP_EOL;
    
    	return $return_string;
    
    };
    
    function my_register_shortcodes(){
       add_shortcode('my_product_titles', 'my_woocommerce_titles_list');
    }
    
    add_action( 'init', 'my_register_shortcodes');

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Looks fine. I don’t think we need such a shortcode in core, since it’s more niche, but feel free to share on your blog etc so its more discoverable 🙂

    I’d love to see this as a shortcode as well. Currently our client is manually listing the product titles and prices as a text widget on the description page. When they update the product price, they have to remember to update the widget as well. Would be great to update just the product.

    hommealone, thanks for posting this. I’ll try adding this to the clients site. Need to add the price field as well, but I’m sure I can figure that out.

    great solution this helped me Thanks, and how can add class to selected product title?

    @hommealone this was exactly the solution I was looking for! Thank you!

    Thread Starter steve49589

    (@steve49589)

    Incredible. Thank you so much for this code.

    Just a quick addition to the code above that will display a message if no products are found in a particular category: (I am NOT a PHP guru, so there may be ways to optimize this, but it works for me)

    functions.php file:

    /*
    *
    * Shortcode to add a list of hyperlinked WooCommerce products from a category, listed by title only.
    * Outputs an unordered list.  Displays user defined message if no products are listed in said category.
    * Used wherever you can use a shortcode, such as pages or posts.
    * See: https://wordpress.org/support/topic/display-list-of-products-title-only-in-a-category-via-shortcode
    *
    * Format is: [my_product_titles]
    *
    * Parameter options are: [my_product_titles number_of_posts="-1" category="tattoo-supplies" orderby="date"]
    *
    * number_of_posts defaults to -1 (show all products); value is an integer. Ex: [my_product_titles number_of_posts="10"]
    * category defaults to '' (empty value; shows all categories); value is a category slug. Ex: [my_product_titles category="tattoo-supplies"]
    * orderby defaults to 'title' (alphabetical); Ex: [my_product_titles category="tattoo-supplies" orderby="date"];
    *   For orderby options see: https://codex.wordpress.org/Template_Tags/get_posts
    *
    **/
    
    function my_woocommerce_titles_list($atts) {
    	extract(shortcode_atts(array(
    		'number_of_posts'	=> -1,
    		'category'		=> '',
    		'orderby' 		=> 'title'
    		), $atts));
    	$return_string = '<ul class="product-titles-list">'.PHP_EOL;
    
    	global $post;
    	$args = array(
    		'posts_per_page'		=> $number_of_posts,
    		'offset'           		=> 0,
    		'post_status'      		=> 'publish',
    		'post_type'      		=> 'product',
    		'product_cat' 			=> $category,
    		'order'				=> 'ASC',
    		'orderby' 			=> $orderby
    	);
    	$postslist = get_posts( $args );
    	if(!$postslist) : 
    		$return_string2 = '<span class="noproducts">NO PRODUCTS TO LIST RIGHT NOW</span>'.PHP_EOL;
    		return $return_string2;
    	endif;
    	foreach ( $postslist as $post ) :
    		setup_postdata( $post );
    		$my_permalink = get_the_permalink();
    		$my_title = get_the_title();
    		$return_string .= '<li><a href="' . $my_permalink . '">' . $my_title . '</a></li>'.PHP_EOL;
    	endforeach;
    	
    	wp_reset_postdata();
    
    	$return_string .= '</ul>'.PHP_EOL;
    
    	return $return_string;
    	
    };
    
    function my_register_shortcodes(){
       add_shortcode('my_product_titles', 'my_woocommerce_titles_list');
    }
    
    add_action( 'init', 'my_register_shortcodes');
    add_filter('user_contactmethods', 'modify_contact_methods');

    Same basic usage:
    [my_product_titles number_of_posts="10" category="tattoo-supplies" orderby="date"]

    I am looking to list products by title name, no images, no descriptions. Please see the below screenshot, and suggestions would be helpful;

    Product List Screenshot

    Thread Starter steve49589

    (@steve49589)

    @exeplore – You’ll want to start your own thread as this thread describes listing ONLY the title of the product in a bulleted list. Your screen shot is much more involved.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Display list of products (title only) in a category via shortcode’ is closed to new replies.