تابع ووکامرسی wc_product_post_type_link نمایش لینک پست تایپ محصول

تابع ووکامرسی wc_product_post_type_link نمایش لینک پست تایپ محصول

تابع ووکامرسی wc_product_post_type_link – نمایش لینک پست تایپ محصول با توجه به product_cat

Syntax – سینتکس

(string) wc_product_post_type_link( (string) $permalink, (WP_Post) $post ); 

Parameters – پارامتر ها (2)

  • 1- $permalink (string)
  • 2- $post (WP_Post)

Usage – نحوه استفاده

if ( !function_exists( 'wc_product_post_type_link' ) ) { 
    require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
} 
  
// The existing permalink URL. 
$permalink = ''; 
  
// The post. 
$post = null; 
  
// NOTICE! Understand what this does before running. 
$result = wc_product_post_type_link($permalink, $post); 
    

Defined – محل تعریف

/includes/wc-product-functions.php

function wc_product_post_type_link( $permalink, $post ) { 
    // Abort if post is not a product. 
    if ( 'product' !== $post->post_type ) { 
        return $permalink; 
    } 
 
    // Abort early if the placeholder rewrite tag isn't in the generated URL. 
    if ( false === strpos( $permalink, '%' ) ) { 
        return $permalink; 
    } 
 
    // Get the custom taxonomy terms in use by this post. 
    $terms = get_the_terms( $post->ID, 'product_cat' ); 
 
    if ( ! empty( $terms ) ) { 
        if ( function_exists( 'wp_list_sort' ) ) { 
            $terms = wp_list_sort( $terms, 'term_id', 'ASC' ); 
        } else { 
            usort( $terms, '_usort_terms_by_ID' ); 
        } 
        $category_object = apply_filters( 'wc_product_post_type_link_product_cat', $terms[0], $terms, $post ); 
        $category_object = get_term( $category_object, 'product_cat' ); 
        $product_cat = $category_object->slug; 
 
        if ( $category_object->parent ) { 
            $ancestors = get_ancestors( $category_object->term_id, 'product_cat' ); 
            foreach ( $ancestors as $ancestor ) { 
                $ancestor_object = get_term( $ancestor, 'product_cat' ); 
                $product_cat = $ancestor_object->slug . '/' . $product_cat; 
            } 
        } 
    } else { 
        // If no terms are assigned to this post, use a string instead (can't leave the placeholder there) 
        $product_cat = _x( 'uncategorized', 'slug', woocommerce ); 
    } 
 
    $find = array( 
        '%year%',  
        '%monthnum%',  
        '%day%',  
        '%hour%',  
        '%minute%',  
        '%second%',  
        '%post_id%',  
        '%category%',  
        '%product_cat%',  
 ); 
 
    $replace = array( 
        date_i18n( 'Y', strtotime( $post->post_date ) ),  
        date_i18n( 'm', strtotime( $post->post_date ) ),  
        date_i18n( 'd', strtotime( $post->post_date ) ),  
        date_i18n( 'H', strtotime( $post->post_date ) ),  
        date_i18n( 'i', strtotime( $post->post_date ) ),  
        date_i18n( 's', strtotime( $post->post_date ) ),  
        $post->ID,  
        $product_cat,  
        $product_cat,  
 ); 
 
    $permalink = str_replace( $find, $replace, $permalink ); 
 
    return $permalink; 
} 

versions – نسخه ها

از نسخه : 3.0.2

نسخه فعلی : 3.0.6

دیگر نسخه ها : 3.0.6 , 3.0.5 , 3.0.4 , 3.0.3 , 3.0.2

ارسال نظر

جهت استفاده از کد حتما از تگ pre استفاده نمایید .

contact us