【问题标题】:WordPress plugin overwrites other plugin's archive-*.phpWordPress 插件覆盖其他插件的存档-*.php
【发布时间】:2017-06-28 09:18:37
【问题描述】:

我在一家轮胎公司工作,正在编写两个插件。

第一个插件是展示合金轮毂。 第二个插件是显示轮胎。

这两个插件都是自定义帖子类型“轮子”和“轮胎”,并且两个插件都包含模板逻辑(templates/archive-wheels.php 和 templates/archive-tires.php)

我的问题: 当两个插件都处于活动状态时,wheels 插件会覆盖 tires 存档页面,我不知道它到底为什么这样做。

我确实已经尝试通过使用其他函数名、其他前缀等来提前避免命名空间冲突。

以下是注册 CPT 的相关代码和两个插件的模板逻辑:

注册自定义帖子类型wheels

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');

wheels 插件的模板逻辑

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_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');

轮胎插件的模板逻辑:

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');

当我停用车轮插件时,轮胎插件模板逻辑工作得非常好。当两个插件都处于活动状态时,archive-wheels.php 总是会覆盖archive-tires.php。为什么?

请帮帮我,我正在失去理智。

【问题讨论】:

  • 我会在wordpress.stackexchange.com 上发布这个。这一切对我来说都很好,很奇怪。
  • 谢谢克里斯蒂娜,我在核心开发人员的帮助下解决了问题。是的,我已经在 WPSE 论坛上发布了这个问题,但没有人回答.. 所以我在这里试试运气.. 第一个条件是错误,请在下面查看我的答案。感谢您尝试提供帮助:)

标签: wordpress templates


【解决方案1】:

一位核心贡献者向我表示感谢并给出了答案。

问题是这个条件语句:

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');

【讨论】:

  • 太棒了!很难看到。 WordPress 有代码编写指南,其中之一是这样的空间:if( is_post_type_archive( 'tires' ) || ( is_search() && $_GET[ 'post_type' ] === 'tires' ) ) { -- 更易于阅读。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 2019-01-27
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
相关资源
最近更新 更多