问题描述

我想组织主题:

  1. wp-content/themes/themeshop/theme1

  2. wp-content/themes/themeshop/theme2

  3. wp-content/themes/themeshop/theme3

从我的理解,这个组织在文件夹级别上完美的工作。我删除了子目录中的主题,一切似乎都很好。现在可以在主题页面的管理后台复制此分类吗?

最佳解决办法

Updated plugin version available at GitHub.

我第一次在 [wp-hackers] list 看到你的问题,在实施解决方案之后,即将出版一份 Q&A 。嗯,它已经在这里了,并有一个赏金放在它:)

正如 Daniel Bachhuber 指出的那样:

WordPress.com puts themes inside of subdirectories

/wp-content/themes/public  /wp-content/themes/premium 

我想出了这个解决方案,显示在后端:

  • 在 「多站点」 中,我们可以在 「主题」 屏幕中添加一列,并使用 manage_themes_custom_columnmanage_themes-network_columns 显示信息。

  • 在单站安装中,我发现的唯一入口点是 theme_action_links

<?php
/**
 * Plugin Name: Theme Folders Categories
 * Plugin URI: http://wordpress.stackexchange.com/q/96361/12615
 * Version: 1.0
 * Author: Rodolfo Buaiz
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 * License: GPLv2 or later
 */
class WPhackersSE_Theme_Folders
{
    public function __construct()
    {
        add_action( 'plugins_loaded', array( $this, 'start_up' ) );
    }

    /**
     * Hooks for Network themes and Single Site themes
     * Nothing happens on sub-sites of a Network
     */
    public function start_up()
    {
        if( is_network_admin() )
        {
            add_filter( 'manage_themes-network_columns', array( $this, 'column_register' ) );
            add_action( 'manage_themes_custom_column', array( $this, 'column_display' ), 10, 3 );
            add_action( 'admin_head-themes.php', array( $this, 'network_theme_category_css' ) );
        }
        elseif( !is_multisite() )
        {
            add_filter( 'theme_action_links', array( $this, 'theme_folder_single_site' ), 10, 2 );
            add_action( 'admin_head-themes.php', array( $this, 'theme_category_css' ) );
        }
    }

    /**
     * Add custom category (folder) column in network themes
     *
     * @param array $columns
     * @return array
     */
    public function column_register( $columns )
    {
        $columns['theme_folder'] = 'Category';
        return $columns;
    }

    /**
     * Display custom row in network themes
     * $stylesheet contains a string "folder/theme_name"
     * $theme is a WP_Theme object
     *
     * @param string $column_name
     * @param string $stylesheet
     * @param object $theme
     * @return string
     */
    public function column_display( $column_name, $stylesheet, $theme )
    {
        if( 'theme_folder' != $column_name  )
            return;

        echo $this->make_button( $stylesheet );
    }

    /**
     * Adjust column width and button style in Multisite screen
     */
    public function network_theme_category_css()
    {
        echo "<style type='text/css'>
            #theme_folder { width: 10% }
            {$this->button_style()}
            </style>";
    }

    /**
     * Show theme category (folder) in single site theme action row
     * $theme is a WP_Theme object
     *
     * @param array $actions
     * @param object $theme
     * @return array
     */
    public function theme_folder_single_site( $actions, $theme )
    {
        array_unshift( $actions, $this->make_button( $theme->stylesheet ) );
        return $actions;
    }

    /**
     * Adjust button style in Single site screen
     */
    public function theme_category_css()
    {
        echo "<style type='text/css'>{$this->button_style()}</style>";
    }

    /**
     * Common button for Multi and Single sites
     * The category name is extracted from a string "folder/themefolder"
     *
     * @param object $theme
     * @return string
     */
    private function make_button( $stylesheet )
    {
        $button_category = sprintf(
            '<a href="javascript:void(0)" class="button-secondary theme-folder" title="%1$s">%1$s</a>',
            dirname( $stylesheet )
        );
        return $button_category;
    }

    /**
     * Common style for Multi and Single sites
     *
     * @return string
     */
    private function button_style()
    {
        return '.theme-folder {
            cursor: default !important;
            line-height: 15px !important;
            height: 17px !important;
            background-image: -webkit-gradient(linear, left top, left bottom, from(#DCFEDE), to(#CBEBCD)) !important;
            background-image: -webkit-linear-gradient(top, #DCFEDE, #CBEBCD) !important;
            background-image: -moz-linear-gradient(top, #DCFEDE, #CBEBCD) !important;
            background-image: -o-linear-gradient(top, #DCFEDE, #CBEBCD) !important;
            background-image: linear-gradient(to bottom, #DCFEDE, #CBEBCD) !important;
        }';
    }
}

new WPhackersSE_Theme_Folders;

结果在多站点:文件夹/themes/clientes//themes/brasofilo/) 。在网络的 sub-sites 中没有输出。

很可能在这里添加一个过滤器。


导致单个站点:多站点未启用。文件夹/themes/defaults//themes/others/

这个屏幕在定制方面确实有限。我同意 @ Ralf912 分析。


重要提示:将活动主题移至 sub-folders 后,所有网站都会丢失其主题配置,每个网站主题都必须重新设置。

次佳解决办法

我想你正在寻找一个动作钩子或过滤器,从主题标准列表中排除主题,并创建一个表示文件夹结构的额外列表。或者更简单地将 hemes 分组到他们所在的文件夹中。

如果打开 wp-admin/themes.php,您将看到没有动作钩子或过滤器。在使用的功能或列表类 (WP_Themes_List_Table) 中也没有动作钩子或过滤器,您可以在其中修改主题列表。

您唯一可以做的是重新编辑主题页面上显示主题的完整过程,并将管理菜单中的 themes.php 链接替换为您自己的脚本。

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。