Better Practice, Dec. 2018

(singke) #1
http://www.phparch.com \ December 2018 \ 7

Custom Post Types in WordPress

You can also disable Gutenberg for a specific custom post
type by passing a callback function to the filter:


function wiverb_disable_gutenberg($is_enabled, $post_type) {
if ($post_type === 'wiverb_bike') {
return false;
}
return $is_enabled;
}
add_filter('gutenberg_can_edit_post_type',
'wiverb_disable_gutenberg', 10 , 2 );


Read more about disabling Gutenberg editor from Digging
into WordPress^8

What About Custom Fields?
Adding custom fields is a powerful way to add functionality
and structure to your custom post type data. If you are using
the Advanced Custom Fields (ACF) plugin, you can specify
that your custom field groups will appear on your custom
post type. You can then use data entered in these fields for
custom post type queries and templating.

8 Digging into WordPress: https://phpa.me/digwp-disable-gutenberg

Listing 5


  1. <?php

  2. /*

  3. Plugin Name: WI Verbs Nested Post Types

  4. Plugin URI: https://wisconsinverbs.com

  5. Description: Display Custom Post Types in a nested menu

  6. Version: 1.

  7. Author: Andrea Roenning

  8. Author URI: https://andrearoenning.com

  9. License: GPL

  10. License URI: https://www.gnu.org/licenses/gpl-2.0.html

  11. Text Domain: wiverb

  12. */

  13. if(!function_exists('wiverb_post_types')) {

  14. /**



    • Adds custom post types



  15. */

  16. function wiverb_post_types() {

  17. /*



    • Hiking Trail listing



  18. */

  19. register_post_type( 'wiverb_hike',

  20. array(

  21. 'labels' => array(

  22. 'name' => __( 'Hiking Trails' ),

  23. 'singular_name' => __( 'Hiking Trail' ),

  24. 'add_new_item' => __('Add New Trail'),

  25. 'edit_item' => __('Edit Trail'),

  26. 'new_item' => __('New trail'),

  27. 'view_item' => __('View trail'),

  28. 'search_items' => __('Search trails'),

  29. 'not_found' => __('No trails were found'),

  30. 'not_found_in_trash' => ('No trails found in trash')

  31. ),

  32. 'public' => true,

  33. 'show_in_menu' => false,

  34. 'exclude_from_search' => true,

  35. 'supports' => array(

  36. 'title', 'editor', 'thumbnail',

  37. 'revisions', 'page-attributes'),

  38. 'rewrite' => array( 'slug' => 'trails/hiking' ),

  39. 'has_archive' => 'trails/hiking'

  40. )

  41. );



  42. /*



    • Biking Trail listing



  43. */

  44. register_post_type( 'wiverb_bike',

  45. array(

  46. 'labels' => array(

  47. 'name' => __( 'Biking Trails' ),

  48. 'singular_name' => ( 'Biking Trail' ),
    53. 'add_new_item' =>
    ('Add New Trail'),
    54. 'edit_item' => ('Edit Trail'),
    55. 'new_item' => __('New trail'),
    56. 'view_item' =>
    ('View trail'),
    57. 'search_items' => __('Search trails'),
    58. 'not_found' => __('No trails were found'),
    59. 'not_found_in_trash' => ('No trails found in trash')
    60. ),
    61. 'public' => true,
    62. 'show_in_menu' => false,
    63. 'exclude_from_search' => true,
    64. 'supports' => array(
    65. 'title', 'editor', 'thumbnail',
    66. 'revisions', 'page-attributes'),
    67. 'rewrite' => array( 'slug' => 'trails/biking' ),
    68. 'has_archive' => 'trails/biking'
    69. )
    70. );
    71. }
    72. add_action( 'init', 'wiverb_post_types' );
    73. }
    74. /
    75.
    Admin Controls
    76. */
    77. if (!function_exists('wiverbs_trails_menu')) {
    78. /
    79. Adds admin nav & subnav
    80.
    /
    81. function wiverbs_trails_menu() {
    82. add_menu_page(
    83. 'Trails', 'Trails', 'edit_posts',
    84. 'wiverbs_trails', 'wiverbs_trails_page',
    85. 'dashicons-location-alt', 5
    86. );
    87.
    88. add_submenu_page(
    89. 'wiverbs_trails', 'Hiking Trails', 'Hiking Trails',
    90. 'edit_posts', 'edit.php?post_type=wiverb_hike'
    91. );
    92.
    93. add_submenu_page(
    94. 'wiverbs_trails', 'Biking Trails', 'Biking Trails',
    95. 'edit_posts', 'edit.php?post_type=wiverb_bike'
    96. );
    97. }
    98. add_action( 'admin_menu', 'wiverbs_trails_menu' );
    99. }
    100.
    101. if (!function_exists('wiverbs_trails_page')) {
    102. /

    View Code ARchive for Full Listing

Free download pdf