WC_Brands::post_type_link()publicWC 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 9.4.2

public function 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_brand' );

	if ( empty( $terms ) ) {
		// If no terms are assigned to this post, use a string instead (can't leave the placeholder there).
		$product_brand = _x( 'uncategorized', 'slug', 'woocommerce' );
	} else {
		// Replace the placeholder rewrite tag with the first term's slug.
		$first_term    = array_shift( $terms );
		$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;
}