• I have the following custom shortcode code which I put into my theme functions.php file (I was given this code). It is just to help me get a list of product names with a cost for every product under a specific category. Example: PRODUCT NAME $X.XX. Each product would be on a new line. This is working out fine, but when I use the short code to put this on a page, I put the shortcode UNDER text I want on the page, but the shortcode ALWAYS shows it ABOVE the text rather than UNDER which is where I put the shortcode and expected the out put to be?

    I also found that it doesn’t show the currency symbol, but shows USD instead?

    function product_list_by_category_name( $product_category_name ){
    	$args = array( 'post_type' => 'product', 'product_cat' => $product_category_name );
    
        $loop = new WP_Query( $args );
    
        while ( $loop->have_posts() ) : $loop->the_post();
        global $product; 
    
    	$amount = get_post_meta($product->post->ID, '_price', true) . ' ' . get_woocommerce_currency();
    
    	echo '<a href="'.get_permalink().'">'. get_the_title() .'</a> '. $amount .'<br />';
    
        endwhile; 
    
        wp_reset_query();
    }
    
    /** SHORTCODE **/
    function product_list_by_category_name_shortcode( $atts ) {
    	extract(shortcode_atts(array(
    		'product_category_slug' => ''
    	), $atts));
    
    	return product_list_by_category_name( $product_category_slug );
    }
    
    add_shortcode( 'product_list_by_category', 'product_list_by_category_name_shortcode' );

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

Viewing 1 replies (of 1 total)
  • Thread Starter lochie1979

    (@lochie1979)

    still same issue but latest version of the code is:

    function product_list_by_category_name( $product_category_name ){
            $args = array( 'post_type' => 'product', 'product_cat' => $product_category_name );
    
        $loop = new WP_Query( $args );
    
        while ( $loop->have_posts() ) : $loop->the_post();
        global $product;
    
            $amount = get_post_meta($product->post->ID, '_price', true) . ' ' . get_woocommerce_currency();
           echo '<table width="400" border="0" cellspacing="0" cellpadding="2">';
            echo '<tr><td width="250"><div align="left"><!-- <a href="'.get_permalink().'"> --!>'. get_the_title() .'<!-- </a> --!></div></td><td width="150"><div align="left"> '. $amount .'</div></td></tr>';
     echo '</table>';
        endwhile;
    
        wp_reset_query();
    
    }
    
    /** SHORTCODE **/
    function product_list_by_category_name_shortcode( $atts ) {
            extract(shortcode_atts(array(
                    'product_category_slug' => ''
            ), $atts));
    
            return product_list_by_category_name( $product_category_slug );
    }
    
    add_shortcode( 'product_list_by_category', 'product_list_by_category_name_shortcode' );
    
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Simple custom shortcode problem’ is closed to new replies.