自定义仪表板中列出的edit.php页面以仅显示应用了特定模板的页面?

时间:2012-09-12 作者:lancemonotone

我想,这个问题说明了一切。我想使用edit创建一个新的子菜单页。php,它将只显示使用特定模板文件的页面。

可能的

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

Try this:

<?php

// add submenu page
add_action(\'admin_menu\', \'add_template_submenu\');   
function add_template_submenu()
{
    add_pages_page( \'My Template\', \'My Template\', \'edit_pages\', \'edit.php?post_type=page&template=your-template-file.php\');
}

// check for the template name passed in $_GET and add it to the query
add_filter(\'parse_query\', \'filter_pages\');  
function filter_pages($query)
{
    global $typenow, $pagenow;
    if ($pagenow == \'edit.php\' && $typenow == \'page\' && $_GET[\'template\'])
    {
        $query->query_vars[\'meta_key\'] = \'_wp_page_template\';
        $query->query_vars[\'meta_value\'] = $_GET[\'template\'];
    }
    return $query;
}
SO网友:lancemonotone
    add_action(\'admin_menu\',\'add_home_page_menu\');
    add_action(\'load-edit.php\', \'filter_home_pages_mysql_action\'); 

    function add_home_page_menu(){
        add_pages_page(\'Home Pages Menu\', \'Home Pages\', \'manage_options\', \'edit.php?post_type=page&page_template=home_pages\');
        add_pages_page(\'Landing Pages Menu\', \'Landing Pages\', \'manage_options\', \'edit.php?post_type=page&page_template=landing_pages\');
    }


    function filter_home_pages_mysql_action() {
        if ($_GET[\'post_type\'] !== \'page\') return;
        switch($_GET[\'page_template\']){
            case \'home_pages\':
                add_filter(\'posts_where\', \'home_pages_where_filter\') );
                add_filter(\'posts_join\', \'join_filter\') );
                break;
            case \'landing_pages\':
                add_filter(\'posts_where\', \'landing_pages_where_filter\') );
                add_filter(\'posts_join\', \'join_filter\') );
                break;
            default:
                break;
        }
    }

    function home_pages_where_filter($sql) {
        global $wpdb;
        $sql = " AND $wpdb->postmeta.meta_key = \'_wp_page_template\' AND $wpdb->postmeta.meta_value LIKE \'template-homepage-grid-%.php\'" . $sql;
        return $sql;
    }

    function landing_pages_where_filter($sql) {
        global $wpdb;
        $sql = " AND $wpdb->postmeta.meta_key = \'_wp_page_template\' AND $wpdb->postmeta.meta_value LIKE \'template-landing.php\'" . $sql;
        return $sql;
    }

    function join_filter($sql) {
        global $wpdb;
        $sql = " JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id " . $sql;
        return $sql;
    }
结束

相关推荐