如何在WordPress上的不同位置重复分类

时间:2015-11-26 作者:user1287087

我需要帮助。我需要在两个地方重复分类法,分别是post和coupones post,这段代码位于clipper主题中。

此代码将分类存储放在优惠券中。

add_action(\'init\', \'clpr_post_type\', 0);
// remove_action(\'init\', \'create_builtin_taxonomies\', 0); // in case we want to remove all default WP taxonomies
// register all the custom taxonomies and custom post type
function clpr_post_type() {
  global $wpdb, $app_abbr; //need $wpdb!!

// get the slug value for the ad custom post type & taxonomies
  if(get_option($app_abbr.\'_coupon_permalink\')) $post_type_base_url = get_option($app_abbr.\'_coupon_permalink\'); else $post_type_base_url = \'coupon\';
  if(get_option($app_abbr.\'_coupon_cat_tax_permalink\')) $cat_tax_base_url = get_option($app_abbr.\'_coupon_cat_tax_permalink\'); else $cat_tax_base_url = \'coupon-category\';
  if(get_option($app_abbr.\'_coupon_type_tax_permalink\')) $type_tax_base_url = get_option($app_abbr.\'_coupon_type_tax_permalink\'); else $type_tax_base_url = \'coupon-type\';
  if(get_option($app_abbr.\'_coupon_store_tax_permalink\')) $store_tax_base_url = get_option($app_abbr.\'_coupon_store_tax_permalink\'); else $store_tax_base_url = \'store\';
  if(get_option($app_abbr.\'_coupon_tag_tax_permalink\')) $tag_tax_base_url = get_option($app_abbr.\'_coupon_tag_tax_permalink\'); else $tag_tax_base_url = \'coupon-tag\';
  if(get_option($app_abbr.\'_coupon_image_tax_permalink\')) $image_tax_base_url = get_option($app_abbr.\'_coupon_image_tax_permalink\'); else $image_tax_base_url = \'coupon-image\';

  register_post_type( APP_POST_TYPE,
    array(  \'labels\' => array(
     \'name\' => __( \'Coupons\', \'appthemes\' ),
     \'singular_name\' => __( \'Coupons\', \'appthemes\' ),     
     \'add_new\' => __( \'Add New\', \'appthemes\' ),
     \'add_new_item\' => __( \'Add New Coupon\', \'appthemes\' ),
     \'edit\' => __( \'Edit\', \'appthemes\' ),
     \'edit_item\' => __( \'Edit Coupon\', \'appthemes\' ),
     \'new_item\' => __( \'New Coupon\', \'appthemes\' ),
     \'view\' => __( \'View Coupons\', \'appthemes\' ),
     \'view_item\' => __( \'View Coupon\', \'appthemes\' ),
     \'search_items\' => __( \'Search Coupons\', \'appthemes\' ),
     \'not_found\' => __( \'No coupons found\', \'appthemes\' ),
     \'not_found_in_trash\' => __( \'No coupons found in trash\', \'appthemes\' ),
     \'parent\' => __( \'Parent Coupon\', \'appthemes\' ),
     ),
    \'description\' => __( \'This is where you can create new coupon listings on your site.\', \'appthemes\' ),
    \'public\' => true,
    \'show_ui\' => true,
    \'capability_type\' => \'post\',
    \'publicly_queryable\' => true,
    \'exclude_from_search\' => false,
    \'menu_position\' => 8,
    \'menu_icon\' => get_template_directory_uri() . \'/images/site_icon.png\',
    \'hierarchical\' => false,
    \'rewrite\' => array( \'slug\' => $post_type_base_url, \'with_front\' => false ), /* Slug set so that permalinks work when just showing post name */
    \'query_var\' => true,
    \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'sticky\' )
    )
        );

    // register post status for unreliable coupons
        register_post_status( \'unreliable\', 
          array(  \'label\' => __( \'Unreliable\', \'appthemes\' ),
            \'public\' => true,
            \'_builtin\' => true,
            \'label_count\' => _n_noop( \'Unreliable <span class="count">(%s)</span>\', \'Unreliable <span class="count">(%s)</span>\', \'appthemes\' ),
            \'show_in_admin_all_list\' => true,
            \'show_in_admin_status_list\' => true,
            \'capability_type\' => APP_POST_TYPE,
            )
          );

    // register the newcategory taxonomy
        register_taxonomy( APP_TAX_CAT,
          array( APP_POST_TYPE ),
          array(  \'hierarchical\' => true,
            \'labels\' => array(
             \'name\' => __( \'Categories\', \'appthemes\'),
             \'singular_name\' => __( \'Coupon Category\', \'appthemes\'),
             \'search_items\' =>  __( \'Search Coupon Categories\', \'appthemes\'),
             \'all_items\' => __( \'All Coupon Categories\', \'appthemes\'),
             \'parent_item\' => __( \'Parent Coupon Category\', \'appthemes\'),
             \'parent_item_colon\' => __( \'Parent Coupon Category:\', \'appthemes\'),
             \'edit_item\' => __( \'Edit Coupon Category\', \'appthemes\'),
             \'update_item\' => __( \'Update Coupon Category\', \'appthemes\'),
             \'add_new_item\' => __( \'Add New Coupon Category\', \'appthemes\'),
             \'new_item_name\' => __( \'New Coupon Category Name\', \'appthemes\')
             ),
            \'show_ui\' => true,
            \'query_var\' => true,
            \'update_count_callback\' => \'_update_post_term_count\',
            \'rewrite\' => array( \'slug\' => $cat_tax_base_url, \'with_front\' => false, \'hierarchical\' => true ), 
            )
        );

        register_taxonomy( APP_TAX_TAG,
          array( APP_POST_TYPE ),
          array(  \'hierarchical\' => false,
            \'labels\' => array(
              \'name\' => __( \'Coupon Tags\', \'appthemes\'),
              \'singular_name\' => __( \'Coupon Tag\', \'appthemes\'),
              \'search_items\' =>  __( \'Search Coupon Tags\', \'appthemes\'),
              \'all_items\' => __( \'All Coupon Tags\', \'appthemes\'),
              \'edit_item\' => __( \'Edit Coupon Tag\', \'appthemes\'),
              \'update_item\' => __( \'Update Coupon Tag\', \'appthemes\'),
              \'add_new_item\' => __( \'Add New Coupon Tag\', \'appthemes\'),
              \'add_or_remove_items\' => __( \'Add or remove Coupon Tags\', \'appthemes\'),
              \'separate_items_with_commas\' => __( \'Separate Coupon Tags with commas\', \'appthemes\'),
              \'choose_from_most_used\' => __( \'Choose from the most common Coupon Tags\', \'appthemes\'),
              \'new_item_name\' => __( \'New Coupon Tag Name\', \'appthemes\')
              ),
            \'show_ui\' => true,
            \'query_var\' => true,
            \'update_count_callback\' => \'_update_post_term_count\',
            \'rewrite\' => array( \'slug\' => $tag_tax_base_url, \'with_front\' => false, \'hierarchical\' => true ), 
            )
        );

        register_taxonomy( APP_TAX_STORE,
          array( APP_POST_TYPE ),
          array(  \'hierarchical\' => true,
            \'labels\' => array(
              \'name\' => __( \'Stores\', \'appthemes\'),
              \'singular_name\' => __( \'Store\', \'appthemes\'),
              \'search_items\' =>  __( \'Search Stores\', \'appthemes\'),
              \'all_items\' => __( \'All Stores\', \'appthemes\'),
              \'edit_item\' => __( \'Edit Store\', \'appthemes\'),
              \'update_item\' => __( \'Update Store\', \'appthemes\'),
              \'add_new_item\' => __( \'Add New Store\', \'appthemes\'),
              \'add_or_remove_items\' => __( \'Add or remove Stores\', \'appthemes\'),
              \'separate_items_with_commas\' => __( \'Separate Stores with commas\', \'appthemes\'),
              \'choose_from_most_used\' => __( \'Choose from the most common Stores\', \'appthemes\'),
              \'new_item_name\' => __( \'New Store Name\', \'appthemes\')
              ),
            \'show_ui\' => true,
            \'query_var\' => true,
            \'update_count_callback\' => \'_update_post_term_count\',
            \'rewrite\' => array( \'slug\' => $store_tax_base_url, \'with_front\' => false, \'hierarchical\' => true ),
            )
        );

        register_taxonomy( APP_TAX_TYPE,
          array( APP_POST_TYPE ),
          array(  \'hierarchical\' => true,
            \'labels\' => array(
              \'name\' => __( \'Coupon Types\', \'appthemes\'),
              \'singular_name\' => __( \'Coupon Type\', \'appthemes\'),
              \'search_items\' =>  __( \'Search Coupon Types\', \'appthemes\'),
              \'all_items\' => __( \'All Coupon Types\', \'appthemes\'),
              \'parent_item\' => __( \'Parent Coupon Type\', \'appthemes\'),
              \'parent_item_colon\' => __( \'Parent Coupon Type:\', \'appthemes\'),
              \'edit_item\' => __( \'Edit Coupon Type\', \'appthemes\'),
              \'update_item\' => __( \'Update Coupon Type\', \'appthemes\'),
              \'add_new_item\' => __( \'Add New Coupon Type\', \'appthemes\'),
              \'new_item_name\' => __( \'New Coupon Type Name\', \'appthemes\')
              ),
            \'show_ui\' => true,
            \'query_var\' => true,
            \'update_count_callback\' => \'_update_post_term_count\',
            \'rewrite\' => array( \'slug\' => $type_tax_base_url, \'with_front\' => false, \'hierarchical\' => true ),
            )
        );

    // register taxonomy for printable coupon images
        register_taxonomy( APP_TAX_IMAGE,
          array( \'attachment\' ),
          array(  \'hierarchical\' => false,
            \'labels\' => array(
              \'name\' => __( \'Coupon Images\', \'appthemes\'),
              \'singular_name\' => __( \'Coupon Image\', \'appthemes\'),
              \'search_items\' =>  __( \'Search Coupon Images\', \'appthemes\'),
              \'all_items\' => __( \'All Coupon Images\', \'appthemes\'),
              \'parent_item\' => __( \'Parent Coupon Image\', \'appthemes\'),
              \'parent_item_colon\' => __( \'Parent Coupon Image:\', \'appthemes\'),
              \'edit_item\' => __( \'Edit Coupon Image\', \'appthemes\'),
              \'update_item\' => __( \'Update Coupon Image\', \'appthemes\'),
              \'add_new_item\' => __( \'Add New Coupon Image\', \'appthemes\'),
              \'new_item_name\' => __( \'New Coupon Image Name\', \'appthemes\')
              ),
            \'public\' => false,
            \'show_ui\' => false,
            \'query_var\' => true,
            \'update_count_callback\' => \'_update_post_term_count\',
            \'rewrite\' => array( \'slug\' => $image_tax_base_url, \'with_front\' => false, \'hierarchical\' => false ),
            )
        );

        $wpdb->storesmeta = $wpdb->clpr_storesmeta;

  // this needs to happen once after install script first runs
        if ( get_option( $app_abbr.\'_rewrite_flush_flag\' ) == \'true\' ) {
          flush_rewrite_rules();
          delete_option( $app_abbr.\'_rewrite_flush_flag\' );
        }

      }
这是我在同一个文件中的代码。

// new code taxonomias
function clrp_taxonomy_post(){

    global $wpdb, $app_abbr; //need $wpdb!!
    if(get_option($app_abbr.\'_coupon_store_tax_permalink\')) $store_tax_base_url = get_option($app_abbr.\'_coupon_store_tax_permalink\'); else $store_tax_base_url = \'store\';

    register_taxonomy( APP_TAX_STORE, // taxonomy stores
        array( APP_POST_TYPE_POST ), // type POST
        array(  \'hierarchical\' => true,
            \'labels\' => array(
                \'name\' => __( \'Stores\', \'appthemes\'),
                \'singular_name\' => __( \'Store\', \'appthemes\'),
                \'search_items\' =>  __( \'Search Stores\', \'appthemes\'),
                \'all_items\' => __( \'All Stores\', \'appthemes\'),
                \'edit_item\' => __( \'Edit Store\', \'appthemes\'),
                \'update_item\' => __( \'Update Store\', \'appthemes\'),
                \'add_new_item\' => __( \'Add New Store\', \'appthemes\'),
                \'add_or_remove_items\' => __( \'Add or remove Stores\', \'appthemes\'),
                \'separate_items_with_commas\' => __( \'Separate Stores with commas\', \'appthemes\'),
                \'choose_from_most_used\' => __( \'Choose from the most common Stores\', \'appthemes\'),
                \'new_item_name\' => __( \'New Store Name\', \'appthemes\')
            ),
            \'show_ui\' => true,
            \'query_var\' => true,
            \'update_count_callback\' => \'_update_post_term_count\',
            \'rewrite\' => array( \'slug\' => $store_tax_base_url, \'with_front\' => false, \'hierarchical\' => true ),
        )
    );
}

add_action(\'init\', \'clrp_taxonomy_post\', 0);
最后一段代码只显示在post中,而不显示在优惠券中,我需要在post和优惠券中显示此分类法

感谢您的关注。

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

register_taxonomy( \'taxonomy\', array( \'post\', \'coupons\' ), $args );
https://generatewp.com/taxonomy/ 可以帮助你。

// new code taxonomias
function clrp_taxonomy_post(){

    global $wpdb, $app_abbr; //need $wpdb!!
    if(get_option($app_abbr.\'_coupon_store_tax_permalink\')) $store_tax_base_url = get_option($app_abbr.\'_coupon_store_tax_permalink\'); else $store_tax_base_url = \'store\';

    register_taxonomy( APP_TAX_STORE, // taxonomy stores
        array( APP_POST_TYPE_POST, APP_POST_TYPE ), // type POST + APP_POST_TYPE 
        array(  \'hierarchical\' => true,
            \'labels\' => array(
                \'name\' => __( \'Stores\', \'appthemes\'),
                \'singular_name\' => __( \'Store\', \'appthemes\'),
                \'search_items\' =>  __( \'Search Stores\', \'appthemes\'),
                \'all_items\' => __( \'All Stores\', \'appthemes\'),
                \'edit_item\' => __( \'Edit Store\', \'appthemes\'),
                \'update_item\' => __( \'Update Store\', \'appthemes\'),
                \'add_new_item\' => __( \'Add New Store\', \'appthemes\'),
                \'add_or_remove_items\' => __( \'Add or remove Stores\', \'appthemes\'),
                \'separate_items_with_commas\' => __( \'Separate Stores with commas\', \'appthemes\'),
                \'choose_from_most_used\' => __( \'Choose from the most common Stores\', \'appthemes\'),
                \'new_item_name\' => __( \'New Store Name\', \'appthemes\')
            ),
            \'show_ui\' => true,
            \'query_var\' => true,
            \'update_count_callback\' => \'_update_post_term_count\',
            \'rewrite\' => array( \'slug\' => $store_tax_base_url, \'with_front\' => false, \'hierarchical\' => true ),
        )
    );
}

add_action(\'init\', \'clrp_taxonomy_post\', 0);

SO网友:Milo

的第二个参数register_taxonomy 是要与之关联的对象类型。现在是:

array( APP_POST_TYPE_POST )
将其更改为:

array( APP_POST_TYPE_POST, APP_POST_TYPE )
您还可以使用register_taxonomy_for_object_type 为了实现这一点taxonomies 的参数register_post_type.

相关推荐

初学者问题:通过管理Web界面访问Functions.php以导入自定义帖子类型?

是否可以访问这些功能。php文件仅仅使用管理web界面?我正在尝试访问以前创建的(手动编码的)自定义帖子类型,我不得不跳过很多障碍,因为我无法访问函数中的代码。php文件。我已经浏览了很多帮助页面,但建议的步骤似乎总是涉及到函数。php文件(我无法访问)或使用插件中的导入/导出工具,该插件首先创建了自定义帖子类型(据我所知,没有使用任何插件)。这似乎是一个非常基本的问题,但我一辈子都想不出来。任何帮助都将不胜感激!