web-dev-qa-db-ja.com

カスタム投稿タイプのパーマリンクを削除

私は以下で投稿タイプを登録しました -

$holidayLabels = array(
    'name' => __( 'Holidays'),
    'singular_name' => __( 'Holidays'),
    'all_items' => __( 'All Holidays'),
    'add_new' => __( 'Add New Holiday'),
    'add_new_item' => __( 'Add New Holiday'),
    'edit_item' => __( 'Edit Holiday'),
    'new_item' => __( 'New Holiday'),
    'view_item' => __( 'View Holidays'),
    'not_found' => __( 'No Holidays found'),
    'not_found_in_trash' => __( 'No Holidays found in Trash'),
    'parent_item_colon' => ''

);

$holidayArgs = array(
    'labels'               => $holidayLabels,
    'public'               => true,
    'publicly_queryable'   => true,
    '_builtin'             => false,
    'show_ui'              => true,
    'query_var'            => true,
    'rewrite'              => array( "slug" => "holidays" ),
    'capability_type'      => 'post',
    'hierarchical'         => false,
    //'menu_position'        => 6,
    'supports'             => array( 'title'),
    'has_archive'          => false,
    'show_in_nav_menus'    => false,

);
register_post_type('holidays', $holidayArgs);

また、新しい休日を投稿したり、既存の休日の編集を開始したりするときに、タイトルの下に表示されているパーマリンクを削除したいと思います。 enter image description here

休日は別のウィジェットに表示されるので、私はこれを削除したいです。とにかく管理者がそれを単一の投稿として見ることができないようにしたいのです。そのようなテンプレートは定義されていません。

13
SachinGutte

まあ、別の方法があります。そしてもっと良かったと思います。

register_post_type parametersを見てください。おそらく次のように設定してください。

'public' => false,  // it's not public, it shouldn't have it's own permalink, and so on
'publicly_queryable' => true,  // you should be able to query it
'show_ui' => true,  // you should be able to edit it in wp-admin
'exclude_from_search' => true,  // you should exclude it from search results
'show_in_nav_menus' => false,  // you shouldn't be able to add it to menus
'has_archive' => false,  // it shouldn't have archive page
'rewrite' => false,  // it shouldn't have rewrite rules

投稿タイプが公開されていない場合は、この部分のエディタは表示されません。

40

さて、1つの簡単な方法はCSSを使用してコンテナのdivを隠すことです。

add_action('admin_head', 'wpds_custom_admin_post_css');
function wpds_custom_admin_post_css() {

    global $post_type;

    if ($post_type == 'post_type') {
        echo "<style>#edit-slug-box {display:none;}</style>";
    }
}
1
M-R

また、小さなJavaScriptコードをadmin_footerフックに配置して、この領域を隠すこともできます。

<?php
add_action('admin_footer', function() {
  global $post_type;
  if ($post_type == 'your_custom_post_type') {
    echo '<script> document.getElementById("edit-slug-box").outerHTML = ""; </script>';
  }
});
1
Eh Jewel