如何创建自定义edit.php/编辑页面

时间:2012-12-30 作者:ptimson

我需要创建3个自定义edit.php 仅显示具有特定父级的页面的页面,并且“发布新页面”按钮链接到具有相应父级的新页面。

我能给出的最接近的答案是,不要编辑核心文件,只需删除元框等。How can I customize "Pages" admin (edit.php) and "Edit Page" admin (post.php) for bulk edit of custom content type?

但我不能这样做。有什么建议我可以复制edit.php

到目前为止,我已将代码复制到模板中,但它没有显示任何页面:

custom edit page

1 个回复
SO网友:brasofilo

一个菜单解决方案是可能的,我将在最后解决它,但另一个解决方案是使用挂钩添加自定义链接views_edit-$post_type.

My AccountCheckout 是两个父页。他们的链接将使用硬编码的页面ID:edit.php?post_type=page&checkout=1669 我们会抓住$_GET[\'checkout\'] 要调用的参数pre_get_posts

views_edit

# Called only in /wp-admin/edit.php* pages
add_action( \'load-edit.php\', function()
{
    # Not our post_type, bail out
    if ( get_current_screen()->id != \'edit-page\' )
        return;

    # Transient cache for pages IDs and Count
    # used in both hooks bellow
    do_cache_wpse_77721();

    # Check if our Query Var is defined and activate pre_get_posts   
    if( isset( $_GET[\'account\'] ) || isset( $_GET[\'checkout\'] ) )
        add_action( \'pre_get_posts\', \'pre_posts_wpse_77721\' );

    add_filter( \'views_edit-page\', \'custom_links_wpse_77721\' );
});

/**
 * Only display comments of specific post_id
 */ 
function pre_posts_wpse_77721( $query )
{
    # Just to play safe, but I think the hook is quite specificaly called
    if( !is_admin() )
        return $query;

    global $pagenow;

    # If there\'s no cache, bail out
    $cache = get_transient( \'custom_page_links\' );
    if( !$cache )
        return $query;

    # Define the IDs we want to query
    if( isset( $_GET[\'account\'] ) )
        $ids = $cache[\'account\'][\'ids\'];
    else
        $ids = $cache[\'checkout\'][\'ids\'];

    # Here, too, just playing safe
    if( \'edit.php\' == $pagenow && ( get_query_var(\'post_type\') && \'page\' == get_query_var(\'post_type\') ) )
        $query->set( \'post__in\', $ids );

    return $query;
}

/**
 * Add link to specific post comments with counter
 */
function custom_links_wpse_77721( $status_links )
{
    $cache = get_transient( \'custom_page_links\' );
    $count_checkout = sprintf(
        \'<span class="count">(%s)</span>\',
        $cache[\'checkout\'][\'count\']
    );
    $count_account = sprintf(
        \'<span class="count">(%s)</span>\',
        $cache[\'account\'][\'count\']
    );

    $link_account = \'edit.php?post_type=page&account=1670\';
    $link_checkout = \'edit.php?post_type=page&checkout=1669\';
    $link_all = \'<a href="edit.php?post_type=page">All</a>\';
    $separator = \'CUSTOM LINKS &#x27BD;\';

    if( isset( $_GET[\'checkout\'] ) ) 
    {
        $status_links[\'all\'] = $link_all;
        $status_links[\'my_sep\'] = $separator;
        $status_links[\'account\'] = "<a href=\'$link_account\'>My Account $count_account</a>";
        $status_links[\'checkout\'] = "<a href=\'$link_checkout\' class=\'current\'>Checkout $count_checkout</a>";
    }                             
    elseif( isset( $_GET[\'account\'] ) ) 
    {                             
        $status_links[\'all\'] = $link_all;
        $status_links[\'my_sep\'] = $separator;
        $status_links[\'account\'] = "<a href=\'$link_account\' class=\'current\'>My Account $count_account</a>";
        $status_links[\'checkout\'] = "<a href=\'$link_checkout\'>Checkout $count_checkout</a>";
    }                             
    else                          
    {                             
        $status_links[\'my_sep\'] = $separator;
        $status_links[\'account\'] = "<a href=\'$link_account\'>My Account $count_account</a>";
        $status_links[\'checkout\'] = "<a href=\'$link_checkout\'>Checkout $count_checkout</a>";
    }

    return $status_links;
}

/**
 * Makes the query once every hour
 * holds the Parent and Children ID, plus the Children total pages count
 */
function do_cache_wpse_77721()
{
    if( !get_transient( \'custom_page_links\' ) )
    {
        # Page 1
        $checkout_posts = get_children( \'post_parent=1669&post_type=page\' );
        // To include the parent ID in the query
        $c_ids = array( \'1669\' ); 
        // Grab the children IDs
        foreach( $checkout_posts as $check )
            $c_ids[] = $check->ID;
        $checkout = array( 
            \'ids\' => $c_ids,
            \'count\' => count( $checkout_posts )
        );

        # Page 2
        $account_posts = get_children( \'post_parent=1670&post_type=page\' );
        $a_ids = array( \'1670\' );
        foreach( $account_posts as $acc )
            $a_ids[] = $acc->ID;
        $account = array( 
            \'ids\' => $a_ids,
            \'count\' => count( $account_posts )
        );

        # Set transient
        $transient = array(
            \'checkout\' => $checkout,
            \'account\' => $account
        );
        set_transient( \'custom_page_links\', $transient, 60*60 );
    }
}
下面是如何向页面添加一个子菜单,该子菜单直接链接到父页面之一。我们必须使用jQuery来adjust the active/inactive states 用于菜单和子菜单。

add_action(\'admin_menu\', function() 
{
    add_submenu_page(
        \'edit.php?post_type=page\', 
        \'Checkout\', 
        \'Checkout\', 
        \'edit_pages\', 
        \'custom_parent_page\', 
        function()
        { 
            wp_redirect(admin_url(\'edit.php?post_type=page&checkout=1669\')); 
            exit; 
        }
    );
});

结束

相关推荐

限制WP-Admin中的摘录字段为文字

是否可以将文章页面上的摘录字段限制为文字?请注意,我知道可以重复摘录并限制其文字,但我希望字段本身限制字数,类似于文本区域的字符限制。Is such thing possible? 也许是Javascript解决方案?也许类似于this plugin 对角色生物字段执行(尽管其限制基于角色数量)。这是因为我运行了一个多作者平台,在这个平台上,用户经常会犯错误,超过帖子列表中打印的字数限制。