删除具有2个分类和1个POST元的CPT slug

时间:2018-04-11 作者:Max

我正在尝试删除我的CPT slug。我已经尝试了关于堆栈溢出的各种答案,但由于某些原因,似乎没有一个有效。

我的代码:

    add_filter(\'post_type_link\', array(__CLASS__, \'rewrite_permalink\'), 10, 3);

//Tell WP products can rewrtie this way
add_action( \'pre_get_posts\',  array(__CLASS__, \'products_parse_request\' ) );

add_action(\'init\', array( __CLASS__, \'add_rewrite_tags\'), 10, 0);


  public static function add_rewrite_tags() {
      add_rewrite_tag(\'%product_holiday%\', \'([^&]+)\');
    }

    function rewrite_permalink($permalink, $post_id, $leavename) {
      $post = get_post($post_id);
        if ( self::$post_type != $post->post_type || \'publish\' != $post->post_status ) return $permalink;

            // Get post
            if (!$post) return $permalink;


            // Get country terms
            $countries = wp_get_object_terms($post->ID, \'_product_countries\');
            if (!is_wp_error($countries) && !empty($countries) && is_object($countries[0])) {
              $country_slug = $countries[0]->slug;
            } else {
              $country_slug = \'country\';
            }

            //Get product type
            $product_type = wp_get_object_terms($post->ID, \'_product_types\');
            if (!is_wp_error($product_type) && !empty($product_type) && is_object($product_type[0])) {
              $product_type_slug = $product_type[0]->slug;
            } else {
             $product_type_slug = \'product-type\';
            }

            //Get post holiday
            $holiday = get_post_meta($post->ID, \'_event_holiday_type\', true);
            if (!empty($holiday)) {
              $holiday = $holiday;
            } else {
             $holiday = \'holiday\';
            }

          return strtr($permalink, array( \'%product_holiday%\' => $holiday, \'%_product_countries%\' => $country_slug, \'%_product_types%\' => $product_type_slug));

          //\'/\' . self::$post_type . \'/\' => \'/\',
    }


    function products_parse_request( $query ) {


      if ( ! $query->is_main_query() || 6 != count( $query->query ) || ! isset( $query->query[\'page\'] ) ) {
          return;
      }

      if ( ! empty( $query->query[\'name\'] ) ) {
          $query->set( \'post_type\', array( \'post\', self::$post_type, \'page\' ) );
      }

    }

    /**
     * Register post type
     */
    private static function register_post_types() {

        $post_types = array();

        $labels = array(
          \'name\'                => __(\'Products\', THEME_TEXT_DOMAIN),
          \'singular_name\'       => __(\'Product\', THEME_TEXT_DOMAIN),
          \'menu_name\'           => __(\'Products\', THEME_TEXT_DOMAIN),
          \'name_admin_bar\'      => __(\'Product\', THEME_TEXT_DOMAIN),
          \'add_new\'             => __(\'Add New\', THEME_TEXT_DOMAIN),
          \'add_new_item\'        => __(\'Add New Product\', THEME_TEXT_DOMAIN),
          \'new_item\'            => __(\'New Product\', THEME_TEXT_DOMAIN),
          \'edit_item\'           => __(\'Edit Product\', THEME_TEXT_DOMAIN),
          \'view_item\'           => __(\'View Product\', THEME_TEXT_DOMAIN),
          \'all_items\'           => __(\'All Products\', THEME_TEXT_DOMAIN),
          \'search_items\'        => __(\'Search Products\', THEME_TEXT_DOMAIN),
          \'parent_item_colon\'   => __(\'Parent Products:\', THEME_TEXT_DOMAIN),
          \'not_found\'           => __(\'No Products found.\', THEME_TEXT_DOMAIN),
          \'not_found_in_trash\'  => __(\'No Products found in Trash.\', THEME_TEXT_DOMAIN)
        );

        $post_types[] =
        array(
          \'post_type\' => \'products\',
          \'args\' => array(
              \'labels\'              => $labels,
              \'public\'              => true,
              \'publicly_queryable\'  => true,
              \'show_ui\'             => true,
              \'show_in_menu\'        => true,
              \'query_var\'           => true,
              // \'taxonomies\' => array(\'_product_types\', \'_product_countries\'),
              \'rewrite\'             => array( \'slug\' => \'products/%product_holiday%/%_product_countries%/%_product_types%\' ),
              \'capability_type\'     => \'post\',
              \'has_archive\'         => false,
              \'hierarchical\'        => true,
              \'menu_icon\'           => \'dashicons-tag\',
              \'menu_position\'       => 41,
              \'supports\'            => array(\'title\', \'thumbnail\', \'page-attributes\')
         )
        );

        foreach($post_types as $post_type) {
          register_post_type($post_type[\'post_type\'], $post_type[\'args\']);
        }
    }
此代码适用于URL结构/产品/假日/国家/产品类型,但我需要删除/产品。。。如果我从帖子类型中删除它,它会工作,但是WordPress上的其余页面会中断,所以我猜我还需要在这里添加其他内容?

任何帮助都将不胜感激。

谢谢

1 个回复
SO网友:Max

最终设法将其排序,需要添加重写规则(见下文),如果您需要完整的源代码,请告诉我,我将非常乐意粘贴它。

    public static function add_rewrite_rules() {
  add_rewrite_rule( \'^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?\', \'index.php?post_type=products&product_holiday=$matches[1]&_product_countries=$matches[2]&_product_types=$matches[3]&name=$matches[4]\', \'top\' );
}
    add_action(\'init\', array( __CLASS__, \'add_rewrite_rules\') );
谢谢你,

结束

相关推荐

How to replace permalinks

我已将wordpress项目导出并导入live server。现在的问题是,我将permalink结构作为“localhost”格式。当我单击网站中的任何链接时,它会将我重定向到localhost。我怎样才能改变这一点?我的htaccess文件如下所示<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /myproject/ RewriteRule ^index\\.php$ - [L] RewriteCo