我已经为WooCommerce创建的产品帖子类型创建了自定义分类法。我想将这种分类法用于产品的永久链接。
我偶然发现this 这对我想做的事情来说似乎是完美的。然而,我几乎没有运气让它发挥作用。在帖子中添加代码后,我仍然会得到%item%(我正在使用它,而不是%artist%),而不是永久链接中的术语。
<?php
/*
Plugin Name: Woocommerce Item Taxonomy
Description: Enables a Taxonomy called "Item" for organizing multiple single products under an unified item.
Version: 1.0.0
License: MIT License
*/
add_action( \'init\', \'ps_taxonomy_item\' );
function ps_taxonomy_item() {
$labels = array(
\'name\' => \'Item\',
\'singular_name\' => \'Item\',
\'menu_name\' => \'Item\',
\'all_items\' => \'All Items\',
\'parent_item\' => \'Parent Item\',
\'parent_item_colon\' => \'Parent Item:\',
\'new_item_name\' => \'New Item Name\',
\'add_new_item\' => \'Add New Item\',
\'edit_item\' => \'Edit Item\',
\'update_item\' => \'Update Item\',
\'separate_items_with_commas\' => \'Separate Item with commas\',
\'search_items\' => \'Search Items\',
\'add_or_remove_items\' => \'Add or remove Items\',
\'choose_from_most_used\' => \'Choose from the most used Items\',
);
$args = array(
\'labels\' => $labels,
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'show_in_nav_menus\' => true,
\'show_tagcloud\' => true,
);
register_taxonomy( \'item\', \'product\', $args );
register_taxonomy_for_object_type( \'item\', \'product\' );
}
/**
* replace the \'%item%\' tag with the first
* term slug in the item taxonomy
*
* @wp-hook post_link
* @param string $permalink
* @param WP_Post $post
* @return string
*/
add_filter( \'post_link\', \'wpse_56769_post_link\', 10, 2 );
function wpse_56769_post_link( $permalink, $post ) {
$default_term = \'no_item\';
$terms = wp_get_post_terms( $post->ID, \'item\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) )
$term = current( $terms )->slug;
else
$term = $default_term;
$permalink = str_replace( \'%item%\', $term, $permalink );
return $permalink;
}
/**
* set WP_Rewrite::use_verbose_page_rules to TRUE if %item%
* is used as the first rewrite tag in post permalinks
*
* @wp-hook do_parse_request
* @wp-hook page_rewrite_rules
* @global $wp_rewrite
* @param mixed $pass_through (Unused)
* @return mixed
*/
function wpse_56769_rewrite_verbose_page_rules( $pass_through = NULL ) {
$permastruct = $GLOBALS[ \'wp_rewrite\' ]->permalink_structure;
$permastruct = trim( $permastruct, \'/%\' );
if ( 0 !== strpos( $permastruct, \'item%\' ) )
return $pass_through;
$GLOBALS[ \'wp_rewrite\' ]->use_verbose_page_rules = TRUE;
return $pass_through;
}
add_filter( \'page_rewrite_rules\', \'wpse_56769_rewrite_verbose_page_rules\', PHP_INT_MAX );
add_filter( \'do_parse_request\', \'wpse_56769_rewrite_verbose_page_rules\', PHP_INT_MAX );
/**
* check for existing item and set query to 404 if necessary
*
* @wp-hook parse_query
* @param array $request_vars
* @return array
*/
function wpse_56769_request_vars( $request_vars ) {
if ( ! isset( $request_vars[ \'item\' ] ) )
return $request_vars;
if ( ! isset( $request_vars[ \'name\' ] ) )
return $request_vars;
if ( \'no_item\' == $request_vars[ \'item\' ] )
unset( $request_vars[ \'item\' ] );
return $request_vars;
}
add_filter( \'request\', \'wpse_56769_request_vars\' );
SO网友:RCNeil
我对WooCommerce产品供应商使用了这个非常有用的答案,因为我希望供应商在Permalink中。下面是一个经过修改的压缩片段,可以在functions.php
文件
//VENDOR PERMALINK
add_filter( \'post_type_link\', \'wpse_56769_post_link\', 10, 2 );
function wpse_56769_post_link( $permalink, $post ) {
if( $post->post_type == \'product\' ) {
$default_term = \'vendor\';
$terms = wp_get_post_terms( $post->ID, \'wcpv_product_vendors\' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) )
$term = current( $terms )->slug;
else
$term = $default_term;
$permalink = str_replace( \'%wcpv_product_vendors%\', $term, $permalink );
}
return $permalink;
}
function wpse_56769_rewrite_verbose_page_rules( $pass_through = NULL ) {
$permastruct = $GLOBALS[ \'wp_rewrite\' ]->permalink_structure;
$permastruct = trim( $permastruct, \'/%\' );
if ( 0 !== strpos( $permastruct, \'wcpv_product_vendors%\' ) )
return $pass_through;
$GLOBALS[ \'wp_rewrite\' ]->use_verbose_page_rules = TRUE;
return $pass_through;
}
add_filter( \'page_rewrite_rules\', \'wpse_56769_rewrite_verbose_page_rules\',
PHP_INT_MAX );
add_filter( \'do_parse_request\', \'wpse_56769_rewrite_verbose_page_rules\',
PHP_INT_MAX );
function wpse_56769_request_vars( $request_vars ) {
if ( ! isset( $request_vars[ \'wcpv_product_vendors\' ] ) )
return $request_vars;
if ( ! isset( $request_vars[ \'name\' ] ) )
return $request_vars;
if ( \'no_item\' == $request_vars[ \'wcpv_product_vendors\' ] )
unset( $request_vars[ \'wcpv_product_vendors\' ] );
return $request_vars;
}
add_filter( \'request\', \'wpse_56769_request_vars\' );
然后你会把标签
%wcpv_product_vendors%
在所需的永久链接设置中。
感谢OP@INT提供了非常有用的答案,并完成了所有的腿部工作!