WooCommerce产品固定链接中的自定义分类术语

时间:2016-10-17 作者:INT

我已经为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\' );

2 个回复
最合适的回答,由SO网友:INT 整理而成

像往常一样,最终还是太简单了,使用了错误的钩子。post_link 仅适用于应使用的帖子post_type_link 相反

/**
 * 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_type_link\', \'wpse_56769_post_link\', 10, 2 );
function wpse_56769_post_link( $permalink, $post ) {

    if( $post->post_type == \'product\' ) {
      $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;
}

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提供了非常有用的答案,并完成了所有的腿部工作!

相关推荐

我在相关产品“TAX_QUERY”‘TERMS’值方面遇到问题

我有两种自定义页面类型。第一个是;“产品”;第二个是;附件;。在产品页面上,有“附件”类别;“输入”;罗列立柱。输入对值进行编码,如;06010603等;。这些代码也是产品代码。当我用metabox输入产品代码时,功能会在某些区域启动。如产品表、产品图片等。如何在术语部分打印此代码。提前感谢你们的帮助。工作代码;<?php $article_list = get_post_meta ( get_the_ID(), \'products_article_list\', t