Come mostro l’elenco dei nomi delle categorie di un post di WordPress senza link separate per virgola?

Per fare questo tipo di output utilizziamo le due funzioni native di wordpress senza doverne creare una nuova. La prima funzione è quella che restituisce la lista delle categorie associate ad un post che si chiama get_the_category_list(  ) la seconda è la funzione che elimina tutti i tag html inclusi script js e stili css che si chiama wp_strip_all_tags( ). La formula è la seguente e va inserita nei template file single.php o single-{post_type}.php.

<?php 
    echo wp_strip_all_tags(get_the_category_list(', ')); 
    // risultato esmpio di output: News, Coorporate, Press release
    // al posto della virgola 
?>

Come stampo il nome della prima categoria di un post di WordPress senza il link?

/*
 *
 * studio42_get_the_category_name()
 * funzione che stampa il nome di una categoria di un post
 * @author toni@schiavoneguga.com
 * 
 * questa funzione va inserita nel file functions.php del tema figlio oppure del tema padre
 * funziona solo con le categre native di wordpress
 */

function studio42_get_the_category_name()
{
    $category = get_the_category();
    return $category[0]->cat_name;
}

// va inserita nel file single.php o single-post.php
echo studio42_get_the_category_name();

Come si aggiungono le categorie di un post nelle classi del body su wordpress?

Con questa funzione php è possibile aggiungere in automatico nelle classi del body la lista dei nomi di categoria associate ad un post. Incolla questo codice nel file functions.php nel tema child o tema padre.


  function studio42_add_post_category_names_to_body_class($classes) {
    if (is_single() ) {
      global $post;
      $categories = get_the_category($post->ID);
      foreach( $categories as $category) {
        $classes[] = 'cat-' . $category->category_nicename;
      }
    }
    return $classes;
  }
  add_filter('body_class','studio42_add_post_category_names_to_body_class');

How to remove duplicate lines in Visual Studio Code?

If the order of lines is not important

Sort lines alphabetically, if they aren’t already, and perform these steps:
(based on this related question: How do I find and remove duplicate lines from a file using Regular Expressions?)

  1. Control+F
  2. Toggle “Replace mode”
  3. Toggle “Use Regular Expression” (the icon with the .* symbol)
  4. In the search field, type ^(.*)(\n\1)+$
  5. In the “replace with” field, type $1
  6. Click the Replace All button (“Replace All”).

If the order of lines is important so you can’t sort

In this case, either resort to a solution outside VS Code (see here), or – if your document is not very large and you don’t mind spamming the Replace All button – follow the previous steps, but in steps 4 and 5, enter these:
(based on Remove specific duplicate lines without sorting)

Caution: Blocks for files with too many lines (1000+); may cause VS Code to crash; may introduce blank lines in some cases.

  • search: ((^[^\S$]*?(?=\S)(?:.*)+$)[\S\s]*?)^\2$(?:\n)?
  • replace with: $1

and then click the “Replace All” button as many times as there are duplicate occurrences.

You’ll know it’s enough when the line count stops decreasing when you click the button. Navigate to the last line of the document to keep an eye on that.

From

https://stackoverflow.com/questions/37992493/how-to-remove-duplicate-lines-in-visual-studio-code

Funzione php per prendere il frame in jpg di un video pubblico su vimeo

function pickup_vimeo_thumbnail_url($vimeo_video_url){
    if(!$vimeo_video_url) return false;
    $json_data = json_decode(file_get_contents('http://vimeo.com/api/oembed.json?url=' . $vimeo_video_url));
    if(!$json_data) return false;
    return $json_data->thumbnail_url;
}

Stampare il prezzo formattato prendendolo dal custom field di woocommerce


// reperisce il prezzo non formattato dal custom field assegnandola alla variabile $prezzo
$prezzo = get_post_meta( get_the_ID(), '_regular_price', true); 

// Stampa il prezzo formattato
echo wc_price( $prezzo ); 

Come reperire i dati di un prodotto utilizzando le funzioni standard di Woocommerce?

// Get $product object from product ID
 
$product = wc_get_product( $product_id );

// Get Product ID
 
$product->get_id(); (fixes the error: "Notice: id was called incorrectly. Product properties should not be accessed directly")
 
// Get Product General Info
 
$product->get_type();
$product->get_name();
$product->get_slug();
$product->get_date_created();
$product->get_date_modified();
$product->get_status();
$product->get_featured();
$product->get_catalog_visibility();
$product->get_description();
$product->get_short_description();
$product->get_sku();
$product->get_menu_order();
$product->get_virtual();
get_permalink( $product->get_id() );
 
// Get Product Prices
 
$product->get_price();
$product->get_regular_price();
$product->get_sale_price();
$product->get_date_on_sale_from();
$product->get_date_on_sale_to();
$product->get_total_sales();
 
// Get Product Tax, Shipping & Stock
 
$product->get_tax_status();
$product->get_tax_class();
$product->get_manage_stock();
$product->get_stock_quantity();
$product->get_stock_status();
$product->get_backorders();
$product->get_sold_individually();
$product->get_purchase_note();
$product->get_shipping_class_id();
 
// Get Product Dimensions
 
$product->get_weight();
$product->get_length();
$product->get_width();
$product->get_height();
$product->get_dimensions();
 
// Get Linked Products
 
$product->get_upsell_ids();
$product->get_cross_sell_ids();
$product->get_parent_id();
 
// Get Product Variations
 
$product->get_attributes();
$product->get_default_attributes();
 
// Get Product Taxonomies
 
$product->get_categories();
$product->get_category_ids();
$product->get_tag_ids();
 
// Get Product Downloads
 
$product->get_downloads();
$product->get_download_expiry();
$product->get_downloadable();
$product->get_download_limit();
 
// Get Product Images
 
$product->get_image_id();
$product->get_image();
$product->get_gallery_image_ids();
 
// Get Product Reviews
 
$product->get_reviews_allowed();
$product->get_rating_counts();
$product->get_average_rating();
$product->get_review_count();

Come reperire i dati di un ordine usando funzioni standard di Woocommerce?

Elendo delle funzione di Woocommerce per reperire i dati di un ordine.

// Get an instance of the WC_Order object (same as before)
$order = wc_get_order( $order_id );

// Get Order ID
$order->get_id();
 
// Get Order Totals $0.00
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
 
// Get Order Items
$order->get_items();
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item();
$order->get_item_count();
$order->get_item_subtotal();
$order->get_item_tax();
$order->get_item_total();
$order->get_downloadable_items();
 
// Get Order Lines
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();
 
// Get Order Shipping
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();
 
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
 
// Get Order User, Billing & Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
 
// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
 
// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();
 
// Get Order Status
$order->get_status();
 
// source: https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

FacetWp: Add a cutom search engine to search facet

//Add a custom Search Engine to search facet
add_filter('facetwp_facet_search_engines', function( $engines ) {
	$engines['custom_search_engine'] = 'Custom Search Engine';
	return $engines;
}, 10, 2);
	


//Modify the query search args for the custom search engine
function silmax_search_query_args( $search_args, $params ) {
	if($params['facet']['search_engine'] == 'custom_search_engine'){
		$search_args = [
			'posts_per_page' => -1,
			'fields' => 'ids',
		];
		return $search_args;
	}
}
add_filter( 'facetwp_search_query_args', 'silmax_search_query_args', 10, 2 );

Order Facetwp fselect by data-value with jQuery

(function($) {
  $(document).on('facetwp-loaded', function(){
    var $sorted_items,
    getSorted = function(selector, attrName) {
         return $(
	   $(selector).toArray().sort(function(a, b){		
	     // var aVal = parseInt(a.getAttribute(attrName));
            // var  bVal = parseInt(b.getAttribute(attrName));
									
	     var aVal = parseFloat($(a).data(attrName));
	     var bVal = parseFloat($(b).data(attrName));
									
	     return aVal - bVal;
	   })
        );
     };
				
     $('.facetwp-facet-slug_facet .fs-options').html(getSorted('.facetwp-facet-slug_facet .fs-options .fs-option', 'value'));
})(jQuery);