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;

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.


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('' . $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
get_permalink( $product->get_id() );
// Get Product Prices
// Get Product Tax, Shipping & Stock
// Get Product Dimensions
// Get Linked Products
// Get Product Variations
// Get Product Taxonomies
// Get Product Downloads
// Get Product Images
// Get Product Reviews

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
// Get Order Totals $0.00
// Get Order Items
// Get Order Lines
// Get Order Shipping
// Get Order Dates
// Get Order User, Billing & Shipping Addresses
// Get Order Payment Details
// Get Order URLs
// Get Order Status
// source:

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'));

Stampare la categoria prodotto di woocommerce come classe nel body quando visiti un prodotto singolo

Con questa funzione puoi stampare tra le classi del body la categoria del prodotto singolo di woocommerce. Per funzionare basta copiare e incollare la funziona nel file functions.php

function sg_wooc_custom_taxonomy_in_body_class( $classes ){
  if( is_singular( 'product' ) ) {
    $custom_terms = get_the_terms(0, 'product_cat');
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {
        $classes[] = 'term-' . $custom_term->slug;
  return $classes;

add_filter( 'body_class', 'sg_wooc_custom_taxonomy_in_body_class' );

Stampare la categoria come classe nel body quando visiti un post singolo

Una funzione semplice che aiuta a stampare tra le classi del body la categoria del post singolo. Per funzionare basta copiare e incollare nel file functions.php

function sg_add_category_to_single($classes) {
    if (is_single() ) {
      global $post;
      foreach((get_the_category($post->ID)) as $category) {
            // add category slug to the $classes array
            $classes[] = $category->category_nicename;
    // return the $classes array
    return $classes;