• Resolved hebhansen

    (@hebhansen)


    Hey

    I Can’t figure out the following:

    1) Using product_categories i want them to display in random order. Short code I use:

    [product_categories limit="3" columns="3" parent="689" orderby="rand" title="hide"]

    2) I am displaying “Brands” with image logo that always feature the company name, hence, I don’t need a title for the category. In fact I don’t want the title shown. How can I do that? As seen above title=”hide” or “none” will not do the trick.

    Thx in advance ??

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 16 total)
  • Thread Starter hebhansen

    (@hebhansen)

    I found this code: random_product_categories

    and I need a bit of help. random_product_categories does not respect the limit value. I need it set as a variable in my SC and not just hardcoded in the Snippet. Who has this capacity?

    /**
     * Create new WooCommerce shortcode [random_product_categories]
     * based on [product_categories] (/wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php)
     * to output product categories randomically.
     *
     * This new shortcode uses all attributes used by [product_categories], except 'orderby' and 'order', of course.
     * See more in //docs.woothemes.com/document/woocommerce-shortcodes/ > Product Categories Section.
     */
    function random_product_categories_shortcode($atts){
    	global $woocommerce_loop;
    
    	$atts = shortcode_atts( array(
    		'number'     => null,
    		'columns'    => '4',
    		'hide_empty' => 1,
    		'parent'     => '',
    		'ids'        => ''
    	), $atts);
    
    	if(isset($atts['ids'])){
    		$ids = explode(',', $atts['ids']);
    		$ids = array_map('trim', $ids);
    	} 
    	else{
    		$ids = array();
    	}
    
    	$hide_empty = ($atts['hide_empty'] == true || $atts['hide_empty'] == 1) ? 1 : 0;
    
    	// get terms and workaround WP bug with parents/pad counts
    	$args = array(
    		'hide_empty' => $hide_empty,
    		'include'    => $ids,
    		'pad_counts' => true,
    		'child_of'   => $atts['parent']
    	);
    
    	$product_categories = get_terms('product_cat', $args);
    
    	/**
    	 * PHP shuffle function will shuffle the array of objects $product_categories
    	 */
    	shuffle($product_categories);
    
    	if ('' !== $atts['parent']){
    		$product_categories = wp_list_filter($product_categories, array('parent' => $atts['parent']));
    	}
    
    	if ($hide_empty){
    		foreach($product_categories as $key => $category){
    			if ($category->count == 0){
    				unset($product_categories[$key]);
    			}
    		}
    	}
    
    	if($atts['number']){
    		$product_categories = array_slice($product_categories, 0, $atts['number']);
    	}
    
    	$columns = absint($atts['columns']);
    	$woocommerce_loop['columns'] = $columns;
    
    	ob_start();
    
    	// Reset loop/columns globals when starting a new loop
    	$woocommerce_loop['loop'] = $woocommerce_loop['column'] = '';
    
    	if($product_categories){
    
    		woocommerce_product_loop_start();
    
    		foreach($product_categories as $category){
    			wc_get_template('content-product_cat.php', 
    				array(
    					'category' => $category
    				)
    			);
    		}
    
    		woocommerce_product_loop_end();
    
    	}
    
    	woocommerce_reset_loop();
    
    	return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
    }
    add_shortcode('random_product_categories', 'random_product_categories_shortcode');
    
    
    Saif

    (@babylon1999)

    Hello @hebhansen,

    You don’t need to edit the original template for this, we can write a few CSS lines to take care of it.

    Can you please attach a screenshot of that duplicate category name and highlight it if possible?

    Also, please make sure to include the URL of the page itself.

    Look forward to hearing back from you.

    Thread Starter hebhansen

    (@hebhansen)

    Hey Saif

    That sounds cool. Link to my site: [ redundant link deleted ]

    Scrolling down a bit will take you to

    Brands

    3 logos display Alpha Industries, Brandit and HBH. As you can see all logos displayed will include the name of the brand, so the categories name = Brand Name should not display.

    Note: when I activate the code above they display random, but all categories are shown. In my case 8 brands, so I need to be able to set a limit. In my case 3 or 4.

    Thread Starter hebhansen

    (@hebhansen)

    Update:

    Attribute number=”x” limits the display to that number, hence, it replaces the limit attribute from products shortcode.

    Attribute Columns work the same as for products.

    I still have no solution to removing the category name for this display.

    • This reply was modified 2 years ago by hebhansen.
    Igor H

    (@ihereira)

    Hi,

    I still have no solution to removing the category name for this display.

    Does this CSS code work for you?

    
    .woocommerce ul.products li.product .woocommerce-loop-category__title, .woocommerce ul.products li.product .woocommerce-loop-product__title, .woocommerce ul.products li.product h3
    {
        visibility: hidden;
    }
    


    Link to image: https://snipboard.io/7iRSDZ.jpg

    To add the code, navigate to WP Dashboard > Appearance > Customize > Additional CSS.

    I hope this helps.

    • This reply was modified 2 years ago by Igor H.
    Thread Starter hebhansen

    (@hebhansen)

    Hey Igor

    Code removes all titles, but also inside shop and that’s not going to work. Maybe if I add a class in shortcode for the front display. I will try that out….

    anastas10s

    (@anastas10s)

    Hi there @hebhansen ??

    Code removes all titles, but also inside shop and that’s not going to work. Maybe if I add a class in shortcode for the front display. I will try that out….

    Sounds good. Let us know how that goes!

    Thread Starter hebhansen

    (@hebhansen)

    Not working, removes everything:

    .woocommerce ul.products li.product .woocommerce-loop-category__title,
    .woocommerce ul.products li.product .woocommerce-loop-product__title,
    .woocommerce ul.products li.product h3,
    .remove-title {
        visibility: hidden;
    }
    Igor H

    (@ihereira)

    Hi,
    Thanks for responding. It looks like I hit submit too fast, my apologies.

    
    .woocommerce-loop-category__title
    {
        visibility: hidden;
    }
    

    Is this code working for you? Please let me know how that goes.

    • This reply was modified 2 years ago by Igor H.
    Thread Starter hebhansen

    (@hebhansen)

    Hey Igor

    It does not remove title from product, but it does remove ALL titles from ALL categories in woo.

    I don’t want titles removed inside shop. Just on the frontpage. The display I call by shortcode in widget. The screendump you sent before.

    Shortcode:
    [random_product_categories class="remove-title" number="3" columns="3" parent="689" title="hide"]

    Your css with my custom class does not work either:

    .woocommerce-loop-category__title .remove-title {
        visibility: hidden;
    }
    • This reply was modified 2 years ago by hebhansen.
    Thread Starter hebhansen

    (@hebhansen)

    Random function in post #2 works well on computer but not on mobile. Does anyone know why? Hint all random function on front does not work on mobile.

    – Top Secret section
    – Brands Section
    – Testimonial Section

    Can someone give me a lead to this….

    Thread Starter hebhansen

    (@hebhansen)

    Someone pointed me here for a fix to remove category name on front. However, I am not good with php, so I have no idea if it’s possible:

    <?php
    /**
     * The template for displaying product category thumbnails within loops.
     *
     * Override this template by copying it to yourtheme/woocommerce/content-product_cat.php
     *
     * @author 		WooThemes
     * @package 	WooCommerce/Templates
     * @version     2.4.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    global $woocommerce_loop;
    
    // Store loop count we're currently on
    if ( empty( $woocommerce_loop['loop'] ) ) {
    	$woocommerce_loop['loop'] = 0;
    }
    
    // Store column count for displaying the grid
    if ( empty( $woocommerce_loop['columns'] ) ) {
    	$woocommerce_loop['columns'] = apply_filters( 'loop_shop_columns', 4 );
    }
    
    // Increase loop count
    $woocommerce_loop['loop'] ++;
    
    ?>
    
    <li <?php wc_product_cat_class(); ?>>
    	<?php do_action( 'woocommerce_before_subcategory', $category ); ?>
    
    	<a href="<?php echo get_term_link( $category->slug, 'product_cat' ); ?>">
    
    		<?php
    			/**
    			 * woocommerce_before_subcategory_title hook
    			 *
    			 * @hooked woocommerce_subcategory_thumbnail - 10
    			 */
    			do_action( 'woocommerce_before_subcategory_title', $category );
    		?>
    
    		<h3>
    			<?php
    				echo $category->name;
    
    				//if ( $category->count > 0 )
    				//	echo apply_filters( 'woocommerce_subcategory_count_html', ' <mark class="count">(' . $category->count . ')</mark>', $category );
    			?>
    		</h3>
    
    		<?php
    			/**
    			 * woocommerce_after_subcategory_title hook
    			 */
    			do_action( 'woocommerce_after_subcategory_title', $category );
    		?>
    
    	</a>
    
    	<?php 
    		echo $category->description;
    		//get subcategories
    		$args = array(
    	       'hierarchical' => 1,
    	       'show_option_none' => '',
    	       'hide_empty' => 0,
    	       'parent' => $category->term_id,
    	       'taxonomy' => 'product_cat'
    	    );
    
    		$subcategories = get_categories($args);
    		//echo subcategory links
    		if(!is_null($subcategories)){
    			echo '<ul class="subcategories">';
    			foreach($subcategories as $subcat){
    				echo '<li><a href="/product-category/' . $subcat->category_nicename . '/">' . $subcat->name . '</a>';
    
    				//get subcategories of the sub-category
    				$args = array(
    			       'hierarchical' => 1,
    			       'show_option_none' => '',
    			       'hide_empty' => 0,
    			       'parent' => $subcat->term_id,
    			       'taxonomy' => 'product_cat'
    			    );
    
    			    $subchildren = get_categories($args);
    			    if(!is_null($subchildren)){
    			    	echo '<ul class="subcat-children">' . "\n";
    			    	foreach($subchildren as $subchild){
    			    		echo '<li><a href="/product-category/' . $subchild->category_nicename . '/">' . $subchild->name . '</a>' ."</li>\n";
    			    	}
    			    	echo '</ul>';
    			    }
    
    				echo "</li>\n";
    			}
    			echo '</ul>';
    		}
    
    		do_action( 'woocommerce_after_subcategory', $category ); 
    
    	?>
    </li>
    anastas10s

    (@anastas10s)

    Hi there @hebhansen ??

    I don’t want titles removed inside shop. Just on the frontpage.

    Thanks for clarifying things further. Know more about hiding the page title on specific pages, using CSS, at the article directly linked here.

    That article will help you adjust the code snippet my colleague shared here, to your needs.

    Random function in post #2 works well on computer but not on mobile. Does anyone know why?

    From what I understand, this is already being discussed on GitHub (direct link here). Therefore, feel free to continue the related correspondence there.

    Someone pointed me here for a fix to remove category name on front. However, I am not good with php, so I have no idea if it’s possible

    Kindly keep in mind we are not developers and only offer support for existing functionality.

    Please see our Support Policy: https://www.woocommerce.com/support-policy/

    For assistance with customization or development with your site, we recommend that you seek help from:

    * A local web developer
    * Codeable.io
    * WooExperts
    * Stackexchange

    If you are comfortable coding yourself and have questions, I would also recommend that you consider:

    * WooCommerce developer Portal
    * WooCommerce Slack Community
    * Advanced WooCommerce Facebook group

    I hope that helps you to figure it out.

    Feel free to get back to us if you have further questions.

    Thread Starter hebhansen

    (@hebhansen)

    Update:

    Above code is written by Filipe. He updated it so that category title can be hidden while enjoying the random shuffle. You can get it at github:

    Random Product Categories w. Remove Title

    Shortcodes for the php:

    [random_product_categories]
    [random_product_categories column=3 numbers=3] – NOTE numbers replace “limit” attribute in the product shortcode.
    [random_product_categories hide_title=1] – Titles under categories will not show. Useful when images are self explanatory, such as brands logo.

    Thread Starter hebhansen

    (@hebhansen)

    Update on the Random obstruction. Turning off: Plugin

    WP-Optimize – Cache, Clean, Compress

    solved this. It’s reported here:

    Random Obstruction Report

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Shotcode Rand Categories & Remove Title’ is closed to new replies.