Displaying Product List for particular Blogs

Hi, I am just wondering is there a way to display the products on the main site using a shortcode for a particular blog, e.g. Blog 34 only?

Thanks.

  • DavidM
    • DEV MAN’s Mascot

    Hi Fondeli,

    There's not a shorcode for that at this point, but I thought it was a great idea myself and since I had a moment, I went and threw the following shortcode together that you can simply paste into your theme's functions.php file.

    From there you can simply use the shortcode almost exactly the same as the mp_list_products shortcode, only specifying a blog id as the first argument.

    As an example, the following would produce a default products list:
    [mp_list_products_by_blog blogid="6"]

    function products_by_blog($atts) {
    	extract(shortcode_atts(array(
    		'blogid' => 'blogid',
      		'paginate' => '',
      		'page' => '',
      		'per_page' => '',
      		'order_by' => '',
      		'order' => '',
      		'category' => '',
      		'tag' => ''
    	), $atts));
    
    	switch_to_blog($blogid);
    	if (function_exists('mp_list_products')){
    		$output = mp_list_products(false, $paginate, $page, $per_page, $order_by, $order, $category, $tag);
    	}
    	else {
    		$output = "";
    	}
    	restore_current_blog();
    	return $output;
    }
    add_shortcode('mp_list_products_by_blog', 'products_by_blog');

    Now, I did test this out on a multisite MarketPress install and it worked perfectly well there. But I'm no code guru like the developers here, so I'm going to run this one by them just to be sure I didn't miss anything and to see if anything can be done more efficiently (like the check for the function existence which doesn't appear to work :slight_smile: ).

    Cheers,
    David

  • JM
    • Design Lord, Child of Thor

    Based on DavidM's code (I hope you don't mind), i've created another one that gets blog description and header (because i use this as a logo) from whatever blog you want, and displays it on the main one. I didn't use the shortcode inside backend pages/posts, but inside source code, like this :

    <?php echo do_shortcode('[descrieri_bloguri blogid="2"]'); ?>

    Here's the code :

    $blogs = get_last_updated();
    function all_descriptions($atts) {
    extract(shortcode_atts(array(
    		'blogid' => 'blogid',
      		'paginate' => '',
      		'page' => '',
      		'per_page' => '',
      		'order_by' => '',
      		'order' => '',
      		'category' => '',
      		'tag' => ''
    	), $atts));
    	switch_to_blog($blogid);
    
    	if (function_exists('get_bloginfo')&&function_exists('get_header_image')){
    	$blogs_descriptions = get_bloginfo('description');
    	$blogs_logo = get_header_image();
    	echo "<li><p><img src=\"$blogs_logo\" /></p><p>$blogs_descriptions</p></li>";
    	}
    	else {
    		$blogs_descriptions  = "";
    	}
    
    	restore_current_blog();
    	//return $blogs_descriptions;
    }
    add_shortcode('descrieri_bloguri', 'all_descriptions');

    Cheers,
    JM

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.