WC_Brands::post_type_linkpublicWC 1.0

Filter to allow product_brand in the permalinks for products.

Метод класса: WC_Brands{}

Хуков нет.

Возвращает

Строку.

Использование

$WC_Brands = new WC_Brands();
$WC_Brands->post_type_link( $permalink, $post );
$permalink(строка) (обязательный)
The existing permalink URL.
$post(WP_Post) (обязательный)
The post.

Код WC_Brands::post_type_link() WC 10.4.3

public function post_type_link( $permalink, $post ) {
	// Abort if post is not a product.
	if ( 'product' !== $post->post_type ) {
		return $permalink;
	}

	if ( empty( $permalink ) ) {
		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_brand' );

	// If no terms are assigned to this post, use a string instead (can't leave the placeholder there).
	$product_brand = _x( 'uncategorized', 'slug', 'woocommerce' );

	if ( is_array( $terms ) && ! empty( $terms ) ) {
		// Replace the placeholder rewrite tag with the first term's slug.
		$first_term = array_shift( $terms );
		if ( $first_term instanceof WP_Term ) {
			$product_brand = $first_term->slug;
		}
	}

	$find = array(
		'%product_brand%',
	);

	$replace = array(
		$product_brand,
	);

	$replace = array_map( 'sanitize_title', $replace );

	$permalink = str_replace( $find, $replace, $permalink );

	return $permalink;
}