如何在自定义帖子类型管理菜单中设置链接?

时间:2013-02-15 作者:Julian

我正在尝试创建一个自定义按钮,并将其链接到插件中的一个文件。How do I get this link pointing to the path of my choice?

Download Donors should point to the link I choose

我尝试了以下方法,但现在确实奏效了。

function _pfund_register_types() {
    $pfund_options = get_option( \'pfund_options\' );
    $template_def = array(
        \'public\' => true,
        \'query_var\' => \'pfund_cause\',
        \'rewrite\' => array(
            \'slug\' => $pfund_options[\'cause_slug\'],
            \'with_front\' => false,
        ),
        \'hierarchical\' => true,
        \'label\' => __( \'Causes\', \'pfund\' ),
        \'labels\' => array(
            \'name\' => __( \'Causes\', \'pfund\' ),
            \'singular_name\' => __( \'Cause\', \'pfund\' ),
            \'add_new\' => __( \'Add New Cause\', \'pfund\' ),
            \'add_new_item\' => __( \'Add New Cause\', \'pfund\' ),
            \'edit_item\' => __( \'Edit Cause\', \'pfund\' ),
            \'view_item\' => __( \'View Cause\', \'pfund\' ),
            \'search_items\' => __( \'Search Causes\', \'pfund\' ),
            \'not_found\' => __( \'No Causes Found\', \'pfund\' ),
            \'not_found_in_trash\' => __( \'No Causes Found In Trash\', \'pfund\' ),
        )
    );
    register_post_type( \'pfund_cause\', $template_def );
    register_post_type( \'pfund_cause_list\' );

    $campaign_def = array(
        \'public\' => true,
        \'query_var\' => \'pfund_campaign\',
        \'rewrite\' => array(
            \'slug\' => $pfund_options[\'campaign_slug\'],
            \'with_front\' => false
        ),
        \'hierarchical\' => true,
        \'label\' => __( \'Campaigns\', \'pfund\' ),
        \'labels\' => array(
            \'name\' => __( \'Campaigns\', \'pfund\' ),
            \'singular_name\' => __( \'Campaign\', \'pfund\' ),
            \'add_new\' => __( \'Add New Campaign\', \'pfund\' ),
            \'add_new_item\' => __( \'Add New Campaign\', \'pfund\' ),
            \'download_donors\' => __( \'Download Donors\', \'pfund\' ),
            \'all_items\' => __( \'Download Donors\', \'pfund\' ),
            \'edit_item\' => __( \'Edit Campaign\', \'pfund\' ),
            \'view_item\' => __( \'View Campaign\', \'pfund\' ),
            \'search_items\' => __( \'Search Campaigns\', \'pfund\' ),
            \'not_found\' => __( \'No Campaigns Found\', \'pfund\' ),
            \'not_found_in_trash\' => __( \'No Campaigns Found In Trash\', \'pfund\' ),
        ),
        \'supports\' => array(
            \'title\'
        ),
        \'capabilities\' => array(
            \'edit_post\' => \'edit_campaign\'
        ),
        \'map_meta_cap\' => true
    );
    register_post_type( \'pfund_campaign\', $campaign_def );
    register_post_type( \'pfund_campaign_list\' );
}
注意线条

\'download_donors\' => __( \'Download Donors\', \'pfund\' ),
        \'all_items\' => __( \'Download Donors\', \'pfund\' ),
“下载捐赠者”标签不起作用。我必须使用“all\\u items”。不知道为什么会这样,但当我尝试添加过滤器时,它不会被覆盖。相反,链接指向wp-admin/edit.php?post_type=pfund_campaign

add_filter(\'post_row_actions\',\'all_items\', 10, 2);

function download_donors($actions, $post){
    //check for your post type
    if ($post->post_type =="pfund_campaign"){
        //remove the default edit
        unset($actions[\'edit\']);
        //set new action
        $actions[\'download_donors\'] = \'<a href="http://www.google.com">Download Donors</a>\';
    }
    return $actions;
}

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

我不知道你是从哪里想到添加“download\\u捐助者”可以满足你的需要的,文档中没有,它与标签捆绑在一起,只是没有任何东西可以利用它。

注册帖子类型时的标签不是管理菜单的定义。

如果你想添加一个管理菜单,你需要像其他任何东西一样通过add_submenu_page

http://codex.wordpress.org/Function_Reference/add_submenu_page

e、 g.:

add_action(\'admin_menu\', \'register_my_custom_submenu_page\');

function register_my_custom_submenu_page() {
    add_submenu_page( \'tools.php\', \'My Custom Submenu Page\', \'My Custom Submenu Page\', \'manage_options\', \'my-custom-submenu-page\', \'my_custom_submenu_page_callback\' ); 
}

function my_custom_submenu_page_callback() {
    echo \'<h3>My Custom Submenu Page</h3>\';

}
将菜单链接到外部页面是不可能的,您需要拦截页面加载并执行wp\\u重定向调用,但这会误导用户和糟糕的UI。你最好在新的子页面上放一个大的下载按钮

结束