简体   繁体   English

Wordpress 我的自定义帖子类型的侧栏管理菜单

[英]Wordpress side bar admin menu for my custom post type

I have created a custom post type called "Home Page" using register_post_type and below is my code.我使用register_post_type创建了一个名为“主页”的自定义帖子类型,下面是我的代码。

$labels = array(
             
        'name' => __( 'Home Page'),
        'singular_name' => __( 'Home Page'),
        'edit_item' => __( 'Edit Home Page'),            
    );

$args = array(
        'label'               => __( 'Home Page'),
        'description'         => __( 'Custom Post Type'),
        'labels'              => $labels,       
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => false,        
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'map_meta_cap'        => fa,
        'capabilities' => array(
            'create_posts' => 'do_not_allow',
            'delete_published_posts' => false,
            'read_post' => false,
        ),
        
        
    );
register_post_type( 'homepage', $args );

And I have used "Advanced Custom Fields" WordPress plugin to add custom fields to this new custom post type.我已经使用“高级自定义字段”WordPress 插件将自定义字段添加到这个新的自定义帖子类型。 These all working fine and its showing like below.这些都工作正常,如下所示。

在此处输入图像描述

But the thing I want is, I will only have 1 record so if I click on "Home Page" on the left side bar admin menu link, it should be redirected to that particular record with something like post.php?post=37&action=edit in the URL.但我想要的是,我只有 1 条记录,所以如果我单击左侧栏管理菜单链接上的“主页”,它应该被重定向到该特定记录,如post.php?post=37&action=edit在 URL 中post.php?post=37&action=edit

I already know doing 'show_in_menu' => false, will hide the url from the left sidebar.我已经知道做'show_in_menu' => false,将从左侧边栏隐藏 url。

But how can I hide this menu and directly go to edit page for only single record which I will always have?但是我怎样才能隐藏这个菜单并直接 go 来编辑我将永远拥有的只有一条记录的页面?

Can someone guide me what should I do from here on to achieve this?有人可以指导我从现在开始我应该怎么做才能实现这一目标?

Thanks in advance.提前致谢。

if you have already made the first post:如果您已经发表了第一篇文章:

in your register_post_type add these parameters:在你的register_post_type添加这些参数:

'map_meta_cap' => true,
'capabilities' => array(
    'create_posts' => 'do_not_allow'
)

this will disable adding new posts to the post type.这将禁止向帖子类型添加新帖子。

And then the function to redirect the edit.php - making sure you pass the correct ID然后 function 重定向 edit.php - 确保你传递了正确的 ID

function redirect_edit_screen() {
    
    $current_screen = get_current_screen();
    
    if( $current_screen->id === "edit-homepage" ) {
        wp_safe_redirect( admin_url('post.php?post=37&action=edit') );
        exit();
    }
}
add_action( 'current_screen', 'redirect_edit_screen' );

OK Guys,好,朋友们,

Eventually, I have found a way to implement this feature.最终,我找到了一种实现此功能的方法。 Here is what I have done.这是我所做的。

// Adding custom menu for custom post type 
function add_link_to_category_tips_n_tricks() {
    $link = 'post.php?post=39&action=edit';
    add_menu_page( 'Home Page', 'Home Page', 'edit_pages', $link, '', 'dashicons-admin-home', 8 );
}
add_action( 'admin_menu', 'add_link_to_category_tips_n_tricks' );

This will create a new custom menu and will redirect at the page where I want.这将创建一个新的自定义菜单,并将重定向到我想要的页面。

Thanks all for your support and guidance.感谢大家的支持和指导。

One way of doing it will be to redirect on admin_init hook.一种方法是在admin_init挂钩上重定向。 Check if $_GET[post_type'] is set and equal to your custom post type and the $pagenow global is edit.php , Then simply redirect it using wp_redirect() .检查$_GET[post_type']是否设置为等于您的自定义帖子类型并且$pagenow全局为edit.php ,然后使用wp_redirect()简单地重定向它。

function redirect_to_edit() {

    global $pagenow;

    if ( isset($_GET['post_type']) && $_GET['post_type'] == 'homepage' && $pagenow == 'edit.php' ) {
      wp_redirect( admin_url('post.php?post=39&action=edit') );
      exit();
    }

}
add_action('admin_init', 'redirect_to_edit');

I would prefer the way to create a new custom menu and redirect it to the page needed.我更喜欢创建新的自定义菜单并将其重定向到所需页面的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM