問題描述

我想組織主題:

  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="" 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。