add_shortcode( 'product_description', 'display_product_description' );
function display_product_description( $atts ){ $atts = shortcode_atts( array( 'id' => get_the_id(), ), $atts, 'product_description' ); global $product; if ( ! is_a( $product, 'WC_Product') ) $product = wc_get_product($atts['id']); return $product->get_description(); } Ürün Kısa Açıklaması Kısa Kodu
4
●138
- 30-01-2025, 13:31:56Temanın function.php dosyasına alttaki kodu ekleyip kısa kod oluşturdum bu kod ürün açıklamasını çekiyor. Bu kodun sadece ilk cümleyi çekicek şekilde nasıl düzenliyebilirim
- 30-01-2025, 13:34:45
add_shortcode( 'product_description', 'display_product_description' ); function display_product_description( $atts ) { $atts = shortcode_atts( array( 'id' => get_the_id(), ), $atts, 'product_description' ); global $product; if ( ! is_a( $product, 'WC_Product' ) ) $product = wc_get_product( $atts['id'] ); $description = $product->get_description(); // İlk cümleyi almak için metni '.' karakterine göre ayır $first_sentence = strtok( $description, '.' ); // İlk cümleyi döndür return $first_sentence . '.'; } - 30-01-2025, 13:34:54
$first_sentence = strtok($description, '.'); return $first_sentence . '.';
en sona eklersek istediğimiz olur. yani:
add_shortcode( 'product_description', 'display_product_description' ); function display_product_description( $atts ){ $atts = shortcode_atts( array( 'id' => get_the_id(), ), $atts, 'product_description' ); global $product; if ( ! is_a( $product, 'WC_Product') ) $product = wc_get_product($atts['id']); $description = $product->get_description(); $first_sentence = strtok($description, '.'); return $first_sentence . '.'; } - 04-02-2025, 16:41:44Bu kodu eklediğimde rest-api sayfasında hata alıyordum. bende başka bi yerde bu kodu buldum. bu kodla açıklamayı nasıl tek cümleye düşürebilirm
function custom_product_description($atts){ global $product; try { if( is_a($product, 'WC_Product') ) { return wc_format_content( $product->get_description("shortcode") ); } return "Product description shortcode run outside of product context"; } catch (Exception $e) { return "Product description shortcode encountered an exception"; } } add_shortcode( 'custom_product_description', 'custom_product_description' ); - 07-02-2025, 12:47:33emreyildz41 adlı üyeden alıntı: mesajı görüntüle
function custom_product_description($atts){ global $product; try { if( is_a($product, 'WC_Product') ) { // Ürün açıklamasını "shortcode" formatında alıyoruz. $description = $product->get_description("shortcode"); // Öncelikle isterseniz HTML etiketlerini temizleyebilirsiniz // (HTML etiketleri gerekmediği durumlarda faydalı olabilir). $description = wp_strip_all_tags($description); // Nokta, soru veya ünlem işaretinden sonra gelen boşluğa göre parçalama yapıyoruz. // preg_split ile 2 parçaya ayırıyoruz, böylece ilk cümleyi ve geri kalanı elde ediyoruz. $sentences = preg_split('/([.?!])\s/', $description, 2, PREG_SPLIT_DELIM_CAPTURE); // $sentences[0] ilk cümlenin metin kısmını, // $sentences[1] ise cümlenin sonundaki noktalama işaretini tutar (eğer varsa). $firstSentence = $sentences[0]; // Cümle sonu noktalamasını da eklemek isterseniz: if(isset($sentences[1])) { $firstSentence .= $sentences[1]; } // İlk cümleyi döndürüyoruz return $firstSentence; } return "Product description shortcode run outside of product context"; } catch (Exception $e) { return "Product description shortcode encountered an exception"; } } add_shortcode( 'custom_product_description', 'custom_product_description' );