2017-06-28 54 views
1

我正在爲一家輪胎公司工作,我正在編寫兩個插件。WordPress插件覆蓋其他插件的存檔 - *。php

第一個插件是展示鋁合金輪轂。 第二個插件是展示輪胎。

兩個插件來作爲一個自定義後類型「輪子」和「輪胎」,兩者插件,它(模板/存檔wheels.php和模板/存檔tires.php)有一個模板邏輯

我的問題: 當兩個插件都處於活動狀態時,輪轂插件會覆蓋輪胎存檔頁面,我不知道它爲什麼會這樣做。

我真的試圖通過使用其他函數名,其他前綴等

這裏是註冊彩管和模板邏輯兩個插件相關的代碼,以避免提前命名空間衝突:

註冊自定義文章類型

function rg_wp_wheels_register_cpt(){ 

    // Custom Post Type Name 
    $cpt_name = 'wheels'; 
    // CPT Features 
    $cpt_features = array(
     'title', 
     'revisions' 
    ); 
    // Slug for archive page 
    $cpt_slug = 'designs'; 
    $labels = array(
     'name'      => __('Wheels', 'rg-wp-wheels'), 
     'singular_name'    => __('Wheel', 'rg-wp-wheels'), 
     'menu_name'     => __('Wheels', 'rg-wp-wheels'), 
     'name_admin_bar'   => __('Wheel', 'rg-wp-wheels'), 
     'all_items'     => __('Designs', 'rg-wp-wheels'), 
     'add_name'     => __('Add new wheel', 'rg-wp-wheels'), 
     'add_new_item'    => __('Add new wheel', 'rg-wp-wheels'), 
     'edit'      => __('Edit wheel', 'rg-wp-wheels'), 
     'edit_item'     => __('Edit wheel', 'rg-wp-wheels'), 
     'new_item'     => __('New wheel', 'rg-wp-wheels'), 
     'view'      => __('View', 'rg-wp-wheels'), 
     'view_item'     => __('View', 'rg-wp-wheels'), 
     'search_items'    => __('Search ', 'rg-wp-wheels'), 
     'parent'     => __('Parent', 'rg-wp-wheels'), 
     'not_found'     => __('No wheels found', 'rg-wp-wheels'), 
     'not_found_in_trash'  => __('No wheels found in Trash', 'rg-wp-wheels') 
); 
    $args = array(
     'labels'    => $labels, 
     'public'    => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'show_in_nav_menus'  => true, 
     'show_ui'    => true, 
     'show_in_menu'   => true, 
     'show_in_admin_bar'  => true, 
     'menu_position'   => 21, 
     'menu_icon'    => 'dashicons-marker', 
     'can_export'   => true, 
     'delete_with_user'  => false, 
     'hierarchical'   => false, 
     'has_archive'   => true, 
     'query_var'    => true, 
     'capability_type'  => 'post', 
     'map_meta_cap'   => true, 
    // 'capabilities'  => array(), 
     'rewrite'    => array(
      'slug'  => $cpt_slug, 
      'with_front'=> true, 
      'pages'  => true, 
      'feeds'  => false 
     ), 
     'supports'  => $cpt_features 
    ); 
    register_post_type($cpt_name, $args); 
} 
add_action('init', 'rg_wp_wheels_register_cpt'); 

模板邏輯爲車輪插件

function rg_wp_wheels_template_logic($original_template) { 
    $post_type = get_post_type(); 

    if(is_archive() || is_search() && $post_type == 'wheels') { 

     if(file_exists(get_template_directory_uri() . '/archive-wheels.php')) { 
      return get_template_directory_uri() . '/archive-wheels.php'; 
     } else { 
      return plugin_dir_path(__FILE__) . 'templates/archive-wheels.php'; 
     } 

    } elseif(is_single() && $post_type == 'wheels') { 

     if(file_exists(get_template_directory_uri() . '/single-wheels.php')) { 
      return get_template_directory_uri() . '/single-wheels.php'; 
     } else { 
      return plugin_dir_path(__FILE__) . 'templates/single-wheels.php'; 
     } 

    } 

    return $original_template; 
    } 
    add_action('template_include', 'rg_wp_wheels_template_logic'); 

註冊自定義文章類型輪胎

function rg_wp_tires_template_logic($original_template) { 
$post_type = get_post_type(); 

if(is_archive() || is_search() && $post_type == 'tires') { 

    if(file_exists(get_template_directory_uri() . '/archive-tires.php')) { 
     return get_template_directory_uri() . '/archive-tires.php'; 
    } else { 
     return plugin_dir_path(__FILE__) . 'templates/archive-tires.php'; 
    } 

} elseif(is_single() && $post_type == 'tires') { 

    if(file_exists(get_template_directory_uri() . '/single-tires.php')) { 
     return get_template_directory_uri() . '/single-tires.php'; 
    } else { 
     return plugin_dir_path(__FILE__) . 'templates/single-tires.php'; 
    } 

} 

return $original_template; 
} 
add_action('template_include', 'rg_wp_tires_template_logic'); 

當我停用車輪插件,輪胎插件模板:

輪胎插件
function rg_wp_tires_register_cpt(){ 

    // Custom Post Type Name 
    $cpt_name = 'tires'; 
    // CPT Features 
    $cpt_features = array(
     'title', 
     'revisions' 
    ); 
    // Slug 
    $cpt_slug_tires = 'profiles'; 
    $labels = array(
     'name'      => __('Tires', 'rg-wp-tires'), 
     'singular_name'    => __('Tire', 'rg-wp-tires'), 
     'menu_name'     => __('Tires', 'rg-wp-tires'), 
     'name_admin_bar'   => __('Tires', 'rg-wp-tires'), 
     'all_items'     => __('Profiles', 'rg-wp-tires'), 
     'add_name'     => __('Add new tire', 'rg-wp-tires'), 
     'add_new_item'    => __('Add new tire', 'rg-wp-tires'), 
     'edit'      => __('Edit tire', 'rg-wp-tires'), 
     'edit_item'     => __('Edit tire', 'rg-wp-tires'), 
     'new_item'     => __('New tire', 'rg-wp-tires'), 
     'view'      => __('View', 'rg-wp-tires'), 
     'view_item'     => __('View', 'rg-wp-tires'), 
     'search_items'    => __('Search ', 'rg-wp-tires'), 
     'parent'     => __('Parent', 'rg-wp-tires'), 
     'not_found'     => __('No tires found', 'rg-wp-tires'), 
     'not_found_in_trash'  => __('No tires found in Trash', 'rg-wp-tires') 
); 

    $args = array(
     'labels'    => $labels, 
     'public'    => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'show_in_nav_menus'  => true, 
     'show_ui'    => true, 
     'show_in_menu'   => true, 
     'show_in_admin_bar'  => true, 
     'menu_position'   => 22, 
     'menu_icon'    => 'dashicons-marker', 
     'can_export'   => true, 
     'delete_with_user'  => false, 
     'hierarchical'   => false, 
     'has_archive'   => true, 
     'query_var'    => true, 
     'capability_type'  => 'post', 
     'map_meta_cap'   => true, 
     // 'capabilities'  => array(), 
     'rewrite'    => array(
      'slug'  => $cpt_slug_tires, 
      'with_front'=> true, 
      'pages'  => true, 
      'feeds'  => false 
     ), 
     'supports'  => $cpt_features 
    ); 
    register_post_type($cpt_name, $args); 
} 
add_action('init', 'rg_wp_tires_register_cpt'); 

模板邏輯邏輯工作非常好。當兩個插件都處於活動狀態時,archive-wheels.php總是覆蓋archive-tires.php。爲什麼?

請幫助我,我對此失去了主意。

+0

我會張貼此對http://wordpress.stackexchange.com/。這一切對我來說都很好,很奇怪。 – Christina

+0

謝謝克里斯蒂娜,我在覈心開發人員的幫助下破解了這個螺母。是的,我已經在WPSE論壇上發佈了這個問題,但是沒有人回答..所以我在這裏嘗試了我的運氣..第一個條件是錯誤,請參閱下面的答案。謝謝你試圖幫助:) – user3135691

回答

1

一個核心貢獻者有我和一個答案merci。

問題是這樣的條件語句:

if(is_archive() || is_search() && $post_type == 'wheels') {.... 

需要被包裹在與歸檔兩個附加的支架和搜索

if((is_archive() || is_search()) && $post_type == 'wheels') {.... 

注意,歸檔和搜索在附加括號包裹起來

最重要的是,get_post_type在這種情況下是不正確的。它應該使用:is_post_type_archive('CPT')..

if(is_post_type_archive('wheels') || (is_search() && $_GET['post_type'] === 'wheels')) {... 

最終的正確功能如下:

// Template Logik 
function rg_wp_tires_template_logic($original_template) { 

    if(is_post_type_archive('tires') || (is_search() && $_GET['post_type'] === 'tires')) { 

     if(file_exists(get_template_directory_uri() . '/archive-tires.php')) { 
      return get_template_directory_uri() . '/archive-tires.php'; 
     } else { 
      return plugin_dir_path(__FILE__) . 'templates/archive-tires.php'; 
     } 

    } elseif(is_singular('tires')) { 

     if(file_exists(get_template_directory_uri() . '/single-tires.php')) { 
      return get_template_directory_uri() . '/single-tires.php'; 
     } else { 
      return plugin_dir_path(__FILE__) . 'templates/single-tires.php'; 
     } 

    } 

    return $original_template; 
} 
add_action('template_include', 'rg_wp_tires_template_logic'); 
+0

太棒了!很難看到。 WordPress有代碼編寫指南,其中一個是這樣的空間:'if(is_post_type_archive('tires')||(is_search()&& $ _GET ['post_type'] ==='tyres')){''' - 更易於閱讀。 – Christina